So don’t use a variable and use a persisted Item instead. This lets you centralize your time of day calculations and use the value throughout your rules files and in your sitemap.
Create a System started rule to recalculate the current time period.
Also, instead of a boolean I highly recommend using a String Item and use the name of the time period to indicate what time period you are in. It is much easier to expand and adjust without impacting the rules that care about time of day.
#Items
String TimeOfDay
String PreviousTimeOfDay
DateTime Date "Date and Time: [%1$tm/%1$td/%1$tY %1$tT]" <calendar> (Weather) { ntp="America/Denver:en_EN" }
Switch Twilight_Event (Weather) { astro="planet=sun, type=set, property=start, offset=-90" }
DateTime Twilight_Time "Twilight [%1$tr]" <moon> (Weather) { astro="planet=sun, type=set, property=start, offset=-90" }
Switch Sunset_Event (Weather) { astro="planet=sun, type=set, property=start" }
DateTime Sunset_Time "Sunset [%1$tr]" <moon> (Weather) { astro="planet=sun, type=set, property=start" }
Switch Sunrise_Event (Weather) { astro="planet=sun, type=rise, property=start" }
DateTime Sunrise_Time "Sunrise [%1$tr]" <sun> (Weather) { astro="planet=sun, type=rise, property=start" }
#Rules
import org.openhab.core.library.types.*
import org.joda.time.*
import org.eclipse.xtext.xbase.lib.*
val Functions$Function3 updateTimeOfDay = [String tod, String ptod, boolean update |
logInfo("Weather", "Setting PreviousTimeOfDay to \"" + ptod + "\" and TimeOfDay to \"" + tod + "\"")
if(update) {
TimeOfDay.postUpdate(tod)
PreviousTimeOfDay.postUpdate(ptod)
}
else {
TimeOfDay.sendCommand(tod)
PreviousTimeOfDay.sendCommand(ptod)
}
]
rule "Get time period for right now"
when
System started
then
val morning = now.withTimeAtStartOfDay.plusHours(6).millis
val sunrise = new DateTime((Sunrise_Time.state as DateTimeType).calendar.timeInMillis)
val twilight = new DateTime((Twilight_Time.state as DateTimeType).calendar.timeInMillis)
val evening = new DateTime((Sunset_Time.state as DateTimeType).calendar.timeInMillis)
val night = now.withTimeAtStartOfDay.plusHours(23).millis
if(now.isAfter(morning) && now.isBefore(sunrise)) updateTimeOfDay.apply("Morning", "Night", true)
else if(now.isAfter(sunrise) && now.isBefore(twilight)) updateTimeOfDay.apply("Day", "Morning", true)
else if(now.isAfter(twilight) && now.isBefore(evening)) updateTimeOfDay.apply("Twilight", "Day", true)
else if(now.isAfter(evening) && now.isBefore(night)) updateTimeOfDay.apply("Evening", "Twilight", true)
else updateTimeOfDay.apply("Night", "Evening", true)
end
rule "Morning start"
when
Time cron "0 0 6 * * ? *"
then
updateTimeOfDay.apply("Morning", TimeOfDay.state.toString, false)
end
rule "Day start"
when
Item Sunrise_Event received update ON
then
updateTimeOfDay.apply("Day", TimeOfDay.state.toString, false)
end
rule "Twilight start"
when
Item Twilight_Event received update ON
then
updateTimeOfDay.apply("Twilight", TimeOfDay.state.toString, false)
end
rule "Evening start"
when
Item Sunset_Event received update ON
then
updateTimeOfDay.apply("Evening", TimeOfDay.state.toString, false)
end
rule "Night started"
when
Time cron "0 0 23 * * ? *"
then
updateTimeOfDay.apply("Night", TimeOfDay.state.toString, false)
end