Programming question re Timers and reboots, using DayOrNight

I have a couple questions about the way Vera is handling some events and timers, as exhibited in the DayOrNight plugin’s implementation file.

Let’s assume that sunset is 7pm and sunrise is 6am and that I have a sunset offset of 30minutes, so that my sunset is at 6:30pm.

Here’s the scenario:

06:00 - sunrise arrives, and DayOrNight (DN) sets itself a timer for 18:30 based on offset.
13:00 - Vera reboots for some reason
13:00:20 - DN inits and sets itself a timer for 18:30
→ this 20 second delay, I assume, is to allow Vera to settle down after a reboot, and have a valid value for is_night()
18:30 - timer fires as scheduled, DN switches to Night time, scenes/PLEG that use DN as trigger get fired
18:40 - Vera reboots for some reason
18:40 - DN indicates Night time because that is what what is in the config file prior to reboot
→ is that a correct assumption?
18:40:20 - DN inits and sets itself to Day, because is_night says day (it’s not 7pm yet)
→ ZWave-enabled rooster now crows because daytime has arrived
18:40:20 - DN sets itself a timer for 18:30 (which will be tomorrow!)
19:00 - (real sunset occurs)
19:05 - scripts that use is_night know that it is night, but those that ask DN get Day instead
→ true?
all night long - DN indicates Day, since it never had a chance to adjust
06:30 - how would DN know that it is daytime? It never got the chance to set itself a timer for daytime? The next timer event would be 18:30. Therefore, it’s now correctly (still) indicating Day, but the rooster doesn’t get told to crow.

→ when you ask Vera to call your function with luup.call_timer(), does that persist through a reboot? In other words, Vera must be using some kind of internal database to schedule this call? Because I know that if a scene has delayed actions and Vera reboots prior to that timer, those delayed actions never fire. So can we rely on Vera’s call_timer, or should we keep track of our own scheduling and check ourselves every N seconds/minutes?

My questions here are programming related, using DN as an example only, this isn’t really a discussion/concern about the DN plugin.

Thanks for your help, all!

You are right … the DayOrNight plugin might now operate properly if it reboots during that time interval between actual sunrise or sunset and the corresponding day or night delta offset that you have specified.

In that case it will behave as if it the specified delta interval was zero … but will work properly for the next transition.

This could be fixed by recording the time of the next transition in a persistent state variable attached to the plugin. We could then use that value to determine the startup behaviour and reset the timer.
[hr]
You are correct that ALL timers are lost when Vera is restarted.
Since Scenes use timers to implement their delayed schedules, that’s why its dangerous to use these Delayed Scene Schedules if it’s critical for the delayed events to ALWAYS be executed.

Say you turn on a sprinkler once a week for 30 minutes. And you use a single Scene with delayed schedule to implement this. If Vera restarts after the sprinkler starts … you could be watering the grass for a whole week before it is turned off!

That’s why the plugins like the Countdown Timer, PLEG, and PLTS, save information about timers in a persistent manner … and re-establish them when Vera Restarts.