Page 1 of 1

How to dim a light

Posted: Saturday 29 June 2019 10:57
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
}

Re: How to dim a light

Posted: Saturday 29 June 2019 11:17
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
}

Re: How to dim a light

Posted: Saturday 29 June 2019 11:35
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

Re: How to dim a light

Posted: Sunday 30 June 2019 11:17
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
}

Re: How to dim a light  [Solved]

Posted: Sunday 30 June 2019 13:17
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
}

Re: How to dim a light

Posted: Sunday 30 June 2019 20:01
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 :)

Re: How to dim a light

Posted: Monday 15 February 2021 19:47
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 ?