Recover from missed sunriseTime and sunsetTime events

The recommended way to handle sunrise/sunset events with an offset is to listen to the sunriseTime and sunsetTime events, then use the time provided in these apps to calculate the desired time and schedule the event. That works great as long as the hub is always online.

Even if the app wasn't installed / changes settings after the events occur, getLocationEventsSince() can be used to retrieve the most recent sunrise and sunset times published and update schedules.

When the hub boots, it calculates sunrise and sunset times; you can see this as one of the status lines shown during boot. However, it appears that sunriseTime and sunsetTime events don't fire on boot if the hub was offline at the time they would normally have fired.

I'm currently working around that by checking location.sunrise and location.sunset; since those might be in the past, if I can't find the time I need, I'm scheduling a task for a few seconds after midnight to try again with the location properties. But that feels like needless gyration.

How difficult would it be to fire those events at system startup if the most recent instance of the event was missed?

This is not how the built-in apps do it (but for different reasons). Instead, they run a job just after midnight to grab today's sunrise and sunset times with whatever offsets are needed, then schedule those jobs accordingly. So, suppose the hub is down at midnight (your problem, just different incarnation). It is simple to subscribe to systemStart event with that midnight handler as the method. The midnight handler doesn't care how many times it is called, it only ends up with the specific jobs needed for today.

midnightHandler is scheduled for random number of seconds after midnight:

schedule("${(Math.random()*56).toInteger()+2} 0 0 * * ? *", midnightHandler)

An example of how sunset is scheduled when midnightHandler runs:

sunOff = getSunriseAndSunset(sunsetOffset: atSunsetOffset).sunset
if(now() < sunOff.time) runOnce(sunOff, someHandler)

That's good info. It's probably more accurate to say that sunriseTime and sunsetTime are what was recommended in SmartThings docs, and I've seen that referenced on the forum before. I don't see any specific recommendations in Hubitat docs.

I'll probably shift the code around so my wait-for-midnight fallback is the main code path instead and get rid of the event subscription. Nonetheless, while the events exist, it'd be nice if they recovered themselves after an outage.

What do you mean? Events happen at a specific time, so what is it you want recovered? And from what outage?

This started because a user had an extended power outage where the hub was offline. Because sunsetTime didn't fire, the following day's scheduled task wasn't scheduled, so my app failed on the subsequent day.

These events announce the time at which a future event will occur; it would be nice if they announced the time of the next sunrise/sunset during hub startup so apps listening to that announcement can still react to them and schedule appropriately.

However, I suppose that assumes that apps aren't using these to identify that it is sunrise or sunset right now, and I can't guarantee that. (In ST, there was a separate location.sunset and location.sunrise event that was for acting at that time.)

Same here.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.