Page 1 of 1
Variable time before sunset
Posted: Friday 26 October 2018 21:02
by niwreg
I would like to do some light switching on sunset. However i would like to change the minutes before sunset to a variable.
I guess i have to do something with function(domoticz) but i'm not sure how i combine that with the 'xx before sunset'
Could somebody point me in the right direction? I saw something like this:
active = function(domoticz) -- This function will be evaluated every minute by the dzVents system
myLight = domoticz.devices(myLightIDX)
return myLight.state == "On"
end,
but this is for active. But how do i check the xx before sunset?
Kan i do something like:
on = function (domoticz)
check variable
but how do i check the sunset time?
Re: Variable time before sunset
Posted: Monday 29 October 2018 22:58
by BOverdevest
You could you use 2 scripts, connected by a persistent variable that holds the 'random number of minutes'
The first would run at 3:00 and generate a random number and store this in the persistent data
(i have not seen a random function in Dzvents) so maybe use a asynchronous http call. See dzvents documentation.
you could use this call:
https://www.random.org/integers/?num=1& ... in&rnd=new
Then when the random number is stored during the night the 2nd script would use the persistent global variable "RandomMinutes" in its timer section
return {
on = {
timer = { Random .. " minutes before sunset'' },
hope this helps or inspires a different solution...
Re: Variable time before sunset
Posted: Tuesday 30 October 2018 20:46
by Maxx
Hello,
This works for me:
Code: Select all
return {
active = true,
on = {
['timer'] = {
'every minute'
},
},
execute = function(domoticz, dummy)
-- get current time
timenow = os.date("*t")
minutesnow = timenow.min + timenow.hour * 60
if (minutesnow >= timeofday['SunsetInMinutes'] + 30) then --(Make 30 a variable)
light.switchOn()
end
end
}
Re: Variable time before sunset [Solved]
Posted: Tuesday 30 October 2018 22:09
by waaren
niwreg wrote: Friday 26 October 2018 21:02
I would like to do some light switching on sunset. However i would like to change the minutes before sunset to a variable.
I guess i have to do something with function(domoticz) but i'm not sure how i combine that with the 'xx before sunset'
Could somebody point me in the right direction? I saw something like this:
active = function(domoticz) -- This function will be evaluated every minute by the dzVents system
myLight = domoticz.devices(myLightIDX)
return myLight.state == "On"
end,
but this is for active. But how do i check the xx before sunset?
Kan i do something like:
on = function (domoticz)
check variable
but how do i check the sunset time?
The first code below does what you describe. But do you want it ?
The active = section will be called on every device update and every minute. I've put a print in so you can see how often that is on your system.
On my system it is called 4 times /second on average
Code: Select all
local minutesBeforeSunset = 20 -- or any other number
return {
active = function(domoticz)
-- Just to give you an idea how many times this function is called
print("in active = section with value " .. minutesBeforeSunset)
-- using dzVents variable
return domoticz.time.matchesRule(minutesBeforeSunset .. " minutes before sunset" )
-- or using a domoticz variable
-- return domoticz.time.matchesRule(domoticz.variables("fancy domoticz variable name").value .. " minutes before sunset")
end,
on = { timer = { 'every minute' } },
execute = function(dz)
dz.log("I remember i should do something... ")
end
}
My personal opinion is that the script below is better suited for your requirement.
Code: Select all
return {
on = { timer = { "every minute" } },
logging = { level = domoticz.LOG_ERROR,
marker = "beforeSunset"},
execute = function(dz)
local minutesBeforeSunset = 20 -- or any other number
if dz.time.matchesRule(minutesBeforeSunset .. " minutes before sunset" ) then
dz.log("I remember I should do something... ",dz.LOG_FORCE)
else
dz.log("nothing to be done now ",dz.LOG_FORCE)
end
end
}
Re: Variable time before sunset
Posted: Wednesday 15 January 2020 14:39
by Ballo50
Kind of an old topic, but maybe useful for someone using DzVents 2.4 or newer:
What also might be a possible solution is to run a script let say 30 minutes for example that turns on the switch with a random interval of 15 minutes
So you get something like:
Code: Select all
return
{
active = true,
on =
{
timer =
{
'30 minutes before sunset',
},
},
execute = function(dz)
dz.devices('switch1').switchOn().withinMin(15)
end
}
This will cause the script to run 30 minutes before sunset and will switch on the switch random within 15 minutes, so effectively between 30 and 15 minutes before sunset.
Re: Variable time before sunset
Posted: Wednesday 15 January 2020 22:25
by HvdW
Maybe you want a lot of control, but isn't the randomness checkbox intended for this?