[SOLVED] Remote python system system to receive OH command

Hi, I know that is a bit off topic, but I have this strange config where I have a remote (still in the same network segment) system (RPI) running a python script to query some nonstandard hardware that does not have a binding.
Reporting of the values to my main OH is done by MQTT and that works great.
But I now need to send a command to this remote system from the OH (a simple 0/1) switch. And I wonder what is the easy way to do that? Any suggestions?

TIA
Maciej

Send an MQTT command message. Here is an example where I have a seperate RPI running with zigbee2mqtt, OH can both receive and send messages to control the light.

Switch TiLight "Ti Light"    <light> ["Lighting"] { mqtt=">[pibroker:zigbee/0xb0ce1814030ac279/set:command:*:JS(setZigbeeState.js)],<[pibroker:zigbee/0xb0ce1814030ac279:state:JSONPATH($.state)]", expire="120m,command=OFF" }
Dimmer TiLight_Level "Ti Light Level"    <light> ["Lighting"] { mqtt=">[pibroker:zigbee/0xb0ce1814030ac279/set:command:*:JS(setZigbeeBrightness.js)],<[pibroker:zigbee/0xb0ce1814030ac279:state:JS(getZigbeeBrightness.js)]" }
1 Like

Ok, great, thanks for the example syntax, revealing is that you can receive and send the command on the same Item. That helps a lot.
Do you have some code example how to receive mqtt on the remote system in python?

Small script I use on a remote pi to play the call to prayer:
See:

import paho.mqtt.client as mqttClient
import time
import os
import subprocess

def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("Connected to broker")
        global Connected
        Connected =True
    else:
        print("Connection failed")

def on_message(client, userdata, message):
    if message.payload == 'ON':
        print "Message received: " + message.topic + " : " + message.payload
        os.system('clear')
        time.sleep(1.5)
        if message.topic == 'Misc/PrayerTrigger':
            subprocess.Popen(["omxplayer", "/home/pi/share/adhan.mp4", "--aspect-mode", "fill"])
        else:
            subprocess.Popen(["omxplayer", "/home/pi/share/adhanfajr.mp4", "--aspect-mode", "fill"])

Connected = False

broker_address = "XXX.XXX.XXX.XXX" #Your MQTT broker IP address
port = 1883 #default port change as required
user = "xxxxxx" #mqtt user name change as required
password = "xxxxxxxx" #mqtt password change as required
client = mqttClient.Client("prayertime")
client.username_pw_set(user, password=password)
client.on_connect = on_connect
client.on_message = on_message

client.connect(broker_address, port=port)
client.loop_start()

while Connected != True:
    time.sleep(0.1)

client.subscribe('Misc/PrayerTrigger')
client.subscribe('Misc/PrayerTriggerFajr')

try:
    while True:
        time.sleep(1)

except KeyboardInterrupt:
    print "exiting"
    client.disconnect()
    client.loop_stop()

2 Likes

I’ve posted a Python script I use for this sort of thing that has kind of taken off well beyond it’s original intended purpose. https://github.com/rkoshak/sensorReporter

It implements the two way MQTT for you and supports plug-ins.

Can you mark the thread as solved, please?
Thanks