Set the dimlevel according to the time of day

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

Moderator: leecollings

Post Reply
martijn1985
Posts: 4
Joined: Wednesday 30 August 2017 17:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Set the dimlevel according to the time of day

Post by martijn1985 »

I'm trying to make a light turn on at the right dimlevel according to the time of day. I have tried a variety of scripts, but I couldn't make it work so that I could adjust the dimlevel after the script had run.

The switch is a fibaro dimmer 2 with one button attached. The problem that I seem to have is that I don't seem to have a way to differentiate between the button press and the light switching as as it is one and the same event.

Below is what I currently have, but at this point the light does not switch on a the right setting and I am not able to alter the dim setting. Could anyone help me out?

Code: Select all

return {
	on = {
		devices = {
			'Slaapkamer Plafond'
		}
	},
	data = {
	    turnedOn = {initial = false}
	    },  
	
	execute = function(domoticz, device)
		local lastDimmerLevel = device.lastLevel
		
		if domoticz.time.isDayTime then
		    dimLvl = 98
		else 
		    dimLvl = 28
		end

		 if (device.active and not turnedOn) then
		    --  de call naar silent() is nodig om te voorkomen dat dit script zichzelf blijft triggeren
		    device.dimTo(dimLvl).silent()
		    turnedOn = true;
		    domoticz.log(device.level)
		 elseif(not device.active) then
		     turnedOn = false
		 end
		
		 -- in alle gevallen na 40 minuten uit
		 device.switchOff().afterMin(40)
		 
	end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Set the dimlevel according to the time of day

Post by waaren »

martijn1985 wrote: Sunday 13 October 2019 10:30 I'm trying to make a light turn on at the right dimlevel according to the time of day. I have tried a variety of scripts, but I couldn't make it work so that I could adjust the dimlevel after the script had run.
Not sure I completely understand your requirement but does this work ?

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'Slaapkamer Plafond'
        }
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Slaapkamer Plafond' ,
    },

    execute = function(domoticz, device)
        if device.state == "set Level" then 
            domoticz.log('Ignore because set manual after switching on',domoticz.LOG_DEBUG)
        elseif device.active then
            device.cancelQueuedCommands()
            local dimLevel = 28
            if domoticz.time.isDayTime then
                dimLevel = 98
            end
            device.dimTo(dimLevel).silent()
            device.switchOff().silent().afterMin(40)
        end
    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
martijn1985
Posts: 4
Joined: Wednesday 30 August 2017 17:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Set the dimlevel according to the time of day

Post by martijn1985 »

waaren wrote: Sunday 13 October 2019 13:14
martijn1985 wrote: Sunday 13 October 2019 10:30 I'm trying to make a light turn on at the right dimlevel according to the time of day. I have tried a variety of scripts, but I couldn't make it work so that I could adjust the dimlevel after the script had run.
Not sure I completely understand your requirement but does this work ?

*code removed for readability*
You do seem to understand what I would like to do, however if I adjust the dimmer with the light on the device.state is not 'set Level'. It is ' On', so the first then-part never runs and after adjusting the dimlevel it returns to its previous state (either 98 or 28).
martijn1985
Posts: 4
Joined: Wednesday 30 August 2017 17:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Set the dimlevel according to the time of day

Post by martijn1985 »

I did some further searching and it seems that device.state is different from device._state (well, obviously...). Your script seems to work if the first if is replaced by

Code: Select all

if string.match(device._state,'Set Level') then 
            domoticz.log('Ignore because set manual after switching on',domoticz.LOG_DEBUG)
        elseif device.active then
        
I do have some questions:
  • what is the difference between device.state and device._state (and where is it documented)?
  • what does device.cancelQueuedCommands() do?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Set the dimlevel according to the time of day

Post by waaren »

martijn1985 wrote: Sunday 13 October 2019 20:31 I do have some questions:
  • what is the difference between device.state and device._state (and where is it documented)?
  • what does device.cancelQueuedCommands() do?
state is a derived attribute based on _state. Most of the times it's the same as _state but if _state == 'Set level xx' then state = 'On'
device._state is a raw value coming from domoticz. In dzVents it is stored in the sValue attribute. All raw values from domoticz are also available in dzVents but not separately documented in the wiki

from the same wiki:
cancelQueuedCommands(): Function. 2.4.0 Cancels queued commands. E.g. you switch on a device after 10 minutes: myDevice.switchOn().afterMin(10). Within those 10 minutes you can cancel that command by calling: myDevice.cancelQueuedCommands().
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: No registered users and 1 guest