Time Of Day not working on Sunday

  • Platform information:
    • Hardware: Rpi 3
    • OS: OpenHabian
    • Java Runtime Environment: default
    • openHAB version: 2.5M1
  • Issue of the topic:

Based on the DP: Time of the Day I’ve the following code:

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

Does anyone see a error that I’ve missed?

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?

EDIT: I just checked at http://www.cronmaker.com/

I think your last section should be "0 1 1 ? * MON,SAT,SUN *" Perhaps the day order matters.

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… :wink:

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

default : {
   logInfo("tod.rule", "Unexpectedly reached default case")
   logInfo("tod.rule", "NT " + Night_Time.state.toString)
   logInfo("tod.rule", "MT " + Morning_Time.state.toString)
   logInfo("tod.rule", "DT " + Day_Time.state.toString)
   logInfo("tod.rule", "ET " + Evening_Time.state.toString)
}
1 Like

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?

Hmm. Perhaps order does not matter then.

If the Rule is firing then the cron expression is right.

I’l bet on a mystery Night_Time value set by another rule :wink:

Again, i’ve the same problem (it has worked till last night), only this time it’s on Saturday 1am.
The rule is the same, now I’ve a log:

> 2019-09-21 01:00:48.600 [vent.ItemStateChangedEvent] - Electricity_PastUse_24H changed from 5.64836667 kWh to 5.65476667 kWh
> 2019-09-21 01:01:00.238 [vent.ItemStateChangedEvent] - Night_Time changed from 2019-09-20T00:00:00.000+0200 to 2019-09-21T01:00:00.000+0200
> 2019-09-21 01:01:00.241 [vent.ItemStateChangedEvent] - Morning_Time changed from 2019-09-20T06:30:00.000+0200 to 2019-09-21T06:30:00.000+0200
> 2019-09-21 01:01:00.244 [vent.ItemStateChangedEvent] - Day_Time changed from 2019-09-20T08:00:00.000+0200 to 2019-09-21T08:00:00.000+0200
> 2019-09-21 01:01:00.247 [vent.ItemStateChangedEvent] - Evening_Time changed from 2019-09-20T19:46:00.000+0200 to 2019-09-21T19:43:00.000+0200
> 2019-09-21 01:01:02.683 [vent.ItemStateChangedEvent] - LivingRoom_Light_LedBulb_TVLeft_Color changed from 257,100,100 to 257,100,0 
> 2019-09-21 06:39:50.992 [vent.ItemStateChangedEvent] - Current_TOD changed from EVENING to MORNING

As you can see, only this part of the rule

		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)

has done it’s job. At 06:39 the rest of the rule did it’s job.

Nothing else related to this rule is mentioned in the event.log en openhab.log.

What can be the problem?

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 :smile:
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 :wink: