Thank you for the advice and the clear explanation of why my original design was going to run into problems owing to sunrise being calculated after midnight by the Astro binding. It is much clearer now.
So I re-designed my logic accordingly and borrowed pretty heavily from your design pattern for Time of Day.
The rule was intended to calculate the correct time of day after OH had been offline during which time an event may have ocurred, so the system would need to recalculate it on startup.
So based on what I stole from your design pattern, it seems to be working so far.
I just have a question about the case now.isAfter(bed_start) which does not have an && condition. Is this because of the ‘midnight’ phenomenon and a second condition is not needed?
// Convert the Astro Items to Joda DateTime
val day_start = new DateTime(Sunrise_Time.state.toString)
val night_start = new DateTime(Sunset_Time.state.toString)
var curr = 'Unknown'
switch now {
case now.isAfter(morning_start) && now.isBefore(day_start): curr = 'Morning'
case now.isAfter(day_start) && now.isBefore(evening_start): curr = 'Day'
case now.isAfter(evening_start) && now.isBefore(night_start): curr = 'Evening'
case now.isAfter(night_start) && now.isBefore(bed_start): curr = 'Night'
case now.isAfter(bed_start): curr = 'Bed Time'
case now.isAfter(after_Midnight_start) && now.isBefore(morning_start): curr = 'After Midnight'
}
Do you think I would need the extra condition as follows? (I’ve tried to understand it per the example but I’m not sure I’ve got it right.
switch now {
case now.isAfter(morning_start) && now.isBefore(day_start): curr = 'Morning'
case now.isAfter(day_start) && now.isBefore(evening_start): curr = 'Day'
case now.isAfter(evening_start) && now.isBefore(night_start): curr = 'Evening'
case now.isAfter(night_start) && now.isBefore(bed_start): curr = 'Night'
case now.isAfter(bed_start) && now.isBefore(midnight_start): curr = 'Bed Time'
case now.isAfter(after_Midnight_start) && now.isBefore(morning_start): curr = 'After Midnight'
}
So far it worked for Night Time and for Bed Time, as it is 22:40. I won’t last until midnight so I’ll check the logs in the AM to see if the midnight condition worked
Thanks again for your feedback.