It might be that I just donāt know how to use it properly, but the issue I have with the astro binding is that all it really does it fire at dawn and dusk. I tried using those events to then set a āisDarkā variable to true at dusk and to false at dawn. The problem is if you edit site maps/rules or restart openHAB etc, isDark has to wait until the next dawn or dusk event before it is set. This rule re-calculates sun angle every 5 minutes (configurable) so I can recalc āisDarkā based on sun angle at that same frequency. This seems much more reliable.
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.
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
Thank you so much for you example. Now I understand the binding much better. Smart way to use it!
In the following line I get an error:
val sunrise = new DateTime((Sunrise_Time.state as DateTimeType).calendar.timeInMillis)
Error:
Error during the execution of startup rule 'Get time period for right now': Cannot cast org.openhab.core.types.UnDefType to org.openhab.core.library.types.DateTimeType
Openhab2 on Linux Mint 18.3 with Astro binding installed.
My first rule! Hereās what worked for me to turn my outside lights on at night and off during the day. The log files on Linux Mint are stored at /var/log/openhab2. Looking through the logs helped me a lot.
My outside lights are assigned a group identity of āOutsideLightsā in my items file.
rule "Daylight Start Event" /* ********** TURN OUTSIDE LIGHTS OFF ********** */
when
Channel 'astro:sun:local:daylight#event' triggered START
then
OutsideLights.sendCommand("OFF")
end
rule "Daylight End Event" /* ********** TURN OUTSIDE LIGHTS ON ********** */
when
Channel 'astro:sun:local:daylight#event' triggered END
then
OutsideLights.sendCommand("ON")
end