Page 1 of 1

Set the dimlevel according to the time of day

Posted: Sunday 13 October 2019 10:30
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
}

Re: Set the dimlevel according to the time of day

Posted: Sunday 13 October 2019 13:14
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
}


Re: Set the dimlevel according to the time of day

Posted: Sunday 13 October 2019 17:29
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).

Re: Set the dimlevel according to the time of day

Posted: Sunday 13 October 2019 20:31
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?

Re: Set the dimlevel according to the time of day

Posted: Monday 14 October 2019 0:07
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().