Variable time before sunset  [Solved]

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

Moderator: leecollings

Post Reply
niwreg
Posts: 25
Joined: Thursday 19 February 2015 11:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Variable time before sunset

Post 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?
BOverdevest
Posts: 23
Joined: Wednesday 28 March 2018 20:56
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11307
Location: Nederland
Contact:

Re: Variable time before sunset

Post 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...
HUE, Tradfri, Zwave, RFXCOM, rooted TOON, PS4
Maxx
Posts: 58
Joined: Saturday 27 January 2018 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Variable time before sunset

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

Re: Variable time before sunset  [Solved]

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Ballo50
Posts: 3
Joined: Tuesday 21 November 2017 9:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Variable time before sunset

Post 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.
HvdW
Posts: 594
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Variable time before sunset

Post by HvdW »

Maybe you want a lot of control, but isn't the randomness checkbox intended for this?
Bugs bug me.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest