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

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

Moderator: leecollings

Post Reply
schwuppdiwupp
Posts: 13
Joined: Wednesday 25 March 2020 19:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Dimming with a button (long press event)

Post 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!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Dimming with a button (long press event)

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
schwuppdiwupp
Posts: 13
Joined: Wednesday 25 March 2020 19:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Dimming with a button (long press event)

Post 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?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
schwuppdiwupp
Posts: 13
Joined: Wednesday 25 March 2020 19:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Dimming with a button (long press event)

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Dimming with a button (long press event)

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

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