switch device AND ledbutton

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

switch device AND ledbutton

Post 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.
User avatar
waltervl
Posts: 5736
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: switch device AND ledbutton

Post 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
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: switch device AND ledbutton

Post 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
User avatar
waltervl
Posts: 5736
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: switch device AND ledbutton

Post 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
waltervl
Posts: 5736
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: switch device AND ledbutton

Post 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
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: switch device AND ledbutton

Post 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.
Image

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'
Attachments
Scherm­afbeelding 2024-02-07 om 17.19.58.png
Scherm­afbeelding 2024-02-07 om 17.19.58.png (114.99 KiB) Viewed 702 times
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: switch device AND ledbutton

Post 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'?
User avatar
waltervl
Posts: 5736
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: switch device AND ledbutton

Post by waltervl »

Try
domoticz.executeShellCommand ('mosquitto_pub -h 10.0.1.122 -p 1883 -t domoticz/out/buttonplus/wk1/button/1l/ledrgb/ -m 8323072')
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: switch device AND ledbutton

Post by remko2000 »

that gives the same result:
domoticz/out/buttonplus/wk1/button/1l/ledrgb/ 8323072
pikassent
Posts: 16
Joined: Saturday 03 December 2022 16:26
Target OS: Linux
Domoticz version: 2024.4b
Location: Spain
Contact:

Re: switch device AND ledbutton

Post 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>
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: switch device AND ledbutton

Post 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?
User avatar
waltervl
Posts: 5736
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: switch device AND ledbutton

Post by waltervl »

Just copy the execute shell command line and put the other mqtt command in it.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: switch device AND ledbutton

Post 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.
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: switch device AND ledbutton

Post 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.....
User avatar
waltervl
Posts: 5736
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: switch device AND ledbutton

Post by waltervl »

The error is referring to line 15 (buttonplusled1r.lua:15) wich probably is the line

Code: Select all

domoticz.log(device.text)
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)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
waltervl
Posts: 5736
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: switch device AND ledbutton

Post by waltervl »

additional: device.text will also give an error because it should probably be device.name (String. Name of the device)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: switch device AND ledbutton

Post 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?
User avatar
waltervl
Posts: 5736
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: switch device AND ledbutton

Post 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.
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: Bing [Bot] and 1 guest