Changing Dimmer level with Selector switch

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

Moderator: leecollings

Post Reply
Hann1BaL
Posts: 23
Joined: Sunday 11 March 2018 15:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Contact:

Changing Dimmer level with Selector switch

Post by Hann1BaL »

Hi all,

I have a square IKEA Tradfri dimmer, which, when added to Domoticz is added as Selector switch with 5 options: 1 On, 2 Off, 3 Up, 4 Down, 5 Stop.
In Zigbee2MQTT I see the actions:
1. On: press the 1
2. Off: press the 0
3. Up: Hold the 1
4. Down: Hold the 0
5. Stop: State if the dimmer when you release 1 or 0 after holding it.

Then I have a different dimmer lightswitch Dimmer that I want to control with the IKEA dimmer.
On and Off is not a problem, that works:

Code: Select all

return {
	on = {
		devices = {
			1257,
		}
	},
	execute = function(domoticz, dimmer)
		   local dimmer = domoticz.devices(1257)
		   local lightswitch = domoticz.devices(1268)
		 if (dimmer.state == 'On') then
		    lightswitch.switchOn()
		    
		elseif (dimmer.state == 'Off') then 
		    lightswitch.switchOff()
		   
		elseif (dimmer.state == 'Up') then
		    
	    elseif (dimmer.state == 'Down') then
	        
	    else
	    domoticz.log(lightswitch.state)
		end
	end
}
What I want is when I hold the button it puts the dimmer up with lets say 20% and if you hold if for more than 2 sec, another 20%. When you release it, it goes to Stop and it should keep the current state (for now I added just as log in there.)

But I cant figure out what to add.
I was thinking to do this, but it errors. The idea was that when you release the dimmer, it will initiate the script again and go into state == Stop

Code: Select all

		    
		   lightswitch.dimTo(40)
		   lightswitch.dimTo(60).aftersec(2)
		   lightswitch.dimTo(80).aftersec(4)
		   lightswitch.ldimTo(100).aftersec(6)
		   
error message:

Code: Select all

Error: dzVents: Error: (3.0.2) .../scripts/dzVents/generated_scripts/Dinnertabledimmer.lua:18: attempt to call a nil value (field 'aftersec')
The above was actually not the goal. The goal is to find the current/last level and add a percentage to it and repeat that after a few seconds, but I got lost in trying to find out how.

Anyone an idea how to use a timer for dimmer and up it step by step by holding the button longer?
pro2call
Posts: 14
Joined: Monday 31 March 2014 16:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: (52.557965, 5.914462)
Contact:

Re: Changing Dimmer level with Selector switch

Post by pro2call »

Have you managed to get it sorted? im looking for the same
lwalbert
Posts: 1
Joined: Friday 29 January 2021 0:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Changing Dimmer level with Selector switch

Post by lwalbert »

Here is mine solution for using the Ikea button as a dimmer combined with a Ikea panel
It works smooth:)
reccomend to install the Mosquitto-client. Command code looks more clean compared to the curl command.
paste the code in a dzVents script an test it out. yo only need to change the device numbers in top.

Update:
You need to set your API transport IDX in this line:
cmd1st = ("curl --globoff 'http://127.0.0.1:8080/json.htm?idx=504&param
change the number 504 to the number you have in your device list.
Skjermbilde 2021-01-29 kl. 09.25.24.png
Skjermbilde 2021-01-29 kl. 09.25.24.png (75.48 KiB) Viewed 1646 times

Code: Select all

return {
	on = {
		devices = {
		    545 -- Your ikea button (states on, off, up, down, stop) idx number 
		    }
	},
	execute = function(dz, device)
		local button = dz.devices(545) -- Your ikea button (states on, off, up, down, stop) idx number
		local lamp1  = dz.devices(617) -- Your ikea panel or light to be dimmed idx number

-- No changes required below unless you are selecting mosquitto-client instead of domoticz API
-- To use mosquitto-client, you must install it by sudo apt install mosquitto-client

		local unfriendlyName = lamp1.deviceId:sub(1, (#lamp1.deviceId - 6)) -- This removes 6 last characters in device id used in API call
		--print(unfriendlyName) -- Used for troubbleshooting
		
		--domoticz API call 
        cmd1st = ("curl --globoff 'http://127.0.0.1:8080/json.htm?idx=504&param=udevice&svalue={%22type%22:%22request%22,%22requestId%22:8,%22command%22:%22device_set%22,%22params%22:{%22topic%22:%22"..unfriendlyName)
        cmd2nd = ("/set%22,%22state%22:{%22brightness_move%22:")
        cmd3rd = ("}}}&type=command'")
        
        --If installed (sudo apt install mosquitto-client) then uncomment cmdtst1 & cmdtst2 and the os.execute for corresponding command below.
        -- Remember to comment domoticz API lines and commands if you will use Mosquitto-client.
        
        -- Mosquitto-client.
        --cmdtst1 = ("mosquitto_pub -h localhost -t zigbee2mqtt/")
        --cmdtst2 = ("/set/brightness_move -m ")

		if button.state == "On" then --and lamp1.state == "Off" then
		    lamp1.switchOn()
		    --lamp1.setKelvin(80) set a fixed Kelvin
		elseif button.state == "Off" and lamp1.state == "On" then
		    lamp1.switchOff()
		elseif button.state == "Up" then
            Controlvalue = 40 -- increments pr. sec
            --os.execute(cmdtst1 ..unfriendlyName ..cmdtst2 ..Controlvalue) -- command for mosquitto-client
            os.execute(cmd1st ..cmd2nd ..Controlvalue ..cmd3rd) -- command for domoticz API
		elseif button.state == "Down" then
            Controlvalue = -40 -- decrements pr. sec
            --os.execute(cmdtst1 ..unfriendlyName ..cmdtst2 ..Controlvalue) -- command for mosquitto-client
            os.execute(cmd1st ..cmd2nd ..Controlvalue ..cmd3rd) -- command for domoticz API
		elseif button.state == "Stop" then
            Controlvalue = 0 -- Stop inc/dec
            --os.execute(cmdtst1 ..unfriendlyName ..cmdtst2 ..Controlvalue) -- command for mosquitto-client
            os.execute(cmd1st ..cmd2nd ..Controlvalue ..cmd3rd) -- command for domoticz API
		end

	end
}
br
Lennart
James83
Posts: 16
Joined: Thursday 28 November 2019 0:35
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: Changing Dimmer level with Selector switch

Post by James83 »

Hi,

Found your topic when i was looking for the same solution.
Meanwhile i found a way to also use the "up" and "down" function of the remote. It was actually very simple.
This is how i did it.
It does create some queued events but only for a short time and cancels the queud events when the button is released.
When controlling more than 1 light, best to increase the "levelchange" and "interval" to prevent having to much traffic on your zigbee network.

Code: Select all

	    if item.state == "On" then
                if dz.devices('Bureaulamp').state == 'Off' then dz.devices('Bureaulamp').dimTo(1) end
            elseif item.state == "Up" then
                local currentlevel = dz.devices('Bureaulamp').level
                local levelchange = 1 -- percentage per step
                local startdimtime = 0.1 -- delay until dimming starts
                local interval = 0.1 -- time per step
                for i = currentlevel,100 do
                dz.devices('Bureaulamp').dimTo(currentlevel).afterSec(startdimtime)
                dz.log("Setting light to "..currentlevel.."% - afterSec "..interval)
                currentlevel = currentlevel + levelchange
                startdimtime = startdimtime + interval
                end
            elseif item.state == "Down" then
                local currentlevel = dz.devices('Bureaulamp').level
                local levelchange = 1 -- percentage per step
                local startdimtime = 0.1 -- delay until dimming starts
                local interval = 0.1 -- time per step
                for i = currentlevel,100 do
                dz.devices('Bureaulamp').dimTo(currentlevel).afterSec(startdimtime)
                dz.log("Setting light to "..currentlevel.."% - afterSec "..interval)
                currentlevel = currentlevel - levelchange
                startdimtime = startdimtime + interval
                end
            elseif item.state == "Stop" then
                dz.devices('Bureaulamp').cancelQueuedCommands()
            elseif item.state == "Off" then
                dz.devices('Bureaulamp').switchOff().checkFirst()
            end
            
Mvg,
Jimmy
Last edited by James83 on Sunday 28 November 2021 16:30, edited 2 times in total.
RPI4, Philips Hue, Tradfri, Sonoff, MQTT, Zigbee2Mqtt, RFXCom 433, Octoprint,...
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Changing Dimmer level with Selector switch

Post by EddyG »

Doesn't that create a lot of "traffic"?
Can Domoticz keep up with that?
James83
Posts: 16
Joined: Thursday 28 November 2019 0:35
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: Changing Dimmer level with Selector switch

Post by James83 »

EddyG wrote: Sunday 28 November 2021 14:33 Doesn't that create a lot of "traffic"?
No, the "Up" statement of the switch is only once send to DZ over MQTT, as well as the "Stop" statement.
The light itself is controlled by the Hue brigde. So it's only like you would change the level of the light by the slider in the GUI. For every percentage there's a command being send to the bridge.
However, i have no lights connected to z2m but i don't think it will be a problem.
Can Domoticz keep up with that?
Easely. It's only 1 command (dimTo) but for multiple times and only lasts a few seconds.
I also have a dimmer sequence combined with the Hue Dimmer Switch. Lights in a room dim over a given time (3min in my case) and by a given percentage each step. Works perfect. No noticable lags in the gui or execution of other scripts.

Jimmy
RPI4, Philips Hue, Tradfri, Sonoff, MQTT, Zigbee2Mqtt, RFXCom 433, Octoprint,...
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Changing Dimmer level with Selector switch

Post by EddyG »

James83 wrote: Sunday 28 November 2021 15:09 The light itself is controlled by the Hue brigde. So it's only like you would change the level of the light by the slider in the GUI. For every percentage there's a command being send to the bridge.
However, i have no lights connected to z2m but i don't think it will be a problem.
That's what I meant. From 0 to 100% in 10 seconds 100 messages are send over the Zigbee network.
I think it will clog my Zigbee network.
James83
Posts: 16
Joined: Thursday 28 November 2019 0:35
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: Changing Dimmer level with Selector switch

Post by James83 »

EddyG wrote: Sunday 28 November 2021 15:17 That's what I meant. From 0 to 100% in 10 seconds 100 messages are send over the Zigbee network.
I think it will clog my Zigbee network.
I doubt it. In the original case (where the dimmer switch dims lights through the Tradfri bridge) the same happens without a problem.
You could change the percentage of each step to decrease the amounts of commands being send. 2% instead of 1% would only need 50 commands from 0 to 100, 3% only 33,...
RPI4, Philips Hue, Tradfri, Sonoff, MQTT, Zigbee2Mqtt, RFXCom 433, Octoprint,...
James83
Posts: 16
Joined: Thursday 28 November 2019 0:35
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: Changing Dimmer level with Selector switch

Post by James83 »

I did some quick testing on 6 Tradfri spots.
I found that using the room switch (created by the Hue bridge/app) does not work well because of the delay between the individual lights and the room switch. So best to use the individual lights in your script.
Also found a flaw in my script, corrected in original post.

I also changed the levelchange = 2 and interval = 0.5 to give the zigbee network the time to adjust and decrease the amount of commands.
That seems to work well.
RPI4, Philips Hue, Tradfri, Sonoff, MQTT, Zigbee2Mqtt, RFXCom 433, Octoprint,...
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest