Page 1 of 1

Dimming with a button (long press event)

Posted: Wednesday 02 September 2020 19:23
by schwuppdiwupp
Hi!
I'm trying to write a script to dim my light using an aqara opple switch.

Code: Select all

return {
	on = {
		devices = {
			'Arbeitszimmer Flur',
			'Arbeitszimmer Wohnzimmer'
		}
	},
	execute = function(domoticz, device)
	    local Wand = domoticz.devices('Arbeitszimmer - Wandbeleuchtung')
	    local Decke = domoticz.devices('Arbeitszimmer - Decke')
	    local dimLevelWand = 1
	    local dimLevelDecke = 0
	    
		domoticz.log('Device ' .. device.name .. ' was changed. State: ' .. Wand.state, domoticz.LOG_INFO)
		
		
		if device.state == "B1" then
		  Wand.toggleSwitch()
		end
		
		if device.state == "B3" then
		    if Decke.state == 'Off' then
                    Decke.setState('On')
                else
                    Decke.setState('Off')
                end
		end
		
		if device.state == "B1L" then
		    repeat
		        dimLevelWand = dimLevelWand + 0.01
                --domoticz.log('Set ' .. Wand.name .. ' to dimLevel '.. dimLevelWand, domoticz.LOG_INFO)
		        Wand.dimTo(dimLevelWand)
	        until dimLevelWand >= 100
        elseif device.state == "B1RL" then
            
        end
        
        if device.state == "B2L" then
		    repeat
		        dimLevelWand = dimLevelWand - 0.01
                --domoticz.log('Set ' .. Wand.name .. ' to dimLevel '.. dimLevelWand .. '%, after ' .. delay .. ' seconds', domoticz.LOG_INFO)
		        Wand.dimTo(dimLevelWand)
	        until dimLevelWand <= 0
        elseif device.state == "B2RL" then
            
        end
		
	end
}
I'm having trouble as the script is running too fast and I don't get that slow fade to be able to stop at the desired new level. Usually it's going form 0 to 100 and the same backwards instantly. Is there a way to slow down the script? like a delay() or sleep() without the bad behaviour of sleep or delay?

Any help would be appreciated!

Re: Dimming with a button (long press event)

Posted: Wednesday 02 September 2020 23:18
by waaren
schwuppdiwupp wrote: Wednesday 02 September 2020 19:23 Hi!
I'm trying to write a script to dim my light using an aqara opple switch.

I'm having trouble as the script is running too fast and I don't get that slow fade to be able to stop at the desired new level. Usually it's going form 0 to 100 and the same backwards instantly. Is there a way to slow down the script? like a delay() or sleep() without the bad behaviour of sleep or delay?

Any help would be appreciated!
A solution could look like

Code: Select all

return {
    on = {
        devices = {
            'Arbeitszimmer Flur',
            'Arbeitszimmer Wohnzimmer'
        }
    },

    execute = function(domoticz, device)
        local Wand = domoticz.devices('Arbeitszimmer - Wandbeleuchtung')
        local Decke = domoticz.devices('Arbeitszimmer - Decke')

        domoticz.log('Device ' .. device.name .. ' was changed. State: ' .. Wand.level, domoticz.LOG_INFO)

        Wand.cancelQueuedCommands()

        if device.levelName == "B1" then
          Wand.toggleSwitch()
        elseif device.levelName == "B3" then
            if Decke.state == 'Off' then
                    Decke.setState('On')
                else
                    Decke.setState('Off')
                end
        elseif device.levelName == "B1L" then
            for dimLevel = 1, 100 do
                Wand.dimTo(dimLevel).afterSec(dimLevel / 10)
            end
        elseif device.levelName == "B2L" then
            for dimLevel = 1, 100 do
                Wand.dimTo(100 - dimLevel).afterSec(dimLevel/ 10)
            end
        end

    end
}

Re: Dimming with a button (long press event)  [Solved]

Posted: Thursday 03 September 2020 12:52
by schwuppdiwupp
Thank you very much! I have a little lag but it's working now!

Code: Select all

return {
    on = {
        devices = {
            'Arbeitszimmer Flur',
            'Arbeitszimmer Wohnzimmer'
        }
    },

    data = {
        lastColor = { initial = 0 }
    },
    

    execute = function(domoticz, device)
        local Wand = domoticz.devices('Arbeitszimmer - Wandbeleuchtung')
        local Decke = domoticz.devices('Arbeitszimmer - Decke')
        colors = { 2700, 3500, 5000, 6000, 7000 }

        domoticz.log('Device ' .. device.name .. ' was changed. State: ' .. Wand.level, domoticz.LOG_INFO)

        Wand.cancelQueuedCommands()

        if device.levelName == "B1" then
          Wand.toggleSwitch()
        elseif device.levelName == "B3" then
            if Decke.state == 'Off' then
                    Decke.setState('On')
                else
                    Decke.setState('Off')
                end
        elseif device.levelName == "B1L" then
            for dimLevel = Wand.level, 100 do
                Wand.dimTo(dimLevel).afterSec(dimLevel / 20)
            end
        elseif device.levelName == "B2L" then
            for dimLevel = 1, (Wand.level-1) do
                Wand.dimTo(Wand.level - dimLevel).afterSec(dimLevel / 20)
            end
        elseif device.levelName == "B1D" then
            --TODO setKelvin
            
            Wand.setKelvin(colors[domoticz.data.lastColor + 1])
            domoticz.data.lastColor = domoticz.data.lastColor + 1
        elseif device.levelName == "B2D" then
            --TODO setKelvin
            if domoticz.data.lastColor - 1 < 0 then
                domoticz.data.lastColor = 4
            end
            Wand.setKelvin(colors[domoticz.data.lastColor - 1])
            domoticz.data.lastColor = domoticz.data.lastColor - 1
        end

    end
}
I'm still strugglin with adjusting my white balance. setKelvin doesn't change anything. guess I'll have to go with setColorBrightness.
Is there a way to get the color the light is set to atm?

Re: Dimming with a button (long press event)

Posted: Thursday 03 September 2020 18:01
by waaren
schwuppdiwupp wrote: Thursday 03 September 2020 12:52 I'm still struggling with adjusting my white balance. setKelvin doesn't change anything. guess I'll have to go with setColorBrightness.
Is there a way to get the color the light is set to atm?

Code: Select all

        Wand.colorTable = Wand.getColor() 
        domoticz.utils.dumpTable( Wand.colorTable ) 
Will show the current color attributes of device Wand.
What do you want to do with this color information?

Re: Dimming with a button (long press event)

Posted: Thursday 03 September 2020 21:00
by schwuppdiwupp
2 options, didn't decide, which way to go:
  • same dimming option as for brightness
  • use a list with defined values and jump through the list
Thanks to your help I get the values I'd like but how do I set the colortemp correctly?
I tried some variations but always keep getting errors.

Code: Select all

Wand.setColor(,,,,,,2,255)
Wand.setColor(2,255)
and some other variations
What's the correct way to set temperature?
Thanks for your help

Re: Dimming with a button (long press event)

Posted: Thursday 03 September 2020 22:10
by waaren
schwuppdiwupp wrote: Thursday 03 September 2020 21:00 2 options, didn't decide, which way to go:
  • same dimming option as for brightness
  • use a list with defined values and jump through the list
Thanks to your help I get the values I'd like but how do I set the colortemp correctly?
I tried some variations but always keep getting errors.

Code: Select all

Wand.setColor(,,,,,,2,255)
Wand.setColor(2,255)
and some other variations
What's the correct way to set temperature?
Thanks for your help
If the devicesubtype is RGBWW you can use the setKelvin method (levels 0..100) but because setKelvin is not a standard command, internally it use a JSON API call. Therefore the cancelQueuedCommand() will not work.

Check the dzVents wiki and API / JSON wiki pages