Need advice for Mqtt pub Topic is solved

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
User avatar
TiXav
Posts: 63
Joined: Saturday 28 November 2015 22:25
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France
Contact:

Need advice for Mqtt pub

Post by TiXav »

I use domoticz on docker. I need to start script outside of docker, mainly python scripts with pychromcast (it cannot be use in docker due to broadcast on a different ip of docker container ) to control Google speaker
Today I used dzvents script to ask to run a script from "docker side" via mosquito

Code: Select all

function RunScript(DZ,s)
	-- s : command to execute
	-- add " on each side !
	local CmdStr ='mosquitto_pub -h 192.168.1.4 -t xGate/cmd -m "'..s..'"'
	DZ.executeShellCommand({
	command = CmdStr,
	callback = 'Retour_MqttScript',
	timeout = 3	,
})
and on the "Raspi side" a sh script service (I adapted it) to start the script I want to run

Code: Select all

#!/bin/sh

# https://gist.github.com/David-Lor/63fb0be80b67359c8de6230c6b1dafa2

while true  # Keep an infinite loop to reconnect when connection lost/broker unavailable
do
    mosquitto_sub -h localhost -t xGate/cmd/\# -F "%t %p" | while read -r payload
    do
		echo "$payload"
        # Here is the callback to execute whenever you receive a message:
        topic=$(echo "$payload" | cut -d ' ' -f 1)
        msg=$(echo "$payload" | cut -d ' ' -f 2-)
        # echo "Rx MQTT: $topic: ${payload}"
        echo "${msg}"
        # p=$(echo "$msg" | jq '.property')
        # echo "Extracted property: $p for $topic"
		eval "$msg" &
    done
    sleep 10  # Wait 10 seconds until reconnection
done # &  # Discomment the & to run in background (but you should rather run THIS script in background)
That is working well for at leat a month but I would like to know if there is easier way to do this as existing MQTT client plugin or other to do the MQTT pub in dzvent

Thanks in advance for answers ...
User avatar
waltervl
Posts: 6676
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: Need advice for Mqtt pub

Post by waltervl »

What if you give the Chromecast a fixed IP on your router? Then the script does not need to discover it and it should also work from the container....
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest