Switch script does not executed when command come via MQTT

Moderator: leecollings

Post Reply
bebeno
Posts: 3
Joined: Tuesday 24 January 2017 5:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Contact:

Switch script does not executed when command come via MQTT

Post by bebeno »

Hi guys.

I would like to ask you for favour. I have a very strange problem. May be it can by strange only for me but I can not fin d a answer how to fix my problem .

In domoticz I have a virtual switch ( push Off button ). When I press this button script for shutting down my local PC is executed and PC goes to Off. This is working perfectly. When I execute this via http command same result. Works perfectly.

But my problem is when I send I command via MQTT. It means When I pres a button on Sonoff switch ( with TASMOTA software ) , sonoff switch send a mqtt comand to domoticz . In domoticz console I see log with command off. No errors but script for PC shuttdown is not executed.

TASMOTA : 8.1
Domoticz Version: 4.10717
Domoticz switch Off action : "script:///home/pi/domoticz/scripts/shutdown_PC.sh"
script shutdown_PC.sh : net rpc -S 192.168.4.2 -U NAME%PASS shutdown -t 1 -f
Log in domoticz console after sonoff switch is pressed : MQTT: Topic: domoticz/in, Message: {"idx":13,"nvalue":0,"svalue":"","Battery":100,"RSSI":10}

Any idea what can be wrong?

Thanks for help.
salvacalatayud
Posts: 112
Joined: Monday 26 June 2017 21:16
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Spain
Contact:

Re: Switch script does not executed when command come via MQTT

Post by salvacalatayud »

Not sure, but maybe you can try to change in tasmota config from button to switch, and set SwitchMode 2. This will send an on command when pushed and an off command when released. If your script does nothing when switched on, I think this can work.
bebeno
Posts: 3
Joined: Tuesday 24 January 2017 5:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Contact:

Re: Switch script does not executed when command come via MQTT

Post by bebeno »

Hi may.
Thanks for answer.

I think problem is not in Tasmota settings because, when I push button on sonoff switch rule is executed. I tasmota there is rule for long press.
In log on tasmota I see that command has been succesfuly sent and in Domoticz log I see same. Domoticz received MQTT command from Tasmota . The problem is that scrip in Domoticz is not executed when this command is received.

But when I change type of Domoticz button from PUSH OFF to normal switch, I can control from Tasmota this switch without any problem , but script is not executed .
I think there is any problem in Domoticz, but Ido not know how to find this problem

Many Thanks
desliem
Posts: 8
Joined: Sunday 04 December 2016 16:51
Target OS: -
Domoticz version: 2022.1
Location: Canada (N.-B.)
Contact:

Re: Switch script does not executed when command come via MQTT

Post by desliem »

Someone contacted me about the same problem and we both investigated and came up with the same asymmetry reported by bebeno.

A virtual On/Off switch created with Dummy hardware will execute its On Action and Off Action http request when turned on or off in the Domoticz Web server or in response to an Http request. It will not execute the On or Off Action when the command is send by MQTT message even as it appropriately changes its state to on or off. To be clear

Code: Select all

~$ curl "http://<DOMOTICZ_IP:DOMOTICZ_PORT>/json.htm?type=command&param=switchlight&idx=44&switchcmd=On"
On Action is executed

Code: Select all

~$ mosquitto_pub -h <MQTT_IP> -t domoticz/in -m '{ "idx" : 44, "nvalue" : 1}'
On Action is not executed

(2023-06-23: fixed an error in each of the above, which changes nothing about the validity of waaren's reply below)

I don't believe this has anything to do with Tasmota or whatever firmware the hardware device is running. Checking the Domoticz log and monitoring all Domoticz MQTT in and out messages, showed that Domoticz did receive the MQTT (of course since it changes the state of the device in the Web interface) but it did not execute the On or Off action and did not push a Domoticz/out MQTT message.

Strangely, if the MQTT command is for a scene (type group or scene, doesn't matter), the appropriate On or Off Action is executed.

Is this a bug in Domoticz? It would be nice if others confirmed before I try to raise an issue on the Domoticz github.

@bebeno : I proposed two ways to work around this problem to my contact who wanted to execute the following request http://192.168.1.86/control?cmd=pulse,12,0,400 to toggle a relay when the virtual device state was changed.

1) Use a scene (type scene) instead of a switch and set its On Action to 'http://192.168.1.86/control?cmd=pulse,12,0,400'

2) Handle the problem in a Domoticz system script. Here is DzVents script that should work

Code: Select all

return {
  on = { devices = {44} },
  execute = function(dz)
  dz.openURL('http://192.168.1.86/control?cmd=pulse,12,0,400') -- url to toggle relay
  end
}
If you need to execute different On and Off actions, then

1) Use a scene of type group and set the On Action and Off Action as required.

2) Modify the DzVents script in a fashion similar to this:

Code: Select all

return {
  on = { devices = {44} },
  execute = function(dz, swicth)
     if (switch.state == 'On') then
        dz.openURL('http://192.168.1.86/control?cmd=relay,1')  -- url to turn relay on
     else
        dz.openURL('http://192.168.1.86/control?cmd=relay,0')  -- url to turn relay off
     end
  end
}

Domoticz Version: 4.10717
Build Hash: b38b49e5
Compile Date: 2019-05-09 08:04:08
dzVents Version: 2.4.19
Python Version: 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.2.0]
Last edited by desliem on Friday 23 June 2023 23:14, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Switch script does not executed when command come via MQTT

Post by waaren »

desliem wrote: Wednesday 25 November 2020 2:50 A virtual On/Off switch created with Dummy hardware will execute its On Action and Off Action http request when turned on or off in the Domoticz Web server or in response to an Http request. It will not execute the On or Off Action when the command is send by MQTT message even as it appropriately changes its state to on or off. To be clear

Code: Select all

~$ curl "http:<DOMOTICZ_IP:DOMOTICZ_PORT>/json.htm?type=command&param=switchlight&idx=44&switchcmd=On"
On Action is executed

Code: Select all

~$ mosquitto_pub -h <DOMOTICZ_IP> -t domoticz/in -m '{ "idx" : 44, "nvalue" : 1}'
On Action is not executed
These two commands are not equivalent. If you need the same behavior in domoticz as from the curl command using the MQTT, you should use

Code: Select all

~$  mosquitto_pub -h <DOMOTICZ_IP> -t domoticz/in -m {"command": "switchlight", "idx": 44, "switchcmd": "On" }
On Action is executed
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
desliem
Posts: 8
Joined: Sunday 04 December 2016 16:51
Target OS: -
Domoticz version: 2022.1
Location: Canada (N.-B.)
Contact:

Re: Switch script does not executed when command come via MQTT

Post by desliem »

Duh!!! Feel stupid. Many thanks yet again to waaren for his help.

2023-06-23:

The MQTT publish message should be sent to the MQTT broker:

Code: Select all

~$  mosquitto_pub -h <[b]MQTT_IP[/b]> -t domoticz/in -m {"command": "switchlight", "idx": 44, "switchcmd": "On" }
waaren was just quoting my initial error made because the MQTT broker and the Domoticz server happened to be on the same computer.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest