rule "Time of Day"
when
System started or
Item g_temperature_average received update or
Time cron "0 0 5 * * ? *" or
Time cron "0 0 8 * * ? *" or
Time cron "0 0 16 * * ? *" or
Time cron "0 0 22 * * ? *"
then
// get the DateTime for the start of the time periods
//5am
val morningStart = now.withTimeAtStartOfDay.plusDays(1).minusHours(24-(heating_morning_time_on.state as Number).intValue)
//8am
val dayStart = now.withTimeAtStartOfDay.plusDays(1).minusHours(24-(heating_morning_time_off.state as Number).intValue)
//4pm
//val eveningStart = now.withTimeAtStartOfDay.plusDays(1).minusHours(24-16)
val eveningStart = new DateTime(Evening_Time.state.toString())
//10pm
val lateEvening = now.withTimeAtStartOfDay.plusDays(1).minusHours(24-(heating_daytime_time_off.state as Number).intValue)
val nightStart = now.withTimeAtStartOfDay.plusDays(1).minusHours(24-(lamp_lighting_time_off.state as Number).intValue)
logInfo("Time of day",String::format("Now: %s Morning Start %s day Start %s evening start %s late evening: %s night start %s",
now,morningStart,dayStart,eveningStart,lateEvening,nightStart))
var newToD = "UNKNOWN"
switch now {
case now.isAfter(morningStart) && now.isBefore(dayStart) : newToD = "MORNING"
case now.isAfter(dayStart) && now.isBefore(eveningStart) : newToD = "DAY"
case now.isAfter(eveningStart) && now.isBefore(lateEvening) : newToD = "EVENING"
case now.isAfter(lateEvening) && now.isBefore(nightStart) : newToD = "LATEEVENING"
case now.isAfter(nightStart) : newToD = "NIGHT"
}
if(heating_timeofday.state.toString != newToD)
{
heating_timeofday.postUpdate(newToD)
logInfo("Time of day","Setting Time of Day: " + newToD)
}
end
heating_morning_time_on is 5
heating_morning_time_off is 8
heating_daytime_time_off is 22
lamp_lighting_time_off is 23
Isn’t Now after night?
Now: 2018-09-23T00:01:51.233+12:00
night start 2018-09-23T23:00:00.000+12:100:
Hmm no it’s not because it’s finally switch from 2018-09-22 to 2018-09-23.
So i need another case for isBefore(morningStart) I think. Right?
To get around the wrapping around midnight, you can make your default the ToD that wraps it. So, instead of using “UNKNOWN” for the default, use “NIGHT” and then remove it from the switch/case. IIRC, this is how the DP is written too.