I had a look at the Design Pattern “Time of Day” and tried to adapt it for “light of day” purposes.
In my astro.things I put an offset line for sun rise and sun set for 15 Minutes to be sure it is dark
astro:sun:timer [ geolocation="52.520008, 13.404954,100", interval=60 ] {
Channels:
Type start : rise#start [
offset=15
]
Type start : set#start [
offset=-15
]
}
In my astro.items file, I added the 3 items
DateTime vSunrise_late "Day Late [%1$tH:%1$tM]" <sun> { channel="astro:sun:timer:rise#start" }
DateTime vSunset_late "Evening Early [%1$tH:%1$tM]" <sunset> { channel="astro:sun:timer:set#start" }
String vLightOfDay "How much light [%s]" <lod>
The rule is lod.rules
val logName = "Light state"
rule "Calculate Light state"
when
System started or // run at system start in case the time changed when OH was offline
Channel 'astro:sun:timer:rise#event' triggered START or
Channel 'astro:sun:timer:set#event' triggered START or
Time cron "0 1 0 * * ? *" or // one minute after midnight so give Astro time to calculate the new day's times
Time cron "0 0 6 * * ? *" or
Time cron "0 0 23 * * ? *"
then
logInfo(logName, "Calculating Light...")
// Convert the Astro Items to Joda DateTime
val bright_start = new DateTime(vSunrise_late.state.toString)
val dark_start = new DateTime(vSunset_late.state.toString)
// Calculate the current light of day
var curr_light = "UNKNOWN"
switch now {
case now.isAfter(bright_start) && now.isBefore(dark_start): curr = "BRIGHT"
case now.isAfter(dark_start) && now.isBefore(bright_start): curr = "DARK"
}
// Publish the current state
logInfo(logName, "Calculated Light " + curr_light)
vLightOfDay.sendCommand(curr)
end
The log says
2020-12-14 20:19:30.910 [INFO ] [e.smarthome.model.script.Light state] - Calculating Light...
2020-12-14 20:19:30.920 [INFO ] [e.smarthome.model.script.Light state] - Calculated Light UNKNOWN
I can’t figure out why!?
Any ideas?