Page 1 of 1

dzVents check if it is past a certain time in if statement

Posted: Wednesday 25 November 2020 20:38
by IceBlackz
Hiya all,

I'm working out a system to automate the lights in my kitchen. I think I've managed to create a timer that triggers at 30 minutes before sunset AND at 17:30 AND at 20:00, but now I want to check which of the above statements triggered my script. I checked the wiki but there are so many options and possibilities that I can't find what I need. See also below example:

Code: Select all

return {
	on = {
		timer = {
			'30 minutes before sunset',
			'at 17:30',
			'at 20:00'
		}
	},
	execute = function(domoticz, timer)
		domoticz.log('Timer event was triggered by ' .. timer.trigger, domoticz.LOG_INFO .. ' at time ' .. domoticz.time)
		local keuken_ledstrip_koof = domoticz.devices("Keuken_ledstrip_koof")
		if domticz.time >= "20:00" then
		    keuken_ledstrip_koof.cancelQueuedCommands()
		    keuken_ledstrip_koof.dimTo(10)
		elseif keuken_ledstrip_koof.level == 0 then
		    keuken_ledstrip_koof.dimTo(15)
		end
	end
}
The same goes for the if statement for my motion sensor, that I want to have a reduced increase after a certain time:

Code: Select all

		    if level_prev > 0 then
		        keuken_ledstrip_koof.cancelQueuedCommands()
		        if domoticz.time >= "20:00" then
		            keuken_ledstrip_koof.dimTo(level_prev+20)
		        else
		            keuken_ledstrip_koof.dimTo(level_prev+30)
		        end
		        keuken_ledstrip_koof.dimTo(level_prev).afterSec(30)
		    end
it's hard to experiment with this because, well, it's only once during a day the script will trigger ;p Hopefully you guys can help me!

Kind regards,

Falco

Re: dzVents check if it is past a certain time in if statement

Posted: Wednesday 25 November 2020 21:05
by waaren
IceBlackz wrote: Wednesday 25 November 2020 20:38 I'm working out a system to automate the lights in my kitchen. I think I've managed to create a timer that triggers at 30 minutes before sunset AND at 17:30 AND at 20:00, but now I want to check which of the above statements triggered my script.
You can use dz.time.matchesRule for that. See below.

Code: Select all

return
{
	on =
	{
		timer =
		{
			'at 17:30',
			'at 20:00',
			'30 minutes before sunset',
		},
	},

	logging =
	{
		level = domoticz.LOG_DEBUG, -- set to domoticz.LOG_ERROR when all ok
		marker = 'keuken',
	},

	execute = function(dz, item)
		local keuken_ledstrip_koof = dz.devices("Keuken_ledstrip_koof")

		dz.log('Timer event was triggered by ' .. item.trigger .. ' at time ' .. dz.time.rawTime, dz.LOG_DEBUG)
		dz.log('State of keuken_ledstrip_koof is ' ..keuken_ledstrip_koof.state , dz.LOG_DEBUG)

		if dz.time.matchesRule('at 20:00-23:59') then
			keuken_ledstrip_koof.cancelQueuedCommands() -- cancelQueuedCommands is only needed when a previous afterSec() is stil in the queue
			keuken_ledstrip_koof.dimTo(10)
		elseif keuken_ledstrip_koof.level == 0 or keuken_ledstrip_koof.state ~= 'On' then
			keuken_ledstrip_koof.dimTo(15)
		end
	end
}
The same goes for the if statement for my motion sensor, that I want to have a reduced increase after a certain time:

Code: Select all

[code]
	if keuken_ledstrip_koof.level > 0 then
		keuken_ledstrip_koof.cancelQueuedCommands()
		if dz.time.matchesRule('at 20:00-23:59') then
			keuken_ledstrip_koof.dimTo( math.min( keuken_ledstrip_koof.level  + 20, 100 ) )
		else
			keuken_ledstrip_koof.dimTo( math.min( keuken_ledstrip_koof.level  + 30, 100 ) )
		end
		keuken_ledstrip_koof.dimTo(keuken_ledstrip_koof.level).afterSec(30)
	end

Re: dzVents check if it is past a certain time in if statement

Posted: Wednesday 25 November 2020 21:06
by IceBlackz
Thank you very much! Also for the improvements on the level/dimTo input :)