How to dim a light  [Solved]

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

Moderator: leecollings

Post Reply
User avatar
Varazir
Posts: 378
Joined: Friday 20 February 2015 22:23
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

How to dim a light

Post by Varazir »

Hello,

I created this test script to dim a light bulb, but it loops to quick.

IKEA Switch Brightness Down is a selector switch device so it has 4 states, Off, Click, Hold, Release

Code: Select all

return {
	on = {
		devices = {
			'IKEA Switch Brightness Down'
		}
	},
	execute = function(domoticz, device)
		local dimDevice = domoticz.devices(73)
		local dimLevel = 100
    	if ( device.state == "Hold" )
    	    then
    	        repeat
    	            dimLevel = dimLevel - 5
    		        domoticz.log('Device ' .. dimLevel .. ' %', domoticz.LOG_INFO)
    		        dimDevice.dimTo(dimLevel)
    		    until ( device.state == "Release" or dimLevel < 5 )
    	end
    	
    	if ( device.state == "Release" )
    	    then
    	        if (dimLevel == 0 )
    	            then
    	                dimDevice.switchOff()
    	        end
    	        
    	        domoticz.log('Device ' .. device.name .. ' Release', domoticz.LOG_INFO)
    	end
		
	end
}
Raspberry PI 2 with RaZberry Controller 2016 ZWave+ and CC2531(zigbee)
Several IKEA devices/z-wave devices
poudenes
Posts: 667
Joined: Wednesday 08 March 2017 9:42
Target OS: Linux
Domoticz version: 3.8993
Location: Amsterdam
Contact:

Re: How to dim a light

Post by poudenes »

You can't use this line?:

Code: Select all

domoticz.devices(73).dimTo(50)
Will dim device 73 to 50%

Code: Select all

return {
	on = {
		devices = {
			'IKEA Switch Brightness Down'
		}
	},
	execute = function(domoticz, device)

    	if ( device.state == "Hold" ) then
    	     	 domoticz.devices(73).dimTo(5)
    	elseif ( device.state == "Release" ) then
    	     	 domoticz.devices(73).switchOff()
    	 end
    	 domoticz.log('Device ' .. device.name .. ' Release', domoticz.LOG_INFO)
    end		
end
}
RPi3 B+, Debain Stretch, Domoticz, Homebridge, Dashticz, RFLink, Milight, Z-Wave, Fibaro, Nanoleaf, Nest, Harmony Hub, Now try to understand pass2php
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to dim a light

Post by waaren »

Varazir wrote: Saturday 29 June 2019 10:57 I created this test script to dim a light bulb, but it loops to quick.
As dzVents does not change the devices by itself but hands over that action to domoticz after the script has finished you cannot use the new state of a device inside a dzVents script that changed that state. The state simply had not changed yet. Same is true for classic domoticz Lua and Blockly
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
Varazir
Posts: 378
Joined: Friday 20 February 2015 22:23
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: How to dim a light

Post by Varazir »

waaren wrote: Saturday 29 June 2019 11:35
Varazir wrote: Saturday 29 June 2019 10:57 I created this test script to dim a light bulb, but it loops to quick.
As dzVents does not change the devices by itself but hands over that action to domoticz after the script has finished you cannot use the new state of a device inside a dzVents script that changed that state. The state simply had not changed yet. Same is true for classic domoticz Lua and Blockly
Okay, I have goten this far now and the diming works but I cant get it to stop

Code: Select all

return {
	    on =        {
		            devices          = { 'IKEA Switch Brightness Down' },
		            },

        logging =   {   
                    level            =  domoticz.LOG_DEBUG,   
                    marker           =  "IKEADown" 
                    },    

        data    =   {   
                    buttonState      = { initial               },
                    },
                
	execute = function(domoticz, device)
    
    function sleep(s)
        local ntime = os.clock() + s
        repeat until os.clock() > ntime
    end
    
		local dimDevice = domoticz.devices(73)
		local dimLevel = 100
    	if ( device.state == "Hold" )
    	    then
    	        repeat
    	            dimLevel = dimLevel - 5
    		        domoticz.log('Device ' .. dimLevel .. ' %', domoticz.LOG_INFO)
    		        domoticz.log('Hold Button State ' .. domoticz.data.buttonState .. ' ', domoticz.LOG_INFO)
    		        dimDevice.dimTo(dimLevel)
    		        sleep(1)
    		    until ( domoticz.data.buttonState == "Release" or dimLevel < 5 )
    	end
    	
    	if ( device.state == "Release" )
    	    then
    	        domoticz.data.buttonState = "Release"
    	        if (dimLevel == 0 )
    	            then
    	                dimDevice.switchOff()
    	        end
    	        domoticz.log('Release Button State ' .. domoticz.data.buttonState .. ' ', domoticz.LOG_INFO)
    	        domoticz.log('Device ' .. device.name .. ' Release', domoticz.LOG_INFO)
    	end
		
	end
}
Raspberry PI 2 with RaZberry Controller 2016 ZWave+ and CC2531(zigbee)
Several IKEA devices/z-wave devices
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to dim a light  [Solved]

Post by waaren »

Varazir wrote: Sunday 30 June 2019 11:17 Okay, I have goten this far now and the diming works but I cant get it to stop
Please have a look at this.

Code: Select all

return
{
    on =
    {
        devices = { 'IKEA Switch Brightness Down' },
    },

    logging =
    {
        level = domoticz.LOG_INFO,
        marker = "IKEADown"
    },

    execute = function(domoticz, device)

        local dimDevice = domoticz.devices(73)
        local dimLevel = 100
        local delay = 0

        if device.state == "Hold"  then
            repeat
                delay = delay + 0.15
                dimLevel = dimLevel - 1
                domoticz.log('Set ' .. dimDevice.name .. ' to dimLevel '.. dimLevel .. '%, after ' .. delay .. ' seconds', domoticz.LOG_INFO)
                dimDevice.dimTo(dimLevel).afterSec(delay)
            until dimLevel <= 0
        elseif device.state == "Release" then
            domoticz.log('Stop dimming of ' .. dimDevice.name, domoticz.LOG_INFO)
            dimDevice.cancelQueuedCommands()
            if dimDevice.level == 0 then
                dimDevice.switchOff()
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
Varazir
Posts: 378
Joined: Friday 20 February 2015 22:23
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: How to dim a light

Post by Varazir »

waaren wrote: Sunday 30 June 2019 13:17
Varazir wrote: Sunday 30 June 2019 11:17 Okay, I have goten this far now and the diming works but I cant get it to stop
Please have a look at this.

Code: Select all

return
{
    on =
    {
        devices = { 'IKEA Switch Brightness Down' },
    },

    logging =
    {
        level = domoticz.LOG_INFO,
        marker = "IKEADown"
    },

    execute = function(domoticz, device)

        local dimDevice = domoticz.devices(73)
        local dimLevel = 100
        local delay = 0

        if device.state == "Hold"  then
            repeat
                delay = delay + 0.15
                dimLevel = dimLevel - 1
                domoticz.log('Set ' .. dimDevice.name .. ' to dimLevel '.. dimLevel .. '%, after ' .. delay .. ' seconds', domoticz.LOG_INFO)
                dimDevice.dimTo(dimLevel).afterSec(delay)
            until dimLevel <= 0
        elseif device.state == "Release" then
            domoticz.log('Stop dimming of ' .. dimDevice.name, domoticz.LOG_INFO)
            dimDevice.cancelQueuedCommands()
            if dimDevice.level == 0 then
                dimDevice.switchOff()
            end
        end
    end
}
Thanks, it worked.

Then to the next step to make it dynamic.
But that is another thread :)
Raspberry PI 2 with RaZberry Controller 2016 ZWave+ and CC2531(zigbee)
Several IKEA devices/z-wave devices
Diplo95
Posts: 15
Joined: Friday 26 July 2019 10:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Melun France
Contact:

Re: How to dim a light

Post by Diplo95 »

Hello,

I join the thread because I try to find a way to dim a light with my Xiaomi Round 1 button switch.

Just to be sure, this script dim the light once the button is released ?
Varazir : have you found a way to dim the light when pushing the switch ?
Domoticz v2021.1 on Raspberry Pi 3B+
Raspbian Buster
ConBee II + Xiaomi devices
Zwave
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest