- openHABian 3.3.0 on rPi4 with 4GB
I thought I have another crack at the time of day business, by pillaging what Rich invented many years ago.
rule "Astro: calculate time of day state"
when
// run at system start in case the time changed when OH was offline
System started or
Channel 'astro:sun:local:rise#event' triggered START or
Channel 'astro:sun:local:set#event' triggered START or
Channel 'astro:sun:local:astroDusk#event' triggered START or
//Time cron "5 /5 * * * ? *" or // temporary during rule development
// 1 min after midnight for Astro to calculate the new day's times
Time cron "5 1 0 * * ? *" or
Time cron "5 0 6 * * ? *" or
Time cron "5 0 21 * * ? *" or
Time cron "5 0 22 * * ? *"
then
/*
Started out with https://community.openhab.org/t/deprecated-design-pattern-time-of-day/15407
until I realised it is deprecated; then found:
https://community.openhab.org/t/creating-capabilities-with-rule-templates-time-of-day/127965/6
*/
val ZonedDateTime zdt = ZonedDateTime.now()
val ZonedDateTime start_of_day = zdt.toLocalDate().atStartOfDay(zdt.getOffset())
// Calculate the times for the static times of day and populate the
// associated items. Update when changing static times.
// Jump to tomorrow and subtract to avoid problems at the change over
// to/from DST; not applicable to QLD, but left for universal application
// (static) Lifestyle times of day (set arbitrarily)
// 06:00
val morning_start = start_of_day.plusDays(1).minusHours(18)
tod_MorningTime.postUpdate(morning_start.toString)
// 21:00
val night_start = start_of_day.plusDays(1).minusHours(3)
tod_NightTime.postUpdate(night_start.toString)
// 22:00
val bed_start = start_of_day.plusDays(1).minusHours(2)
tod_BedTime.postUpdate(bed_start.toString)
// (dynamic) Astro times of day; here early April 2023
// 06:03
val day_start = (tod_SunriseTime.state as DateTimeType).getZonedDateTime()
// 16:17
val afternoon_start = (tod_EveningTime.state as DateTimeType).getZonedDateTime()
// 17:40
val evening_start = (tod_SunsetTime.state as DateTimeType).getZonedDateTime()
// Calculate the current time of day
var curr = "UNKNOWN"
switch now
{
// 06:00 06:03
case now.isAfter(morning_start) && now.isBefore(day_start): curr = "MORNING"
// 06:03 16:07
case now.isAfter(day_start) && now.isBefore(afternoon_start): curr = "DAY"
// 16:07 17:44
case now.isAfter(afternoon_start) && now.isBefore(evening_start): curr = "AFTERNOON"
// 17:44 21:00
case now.isAfter(evening_start) && now.isBefore(night_start): curr = "EVENING"
// 21:00 06:03
case now.isAfter(night_start): curr = "NIGHT"
// 22:00 06:00
case now.isAfter(bed_start) && now.isBefore(morning_start): curr = "BED"
}
// Publish the current state in case it has changed
if (tod_TimeOfDay.state != curr)
{
tod_TimeOfDay.sendCommand(curr)
logInfo(LOG_PREFIX + "01.02", "Updated time of day to..: " + curr)
}
end
As it stand, the rule does not switch to AFTERNOON. and it is UNKNOWN at midnight till morning.
2023-04-06 00:00:00.913 [INFO ] [penhab.core.model.script.Astro.01.02] - Updated time of day to..: EVENING
2023-04-06 00:01:00.911 [INFO ] [penhab.core.model.script.Astro.01.02] - Updated time of day to..: UNKNOWN
2023-04-06 06:00:00.917 [INFO ] [penhab.core.model.script.Astro.01.02] - Updated time of day to..: MORNING
2023-04-06 06:03:00.008 [INFO ] [penhab.core.model.script.Astro.01.02] - Updated time of day to..: DAY
2023-04-06 17:43:00.841 [INFO ] [penhab.core.model.script.Astro.01.02] - Updated time of day to..: EVENING
2023-04-06 21:00:05.411 [INFO ] [penhab.core.model.script.Astro.01.02] - Updated time of day to..: NIGHT
2023-04-07 00:01:05.400 [INFO ] [penhab.core.model.script.Astro.01.02] - Updated time of day to..: UNKNOWN
2023-04-07 06:00:05.405 [INFO ] [penhab.core.model.script.Astro.01.02] - Updated time of day to..: MORNING
2023-04-07 06:04:00.016 [INFO ] [penhab.core.model.script.Astro.01.02] - Updated time of day to..: DAY
What am I missing?
I was thinking, wouldn’t it be better to ‘abuse’ other astro events with offsets to cater for the other static events?