rule "Time of Day"
when
System started or
Channel 'astro:sun:local-offset:rise#event' triggered START or
Channel 'astro:sun:local-offset:set#event' triggered END or
Channel 'astro:sun:local-morning:rise#event' triggered START or
Time cron "0 1 0 ? * TUE,WED,THU,FRI *" or //1 minute after midnight to be sure that astro has refreshed
Time cron "0 1 1 ? * SUN,MON,SAT *"
then
if (now.getHourOfDay ==0){
Night_Time.postUpdate(now.withTimeAtStartOfDay.toString)
}else {
if (now.getHourOfDay ==1){
Night_Time.postUpdate(now.withTimeAtStartOfDay.plusHours(1).toString)
}
}
Morning_Time.postUpdate(Sunrise_Morning_Time.state.toString)
Day_Time.postUpdate(Sunrise_Offset_Time.state.toString)
Evening_Time.postUpdate(Sunset_Offset_Time.state.toString)
var curr = "UNKNOWN"
switch now {
case now.isAfter(new DateTime(Night_Time.state.toString)) && now.isBefore(new DateTime(Morning_Time.state.toString)): curr = "NIGHT"
case now.isAfter(new DateTime(Morning_Time.state.toString)) && now.isBefore(new DateTime(Day_Time.state.toString)): curr = "MORNING"
case now.isAfter(new DateTime(Day_Time.state.toString)) && now.isBefore(new DateTime(Evening_Time.state.toString)): curr = "DAY"
case now.isAfter(new DateTime(Evening_Time.state.toString)): curr = "EVENING"
}
Current_TOD.postUpdate(curr)
if (curr == "UNKNOWN"){sendTelegram("openHAB","TOD rule \n Nieuwe status %s \n Huidige tijd %s \n Ochtend tijd %s \n Dag tijd %s \n Avond tijd %s \n Nacht tijd %s",Current_TOD.state, now.toString("HH:mm:ss.SSS"), Morning_Time.state.format("%1$tH:%1$tM").toString,Day_Time.state.format("%1$tH:%1$tM").toString,Evening_Time.state.format("%1$tH:%1$tM").toString,Night_Time.state.format("%1$tH:%1$tM").toString)}
end
The problem is dat I get every Sunday at 1:01 a Telegram message that item "Current_TOD" is "UNKOWN". Why? Every other day it works fine, only on Sunday’s it won’t. The item "Night_Time" is at the moment: 2019-06-30T01:00:00.000+0200 (as it should be). But according the eventlog, "Current_TOD" is indeed "UNKOWN"
Part of my eventlog:
2019-06-27 00:01:00.151 [vent.ItemStateChangedEvent] - Current_TOD changed from EVENING to NIGHT
2019-06-27 05:22:00.268 [vent.ItemStateChangedEvent] - Current_TOD changed from NIGHT to MORNING
2019-06-27 08:00:00.208 [vent.ItemStateChangedEvent] - Current_TOD changed from MORNING to DAY
2019-06-27 19:00:00.112 [vent.ItemStateChangedEvent] - Current_TOD changed from DAY to EVENING
2019-06-28 00:01:00.092 [vent.ItemStateChangedEvent] - Current_TOD changed from EVENING to NIGHT
2019-06-28 05:23:00.239 [vent.ItemStateChangedEvent] - Current_TOD changed from NIGHT to MORNING
2019-06-28 08:00:00.156 [vent.ItemStateChangedEvent] - Current_TOD changed from MORNING to DAY
2019-06-28 19:00:00.136 [vent.ItemStateChangedEvent] - Current_TOD changed from DAY to EVENING
2019-06-29 01:01:00.133 [vent.ItemStateChangedEvent] - Current_TOD changed from EVENING to NIGHT
2019-06-29 05:23:00.223 [vent.ItemStateChangedEvent] - Current_TOD changed from NIGHT to MORNING
2019-06-29 08:00:00.137 [vent.ItemStateChangedEvent] - Current_TOD changed from MORNING to DAY
2019-06-29 19:00:00.201 [vent.ItemStateChangedEvent] - Current_TOD changed from DAY to EVENING
2019-06-30 01:01:00.170 [vent.ItemStateChangedEvent] - Current_TOD changed from EVENING to UNKNOWN
2019-06-30 05:24:00.172 [vent.ItemStateChangedEvent] - Current_TOD changed from UNKNOWN to MORNING
2019-06-30 08:00:00.162 [vent.ItemStateChangedEvent] - Current_TOD changed from MORNING to DAY
I am a newbie here.
Did you try removing the comment on this line? It may be breaking your when logic.
Think about how you can simplify the rule for testing to determine what is breaking. Perhaps a second “testing” rule. You have complex logic in both when and then which makes troubleshooting challenging.
The comment is not the problem, other day the rule works. I’ve created a test rule (when system started) en the rule works.
The oddness is Sunday…
Is there een possibility to “fake” the date/time to Sunday night?
Not sure.
Perhaps create a dummy switch and add your “when” logic, or at least the part with Sunday .
Is it just Sunday that is broken or Monday & Saturday too?
With a dummy switch it just works as suppose to be.
According to Freeformatter the order is SUN,MON,SAT.
But the strangest thing is, the rules fires at 01:01AM at Sunday, the Night_Time item has the correct time/date/day. And it will be only updated at System start, Tue-Fri at 00:01AM and Sat-Mon at 01:01AM.
I think that I miss something or my RPI has an own life…
So, to get set to “UNKNOWN” the rule has not satisfied any of your switch-case conditions.
I would add a diagnostic default case and check conditions data
The telegram is reporting UNKNOWN because none of your case statements are evaluating to true. So a first step will be to log out all the times and see if you can figure out why none of the statements are true. Do you have a gap in the times? Is the Rule running before the next days’s Astro times are calculated?
Check to see if all your role triggers are working. For example, if Astro is failing for some reason, the Rule won’t run. You can see events occurring in events.log.
It has been a long time, without any error. But I think I’ve found the problem nevertheless
I post the solution for further reference.
The problem is the time delay that can occur between a item.postUpdate(value) and item.state. So when you call item.state, it is possbile that the previous call item.postUpdate(value) has not happend.
For example (part of code, with possible random errors):
Morning_Time.postUpdate(Sunrise_Morning_Time.state.toString)
now.isAfter(new DateTime(Morning_Time.state.toString)) // previous line can or can't have executed
For example (corrected part of code):
val morning_time = new DateTime(Sunrise_Morning_Time.state.toString)
now.isAfter(morning_time)
Morning_Time.postUpdate(morning_time.toString)
When I wrote the code, based on Design Pattern: Time Of Day, I was lazy and didn’t use variables as stated in de Design Pattern.
So I’ve to rewrite the code en read better the original code