Page 1 of 1
switch device AND ledbutton
Posted: Wednesday 07 February 2024 12:59
by remko2000
I am setting up an MQTT display (button+
https://button.plus/). There are also physical buttons on this. I want to switch lighting with 1 button. E.g. with payload: {"command": "switchlight", "idx": 268, "switchcmd": "Toggle" }
However, I also want that when this switch (idx268) is switched in domoticz, an MQTT command is sent from domoticz that turns on the LED of this button so that I see that the relevant light is on (and vice versa).
I can control this LED via topic 'buttonplus/wk1/button/7/ledrgb' + the RGB number such as '8323072'.
What is the best (and easiest) way to switch this in Domoticz? Script? What should this approximately look like? Unfortunately I don't know enough about domoticz to come up with a good solution for this.
Re: switch device AND ledbutton
Posted: Wednesday 07 February 2024 14:35
by waltervl
What happened with this:
https://www.domoticz.com/forum/viewtopic.php?p=313495
You can make a dzvents script that sends mqtt payloads to your button plus based on the domoticz trigger device. Similar as the script you used in the previous topic
Re: switch device AND ledbutton
Posted: Wednesday 07 February 2024 15:06
by remko2000
I listened to your advice and now use an execute shell. I use a script for each value. I tried converting such a script to this functionality:
Code: Select all
return {
on = {
devices = {
'lamp keukenkast'
}
},
logging = {
level = domoticz.LOG_INFO,
marker = 'template',
},
execute = function(domoticz, device)
domoticz.log('Device ' .. device .. ' was changed', domoticz.LOG_INFO)
domoticz.log(device.text)
domoticz.executeShellCommand ( 'mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/ButtonPlus/wk7/btton/7/ledrgb/8323072 -m ' .. device.. '')
end
}
However, this gives error messages, so I am doing something wrong somewhere.....
Error:
2024-02-07 12:43:38.196 Error: dzVents: Error: (3.1.8) template: ...e/pi/domoticz/scripts/dzVents/generated_scripts/test.lua:12: attempt to concatenate a table value (local 'device')
The RGB color number must also be sent as a 'raw' value, I don't know if this is done this way
Re: switch device AND ledbutton
Posted: Wednesday 07 February 2024 15:56
by waltervl
Your device is an device object, So use device.name to log the device name in
Code: Select all
domoticz.log('Device ' .. device.name .. ' was changed', domoticz.LOG_INFO)
The same probably for the device in the mosquitto_pub command.
What is the correct mosquitto_pub command to make the button change color?
I suppose it should be something like
Code: Select all
mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/ButtonPlus/wk7/btton/7/ledrgb -m 8323072
or when rgb value as text
Code: Select all
mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/ButtonPlus/wk7/btton/7/ledrgb -m "8323072"
t=mqtt topic
m= mqtt message, so the value to be set onto the topic.
Re: switch device AND ledbutton
Posted: Wednesday 07 February 2024 16:03
by waltervl
Additional I would advise you to use mqtt explorer were you can look to the mqtt topics and also send messages to test.
https://www.emqx.com/en/blog/connecting ... t-explorer
Re: switch device AND ledbutton
Posted: Wednesday 07 February 2024 17:23
by remko2000
I'm using mqtt explorer. With this command 'domoticz/out/buttonplus/wk1/button/1l/ledrgb' I can switch a buttonLed with the value ' 8323072' as a raw value.
I adjust my script:
Code: Select all
return {
on = {
devices = {
'Lamp keukenkast'
}
},
logging = {
level = domoticz.LOG_INFO,
marker = 'template',
},
execute = function(domoticz, device)
domoticz.log('Device ' .. device.name .. ' was changed', domoticz.LOG_INFO)
domoticz.log(device.text)
domoticz.executeShellCommand ( 'mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/buttonplus/wk1/button/1l/ledrgb -m 8323072' .. device.name.. '')
end
}
Nothing happened. I also tried:
'domoticz.executeShellCommand ( 'mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/buttonplus/wk1/button/1l/ledrgb -m "8323072"' .. device.name.. '')'
Same result (my log says ' 2024-02-07 17:22:57.095 Status: dzVents: Info: template: nil'
Re: switch device AND ledbutton
Posted: Wednesday 07 February 2024 17:45
by remko2000
I think I' almost there:
if I use in the sript:
domoticz.executeShellCommand ('mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/buttonplus/wk1/button/1l/ledrgb/ -m "8323072"')
I get in mqtt explorer: 'domoticz/out/buttonplus/wk1/button/1l/ledrgb/ 8323072'
there are 2 spaces in between, maybe this is the problem my buttonled doesn’t switch on?
This is sent as raw data with ' -m'?
Re: switch device AND ledbutton
Posted: Wednesday 07 February 2024 19:23
by waltervl
Try
domoticz.executeShellCommand ('mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/buttonplus/wk1/button/1l/ledrgb/ -m 8323072')
Re: switch device AND ledbutton
Posted: Wednesday 07 February 2024 20:08
by remko2000
that gives the same result:
domoticz/out/buttonplus/wk1/button/1l/ledrgb/ 8323072
Re: switch device AND ledbutton
Posted: Wednesday 07 February 2024 20:13
by pikassent
Hi,
Try to remove the trailing slash:
Code: Select all
domoticz.executeShellCommand ('mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/buttonplus/wk1/button/1l/ledrgb -m 8323072')
maybe you are publishing in topic domoticz/out/buttonplus/wk1/button/1l/ledrgb/<blank>
Re: switch device AND ledbutton
Posted: Wednesday 07 February 2024 20:22
by remko2000
pikassent wrote: ↑Wednesday 07 February 2024 20:13
Hi,
Try to remove the trailing slash:
Code: Select all
domoticz.executeShellCommand ('mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/buttonplus/wk1/button/1l/ledrgb -m 8323072')
maybe you are publishing in topic domoticz/out/buttonplus/wk1/button/1l/ledrgb/<blank>
That do the trick. Many thanx! When I switch off the led does not switch. I have to send ' off' or '0' when off. Is there a way to combine this in one script?
Re: switch device AND ledbutton
Posted: Wednesday 07 February 2024 22:03
by waltervl
Just copy the execute shell command line and put the other mqtt command in it.
Re: switch device AND ledbutton
Posted: Thursday 08 February 2024 11:24
by remko2000
I combined these two execute shell commands with this script:
Code: Select all
return {
on = {
devices = {
'device xx'
}
},
execute = function(domoticz, switch)
if (switch.state == 'On') then
domoticz.executeShellCommand ('mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/buttonplus/wk1/button/1l/ledrgb -m 8323072')
domoticz.log(device.text)
-- domoticz.notify('Hey!', 'I am on!',
-- domoticz.PRIORITY_NORMAL)
else
domoticz.executeShellCommand ('mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/buttonplus/wk1/button/1l/ledrgb -m 0')
domoticz.log(device.text)
-- domoticz.notify('Hey!', 'I am off!',
-- domoticz.PRIORITY_NORMAL)
end
end
}
This works fine.
Re: switch device AND ledbutton
Posted: Monday 12 February 2024 16:07
by remko2000
correction; it doesn't work very well anyway. I get an error in my log that I cannot explain:
Code: Select all
return {
on = {
devices = {
'Beveiliging buttonplus schakelen'
}
},
execute = function(domoticz, switch)
if (switch.state == 'On') then
domoticz.executeShellCommand ('mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/buttonplus/wk1/button/1r/ledrgb -m 8323072')
domoticz.log(device.text)
-- domoticz.notify('Hey!', 'I am on!',
-- domoticz.PRIORITY_NORMAL)
else
domoticz.executeShellCommand ('mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/buttonplus/wk1/button/1r/ledrgb -m 0')
domoticz.log(device.text)
-- domoticz.notify('Hey!', 'I am off!',
-- domoticz.PRIORITY_NORMAL)
end
end
}
Error:
2024-02-12 16:06:27.153 Error: dzVents: Error: (3.1.8) An error occurred when calling event handler buttonplusled1r
2024-02-12 16:06:27.153 Error: dzVents: Error: (3.1.8) ...cz/scripts/dzVents/generated_scripts/buttonplusled1r.lua:15: attempt to index a nil value (global 'device')
2024-02-12 16:06:27.723 Error: dzVents: Error: (3.1.8) An error occurred when calling event handler buttonplusled1r
2024-02-12 16:06:27.723 Error: dzVents: Error: (3.1.8) ...cz/scripts/dzVents/generated_scripts/buttonplusled1r.lua:15: attempt to index a nil value (global 'device')
What does this mean/what am I doing wrong?
The script also keeps running continuously and that is not the intention. The script only has to do its work in the event of a switch state change.....
Re: switch device AND ledbutton
Posted: Monday 12 February 2024 16:33
by waltervl
The error is referring to line 15 (buttonplusled1r.lua:15) wich probably is the line
You have that 2 times in your script. But "device" is not known in your script. so nil value, as you you execute the script with
Code: Select all
execute = function(domoticz, switch)
you better change that in
Code: Select all
execute = function(domoticz, device)
Re: switch device AND ledbutton
Posted: Monday 12 February 2024 16:36
by waltervl
additional: device.text will also give an error because it should probably be device.name (String. Name of the device)
Re: switch device AND ledbutton
Posted: Thursday 15 February 2024 9:41
by remko2000
thx, I adjust the 'switch' function.
This is my script now:
Code: Select all
return {
on = {
devices = {
'Beveiliging buttonplus schakelen'
}
},
execute = function(domoticz, device)
if (device.state == 'On') then
domoticz.executeShellCommand ('mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/buttonplus/wk1/button/1r/ledrgb -m 8323072')
domoticz.log(device.text)
-- domoticz.notify('Hey!', 'I am on!',
-- domoticz.PRIORITY_NORMAL)
else
domoticz.executeShellCommand ('mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/buttonplus/wk1/button/1r/ledrgb -m 0')
domoticz.log(device.text)
-- domoticz.notify('Hey!', 'I am off!',
-- domoticz.PRIORITY_NORMAL)
end
end
}
With this code it looks like the 'off' function keeps repeating:
2024-02-15 09:38:37.392 Status: dzVents: Info: Handling events for: "Beveiliging buttonplus schakelen", value: "Off"
2024-02-15 09:38:37.393 Status: dzVents: Info: ------ Start internal script: buttonplusled1r: Device: "Beveiliging buttonplus schakelen (QuinLED)", Index: 269
2024-02-15 09:38:37.393 Status: dzVents: Info: nil
2024-02-15 09:38:37.393 Status: dzVents: Info: ------ Finished buttonplusled1r
2024-02-15 09:38:37.394 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2024-02-15 09:38:37.705 Status: dzVents: Info: Handling events for: "Beveiliging buttonplus schakelen", value: "Off"
2024-02-15 09:38:37.705 Status: dzVents: Info: ------ Start internal script: buttonplusled1r: Device: "Beveiliging buttonplus schakelen (QuinLED)", Index: 269
2024-02-15 09:38:37.705 Status: dzVents: Info: nil
2024-02-15 09:38:37.705 Status: dzVents: Info: ------ Finished buttonplusled1r
2024-02-15 09:38:37.706 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
Is there a way the script does nothing until the state of 'Beveiliging buttonplus schakelen' is changing?
Re: switch device AND ledbutton
Posted: Thursday 15 February 2024 10:21
by waltervl
according your script it should only be triggered when device 'Beveiliging buttonplus schakelen' is being switched.
PS
you still have a message like 2024-02-15 09:38:37.393 Status: dzVents: Info: nil
this is because of the lines
domoticz.log(device.text)
that should be changed into
domoticz.log(device.name)
as device.text is nothing..... so nil.