Sending MQTT with Remote Button Press

  • Hi,

    i have a question at my libreelec installation.

    It runs on RPi3 at version 18.8


    Is it possible with a button press on the remote control to publish a string via mqtt?

    Example:

    The button "Power" is pressed on the remote.

    After that the string "OFF" should be published on the topic "device/Power"

    is it possible?

    Maybe a script or something?

    I still have searched the internet, but didn´t find the right answers.


    Can anyone give me a hint?

    Thanks in advance

    Yankee

  • Hello,

    thanks for the response.

    i have installed the addon "kodi2mqtt"

    i am trying to get a python script to run, but i am getting an error.

    The script looks like this:

    at the command-line i am trying the script to work like this:

    "python test.py"

    And the error:

    Code
    Traceback (most recent call last):
      File "test.py", line 3, in <module>
        import paho.mqtt.client as mqtt #import the client1
    ImportError: No module named paho.mqtt.client

    The python-script doesn´t work.

    Can someone help me with this script?

    do i have to install some addons?

    At the command-line it is not possible to install something.

    Greetings

    Yankee

    Edited 2 times, last by Yankee_1978 (December 1, 2020 at 1:39 PM).

  • I am sorry but it's another story - maybe we should not mix more different questions to the same thread.

    I don't know (yet) the kodi2mqtt add-on (and it looks it's not an official add-on in LE?) but make sure you have installed / configured all (possible) necessary prerequisites, maybe you should look into kodi2mqtt documentation or visit some related forum thread.

    In this case you don't have (at least) the path to the paho.mqtt.client (which is IMHO the important prerequisite) module specified so that's why you can't run the test script.

    Edited 6 times, last by ghtester (December 1, 2020 at 4:38 PM).

  • Thanks for your response.

    I changed the script this way, but it still won´t run... :(

    Script:

    This error occurs:

    Code
        client = mqtt.Client("P1") #create new instance
    NameError: name 'mqtt' is not defined

    What do i have to do to define "mqtt"

    "import sys as mqtt" doesn´t work.

  • Yeah, now it works.

    The script looks like this:


    Code
    import sys
    sys.path.append('/storage/.kodi/addons/service.mqtt/lib')
    import client as mqtt
    
    broker_address="192.168.XXX.XXX" 
    
    client = mqtt.Client("P1") #create new instance
    client.connect(broker_address) #connect to broker
    client.publish("cmnd/bruecke1/power","ON")#publish

    Now when the script is started this way

    "python test.py"

    a payload named "ON" is published to the topic "cmnd/bruecke1/power".

    Thanks for your help.

    Maybe this can help someone else...

    Greetings

    Yankee