Simulate Astrobinding Events in OH 2

I’ve posted how I do this on this thread.

I’m still on OH 1 so the main thing that drives the change in Time of Day are the events (bummer the went away), but when OH has restarted or the rules file is reloaded I don’t know how long OH was down so need to redetermine the current time of day in a System started rule.

The way I handle the situation for Night is to test everything else first and if it isn’t any of those times it must be Night by default. This lets you sidestep the problem where a time period stretches over midnight by forming your conditions such that you never have to test for it directly. Instead it is a process of elimination.

Also, I advise using a String Item to store the current time of day as opposed to a bunch of Switches. It greatly simplifies the rules and your items and it makes it easier to add or remove named time periods. I too started out with separate switches but when, upon the advice of @watou, I switched to using a String everything became much much simpler and more flexible.

One note about the code linked, there is a minor known bug in it when sunrise occurs before 6 am which I’m currently just living with right now but will correct someday. Ultimately I need to skip over the Morning state in the summer when sunrise is before 6.

Another alternative is to split the test into two tests. Astro recalculates its times at midnight. So you can test to see if it is between Night_start and midnight or if now is between midnight and Morning_start. If it is after midnight, Morning_start will have been recalculated for the current day.

If you use this approach, I recommend you set up your cron trigger to trigger on second 30 or 45 instead of 0 to give Astro time to recalculate the new times before your rule executes immediately after midnight.