Problem in tutorials: Time of Day Events. "Could not cast NULL to org.eclipse.smarthome.core. "

I have copy and paste the tutorials examples: Time of Day Events, but I get some error in my log file. No time showing up in the sitemap and no event take place. I have read further in the thread, but I don’t see any notice about my problem. I haven’t make any adjustment to my file, so it stand as in the first post.

I’m using a pi 3b+. Running OH 2.4 which has been power off and on.

I have copied the error from the log file, Asto item, rules and sitemap (The file on PI).

I don’t think there any problem in the Astro and sitemap, since the log file point to DawnStart_ in this line:
val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis)

Can any tell my what the problem is and how I correct it.

Copy of error in log file:
‘’’
2019-09-21 16:37:11.537 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Generate Time of Day Events’: Could not cast NULL to org.eclipse.smarthome.core.library.types.DateTimeType; line 59, column 26, length 36

2019-09-21 16:37:55.188 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Text

2019-09-21 16:37:55.198 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘SunriseStart_Time’ for widget org.eclipse.smarthome.model.sitemap.Text

2019-09-21 16:37:55.204 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘SunriseStart_Time’ for widget org.eclipse.smarthome.model.sitemap.Text

2019-09-21 16:37:55.208 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘SunriseStart_Time’ for widget org.eclipse.smarthome.model.sitemap.Text

2019-09-21 16:37:55.213 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Text

2019-09-21 16:37:55.217 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘SunsetStart_Time’ for widget org.eclipse.smarthome.model.sitemap.Text

2019-09-21 16:37:55.225 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘SunsetStart_Time’ for widget org.eclipse.smarthome.model.sitemap.Text

2019-09-21 16:37:55.231 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘SunsetStart_Time’ for widget org.eclipse.smarthome.model.sitemap.Text
‘’’’

Astro items start here: The top part of are from the original astro example. I don’t see any problems here.

Astro.items
‘’’’
Group Astro <sun_clouds> (W66a)
//
DateTime Current_DateTime “Today [%1$tA, %1$td.%1$tm.%1$tY]” (Astro) {channel=“ntp:ntp:local:dateTime”}
//
DateTime Sunset_Time “Sunset [%1$tH:%1$tM]” (Astro) {channel=“astro:sun:home:set#start”}
DateTime Sunrise_Time “Sunrise [%1$tH:%1$tM]” (Astro) {channel=“astro:sun:home:rise#end”}
DateTime Evening_Time “Evening [%1$tH:%1$tM]” (Astro) {channel=“astro:sun:minus90:set#start”}
//
String Day_Phase “Phase of Day [MAP(astro.map):%s]” (Astro) {channel=“astro:sun:home:phase#name”}
Switch Night_State “Night” (Astro)
//
String Season_Name “Season [MAP(astro.map):%s]” (Astro) {channel=“astro:sun:home:season#name”}
String Zodiac_Sign “Zodiac [MAP(astro.map):%s]” (Astro) {channel=“astro:sun:home:zodiac#sign”}
Number Sun_Elevation “Sun Elevation [%.1f °]” (Astro) {channel=“astro:sun:home:position#elevation”}
Number Moon_Elevation “Moon Elevation [%.1f °]” (Astro) {channel=“astro:moon:home:position#elevation”}
String Moon_Phase “Moon Phase [MAP(astro.map):%s]” (Astro) {channel=“astro:moon:home:phase#name”}
DateTime Moon_Next_Full “Next Full Moon [%1$td.%1$tm.%1$tY, %1$tH:%1$tM]” (Astro) {channel=“astro:moon:home:phase#full”}
DateTime Moon_Next_New “Next New Moon [%1$td.%1$tm.%1$tY, %1$tH:%1$tM]” (Astro) {channel=“astro:moon:home:phase#new”}

///

//Design Pattern: time-of-day-events
// Time of Day Events

// Times of Day
String TimePeriodOfDay “Time of Day [%s]”

DateTime DawnStart_Time “Dawn Start [%1$tH:%1$tM]” { channel=“astro:sun:local:civilDawn#start” }
DateTime DawnStop_Time “Dawn Stop [%1$tH:%1$tM]” { channel=“astro:sun:local:civilDawn#end” }
DateTime DayStart_Time “Day Start [%1$tH:%1$tM]” { channel=“astro:sun:local:daylight#start” }
DateTime DayStop_Time “Day Stop [%1$tH:%1$tM]” { channel=“astro:sun:local:daylight#end” }
DateTime DuskStart_Time “Dusk Start [%1$tH:%1$tM]” { channel=“astro:sun:local:civilDusk#start” }
DateTime DuskStop_Time “Dusk Stop [%1$tH:%1$tM]” { channel=“astro:sun:local:civilDusk#end” }
DateTime NightStart_Time “Night Start [%1$tH:%1$tM]” { channel=“astro:sun:local:night#start” }
DateTime NightStop_Time “Night Stop[%1$tH:%1$tM]” { channel=“astro:sun:local:night#end” }
DateTime MorningNightStart_Time “MorningNightStart [%1$tH:%1$tM]” { channel=“astro:sun:local:morningNight#start” }
DateTime MorningNightStop_Time “MorningNightStop [%1$tH:%1$tM]” { channel=“astro:sun:local:morningNight#end” }
DateTime EveningNightStart_Time “EveningNightStart [%1$tH:%1$tM]” { channel=“astro:sun:local:eveningNight#start” }
DateTime EveningNightStop_Time “EveningNightStop [%1$tH:%1$tM]” { channel=“astro:sun:local:eveningNight#end” }

// Events
Switch DawnStart_Event “Start of Dawn Event”
Switch DayStart_Event “Start of Day Event”
Switch DuskStart_Event “Start of Dusk Event”
Switch NightStart_Event “Start of Night Event”

// Items
Switch Dawn “Dawn [%s]” // After Dawn and before Day
Switch Day “Day [%s]” // After Day and before Dusk
Switch Dusk “Dusk [%s]” // After Dusk and before Night
Switch Night “Night [%s]” // After Night and before Dawn

Number SunAzimuth “Azimuth [%.0f °]” { channel=“astro:sun:local:position#azimuth” }
Number SunElevation “Elevation [%.0f °]” { channel=“astro:sun:local:position#elevation” }

String MoonPhase “MoonPhase [%s]” { channel=“astro:moon:local:phase#name” }
‘’’’

RULES file start here:

time-of-day.rules
‘’’’
import java.util.Date

// Time of Day Rules
// Requires time update every 60 seconds

val String RFN = “time-of-day.rules”

rule “Get time period for right now”
when
System started
then
val now = new Date()
val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis)
val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)
val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)
val night = new Date((NightStart_Time.state as DateTimeType).calendar.timeInMillis)

val String initStr = "Initializing time period. The time of day is "

if(now.after(dawn) && now.before(day)) {
    logInfo(RFN, initStr + "Dawn: " + now)
    Dawn.sendCommand(ON)
    Day.sendCommand(OFF)
    Dusk.sendCommand(OFF)
    Night.sendCommand(OFF)
    TimePeriodOfDay.postUpdate("Dawn")
}
else if(now.after(day) && now.before(dusk)) {
    logInfo(RFN, initStr + "Day: " + now)
    Dawn.sendCommand(OFF)
    Day.sendCommand(ON)
    Dusk.sendCommand(OFF)
    Night.sendCommand(OFF)
    TimePeriodOfDay.postUpdate("Day")
}
else if(now.after(dusk) && now.before(night)) {
    logInfo(RFN, initStr + "Dusk: " + now)
    Dawn.sendCommand(OFF)
    Day.sendCommand(OFF)
    Dusk.sendCommand(ON)
    Night.sendCommand(OFF)
    TimePeriodOfDay.postUpdate("Dusk")
}
else {
    logInfo(RFN, initStr + "Night: " + now)
    Dawn.sendCommand(OFF)
    Day.sendCommand(OFF)
    Dusk.sendCommand(OFF)
    Night.sendCommand(ON)
    TimePeriodOfDay.postUpdate("Night")
}

end

rule “Generate Time of Day Events”
when
Item CurrentTime received update
then
val now = new Date()
val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis)
val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)
val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)
val night = new Date((NightStart_Time.state as DateTimeType).calendar.timeInMillis)
val sunset = new Date((SunsetStart_Time.state as DateTimeType).calendar.timeInMillis)
val sunrise = new Date((SunriseStart_Time.state as DateTimeType).calendar.timeInMillis)

if((now.getTime-(now.getTime%60000)) == (dawn.getTime-(dawn.getTime%60000))) {
    logInfo(RFN, "Transitioning to Dawn!!!")
    DawnStart_Event.postUpdate(ON)
}
else if((now.getTime-(now.getTime%60000)) == (day.getTime-(day.getTime%60000))) {
    logInfo(RFN, "Transitioning to Day!!!")
    DayStart_Event.postUpdate(ON)
}
else if((now.getTime-(now.getTime%60000)) == (dusk.getTime-(dusk.getTime%60000))) {
    logInfo(RFN, "Transitioning to Dusk!!!")
    DuskStart_Event.postUpdate(ON)
}
else if((now.getTime-(now.getTime%60000)) == (night.getTime-(night.getTime%60000))) {
    logInfo(RFN, "Transitioning to Night!!!")
    NightStart_Event.postUpdate(ON)
}

if((now.getTime-(now.getTime%60000)) == (sunrise.getTime-(sunrise.getTime%60000))) {
    logInfo(RFN, "Start of Sunrise!")
    SunriseStart_Event.postUpdate(ON)
}
else if((now.getTime-(now.getTime%60000)) == (sunset.getTime-(sunset.getTime%60000))) {
    logInfo(RFN, "Start of Sunset!")
    SunsetStart_Event.postUpdate(ON)
}

end

rule “Dawn Started”
when
Item DawnStart_Event received update ON
then
val now = new Date()
val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis)
val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)

if(now.after(dawn) && now.before(day)) {
    logInfo(RFN, "Its Dawn: " + now)
    Dawn.sendCommand(ON)
    Day.sendCommand(OFF)
    Dusk.sendCommand(OFF)
    Night.sendCommand(OFF)
    TimePeriodOfDay.postUpdate("Dawn")
}

end

rule “Day Started”
when
Item DayStart_Event received update ON
then
val now = new Date()
val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)
val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)

if(now.after(day) && now.before(dusk)) {
    logInfo(RFN, "Its Day: " + now)
    Dawn.sendCommand(OFF)
    Day.sendCommand(ON)
    Dusk.sendCommand(OFF)
    Night.sendCommand(OFF)
    TimePeriodOfDay.postUpdate("Day")
}

end

rule “Dusk started”
when
Item DuskStart_Event received update ON
then
val now = new Date()
val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)
val night = new Date((NightStart_Time.state as DateTimeType).calendar.timeInMillis)

if(now.after(dusk) && now.before(night)) {
    logInfo(RFN, "Its Dusk: " + now)
    Dawn.sendCommand(OFF)
    Day.sendCommand(OFF)
    Dusk.sendCommand(ON)
    Night.sendCommand(OFF)
    TimePeriodOfDay.postUpdate("Dusk")
}

end

rule “Night started”
when
Item NightStart_Event received update ON
then
val now = new Date()
val morningNightStart = new Date((MorningNightStart_Time.state as DateTimeType).calendar.timeInMillis)
val morningNightStop = new Date((MorningNightStop_Time.state as DateTimeType).calendar.timeInMillis)
val eveningNightStart = new Date((EveningNightStart_Time.state as DateTimeType).calendar.timeInMillis)
val eveningNightStop = new Date((EveningNightStop_Time.state as DateTimeType).calendar.timeInMillis)

if((now.after(morningNightStart) && now.before(morningNightStop)) || 
   (now.after(eveningNightStart) && now.before(eveningNightStop))) {
    logInfo(RFN, "Its Night: " + now)
    Dawn.sendCommand(OFF)
    Day.sendCommand(OFF)
    Dusk.sendCommand(OFF)
    Night.sendCommand(ON)
    TimePeriodOfDay.postUpdate("Night")
}

end

rule “Sunrise started”
when
Item SunriseStart_Event received update ON
then
val now = new Date()
logInfo(RFN, "Its Sunrise : " + now)
end

rule “Sunset started”
when
Item SunsetStart_Event received update ON
then
val now = new Date()
logInfo(RFN, "Its Sunset : " + now)
end
‘’’

Sitemap start here:
Test.sitemap
‘’’’
sitemap test2 label=“Header2”

{
Frame label=“Day switch”
{
Switch item=Switch1 label=“Test knap”
}
Slider item=Dimmer1 label=“dimmer [%d %%]”
Switch item=Pir1 label=“movement”
Text item=Pir1_Bat label=“pir1”
Text item=Door1 label=“driveway [%s]”
// Text item=Door1_B label=“door1”
Text item=Door2 label=“Door2 [%s]”
// Text item=Door2_B label=“door2”
Text item=Door3 label=“Door3 [%s]”
// Text item=Door3_B label=“door3”
Text item=Door4 label=“Door4 [%s]”
// Text item=Door4_B label=“door4”
Text item=Door5 label=“Door5 [%s]”
// Text item=Door5_B label=“door5”
Text item=Door6 label=“Door6 [%s]”
// Text item=Door6_B label=“door6”
Text item=Door7 label=“Stairway [%s]”
Text item=Westwall label=“West wall [%s]”
// Text item=Door7_B label=“door7”
Text item=Door8 label=“Door8 [%s]”
// Text item=Door8_B label=“door8”
Switch item=Switch1 label=“Test knap”
Switch item=Sonoff_s31_1 label=“Wall plug 1”
Switch item=Sonoff_s31_2 label=“Wall plug 2”
Text item=vTimeOfDay label=“vTimeOfDay [%]”

///
//Time of Day Events
//https://community.openhab.org/t/time-of-day-events/14554

Frame {
Text label=“Time of Day Information” {
Frame label=“Time Period of Day” {
Text item=TimePeriodOfDay label=“Time of Day”
}
Frame label=“Time Transition Events” {
Text item=SunriseStart_Time label=“Sunrise Start Time”
Text item=SunsetStart_Time label=“Sunset Start Time”
Text item=DawnStart_Time label=“Dawn Start Time”
Text item=DayStart_Time label=“Day Start Time”
Text item=DuskStart_Time label=“Dusk Start Time”
Text item=NightStart_Time label=“Night Start Time”
}
Frame label=“Time of Day Status” {
Text item=Dawn label=“Is Dawn”
Text item=Day label=“Is Day”
Text item=Dusk label=“Is Dusk”
Text item=Night label=“Is Night”
}
Frame label=“Sun” {
Text item=SunAzimuth label=“Sun Azimuth” icon=“sun”
Text item=SunElevation label=“Sun Elevation” icon=“sun”
}
Frame label=“Moon” {
Text item=MoonPhase label=“Moon Phase” icon=“moon”
}
}
}
///

}
‘’’’’

It is an example, to show you how you can do this and tailor it for your needs; not a complete copy-paste solution.

You don’t have an Item called SunriseStart_Time or one called SunsetStart_Time

Item DawnStart_Time has not (yet) been populated with a state, on this run of the rule.

That can happen if the rule runs before Astro has done all its work.
It begins life at system startup with state NULL, and later the Astro binding should update it to something meaningful, if you have configured correctly. Usually Astro has done that before rules start - but sometimes not, depending on system.

Astro calculates those at system startup and at midnight.
If the Item gets reset to NULL state, by editing for example, it will stay that way until next Astro run.

And please, as always, use code fences.

@Udo_Hartmann Thanks for pointing my ‘’’ out, I will remember to use 3 ``` next time I start and end coding here in the blog.

@rossko57 Thanks for point out the problems in the config.

I have removed SunriseStart_Time, SunsetStart_Time since I only need 4 times to start with. Dawn, Day, Dusk, Night. There was some more in the example that also needed to be corrected.
System has been power off/on

I don’t see any update in the sitemap, but I assume it’s related to

So I will wait 24 hour to get a full run of all state.

Is there any way I can force a run Astro or get a faster replacement of “NULL” state, after an edit.

You could reboot openHAB, or if you search these forums find a way to “restart a bundle” and restart Astro.

Still got some error in the log file and I don’t see any update in the sitemap. I have power off/on. I have used the internal consol to stop and start Astro binding. The log file include that:

2019-09-22 17:36:26.006 [hingStatusInfoChangedEvent] - 'astro:sun:home' changed from UNINITIALIZED to UNINITIALIZED (HANDLER_MISSING_ERROR)

2019-09-22 17:36:26.021 [hingStatusInfoChangedEvent] - 'astro:moon:home' changed from ONLINE to UNINITIALIZED

2019-09-22 17:36:26.024 [hingStatusInfoChangedEvent] - 'astro:moon:home' changed from UNINITIALIZED to UNINITIALIZED (HANDLER_MISSING_ERROR)

2019-09-22 17:36:32.955 [vent.ItemStateChangedEvent] - CurrentTime changed from 2019-09-22T17:35:32.715-0400 to 2019-09-22T17:36:32.939-0400

2019-09-22 17:36:32.962 [vent.ItemStateChangedEvent] - Current_DateTime changed from 2019-09-22T17:35:32.715-0400 to 2019-09-22T17:36:32.939-0400

==> /var/log/openhab2/openhab.log <==

2019-09-22 17:36:32.974 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Generate Time of Day Events': Could not cast NULL to org.eclipse.smarthome.core.library.types.DateTimeType; line 59, column 26, length 36

==> /var/log/openhab2/events.log <==

2019-09-22 17:37:13.959 [hingStatusInfoChangedEvent] - 'astro:sun:home' changed from UNINITIALIZED (HANDLER_MISSING_ERROR) to INITIALIZING

2019-09-22 17:37:13.966 [hingStatusInfoChangedEvent] - 'astro:sun:home' changed from INITIALIZING to ONLINE

==> /var/log/openhab2/openhab.log <==

2019-09-22 17:37:14.126 [INFO ] [thome.binding.astro.internal.job.Job] - Scheduled Astro event-jobs for thing astro:sun:home

==> /var/log/openhab2/events.log <==

2019-09-22 17:37:14.130 [vent.ItemStateChangedEvent] - Sun_Elevation changed from 16.831844825199482 to 16.435221411707

==> /var/log/openhab2/openhab.log <==

2019-09-22 17:37:14.157 [INFO ] [ding.astro.handler.AstroThingHandler] - Scheduled Positional job astro:sun:home every 300 seconds

==> /var/log/openhab2/events.log <==

2019-09-22 17:37:14.202 [hingStatusInfoChangedEvent] - 'astro:moon:home' changed from UNINITIALIZED (HANDLER_MISSING_ERROR) to INITIALIZING

2019-09-22 17:37:14.204 [vent.ItemStateChangedEvent] - Sun_Elevation changed from 16.435221411707 to 16.43500025498709

2019-09-22 17:37:14.213 [hingStatusInfoChangedEvent] - 'astro:moon:home' changed from INITIALIZING to ONLINE

==> /var/log/openhab2/openhab.log <==

2019-09-22 17:37:14.277 [INFO ] [thome.binding.astro.internal.job.Job] - Scheduled Astro event-jobs for thing astro:sun:home

==> /var/log/openhab2/events.log <==

2019-09-22 17:37:14.299 [vent.ItemStateChangedEvent] - Sun_Elevation changed from 16.43500025498709 to 16.434722156396436

==> /var/log/openhab2/openhab.log <==

2019-09-22 17:37:14.316 [INFO ] [ding.astro.handler.AstroThingHandler] - Scheduled Positional job astro:sun:home every 300 seconds

2019-09-22 17:37:14.330 [INFO ] [thome.binding.astro.internal.job.Job] - Scheduled Astro event-jobs for thing astro:moon:home

2019-09-22 17:37:14.342 [INFO ] [ding.astro.handler.AstroThingHandler] - Scheduled Positional job astro:moon:home every 300 seconds

==> /var/log/openhab2/events.log <==

2019-09-22 17:37:14.348 [vent.ItemStateChangedEvent] - Moon_Elevation changed from -22.401981576407465 to -22.60491903409721

2019-09-22 17:37:14.379 [vent.ItemStateChangedEvent] - Sun_Elevation changed from 16.434722156396436 to 16.434532647821626

2019-09-22 17:37:14.420 [vent.ItemStateChangedEvent] - Moon_Phase changed from WANING_CRESCENT to UNDEF

2019-09-22 17:37:14.424 [vent.ItemStateChangedEvent] - Moon_Elevation changed from -22.60491903409721 to 0.0

==> /var/log/openhab2/openhab.log <==

2019-09-22 17:37:14.439 [INFO ] [thome.binding.astro.internal.job.Job] - Scheduled Astro event-jobs for thing astro:moon:home

2019-09-22 17:37:14.452 [INFO ] [ding.astro.handler.AstroThingHandler] - Scheduled Positional job astro:moon:home every 300 seconds

==> /var/log/openhab2/events.log <==

2019-09-22 17:37:14.458 [vent.ItemStateChangedEvent] - Moon_Phase changed from UNDEF to WANING_CRESCENT

2019-09-22 17:37:14.462 [vent.ItemStateChangedEvent] - Moon_Elevation changed from 0.0 to -22.605111558465754

2019-09-22 17:37:14.487 [vent.ItemStateChangedEvent] - Moon_Elevation changed from -22.605111558465754 to -22.605169321778405

2019-09-22 17:37:32.966 [vent.ItemStateChangedEvent] - CurrentTime changed from 2019-09-22T17:36:32.939-0400 to 2019-09-22T17:37:32.946-0400

2019-09-22 17:37:32.971 [vent.ItemStateChangedEvent] - Current_DateTime changed from 2019-09-22T17:36:32.939-0400 to 2019-09-22T17:37:32.946-0400

==> /var/log/openhab2/openhab.log <==

2019-09-22 17:37:32.981 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Generate Time of Day Events': Could not cast NULL to org.eclipse.smarthome.core.library.types.DateTimeType; line 59, column 26, length 36

==> /var/log/openhab2/events.log <==

2019-09-22 17:38:32.969 [vent.ItemStateChangedEvent] - CurrentTime changed from 2019-09-22T17:37:32.946-0400 to 2019-09-22T17:38:32.951-0400

2019-09-22 17:38:32.979 [vent.ItemStateChangedEvent] - Current_DateTime changed from 2019-09-22T17:37:32.946-0400 to 2019-09-22T17:38:32.951-0400

==> /var/log/openhab2/openhab.log <==

2019-09-22 17:38:32.995 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Generate Time of Day Events': Could not cast NULL to org.eclipse.smarthome.core.library.types.DateTimeType; line 59, column 26, length 36

==> /var/log/openhab2/events.log <==

2019-09-22 17:39:32.970 [vent.ItemStateChangedEvent] - CurrentTime changed from 2019-09-22T17:38:32.951-0400 to 2019-09-22T17:39:32.955-0400

2019-09-22 17:39:32.983 [vent.ItemStateChangedEvent] - Current_DateTime changed from 2019-09-22T17:38:32.951-0400 to 2019-09-22T17:39:32.955-0400

==> /var/log/openhab2/openhab.log <==

2019-09-22 17:39:32.995 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Generate Time of Day Events': Could not cast NULL to org.eclipse.smarthome.core.library.types.DateTimeType; line 59, column 26, length 36

2019-09-22 17:40:32.997 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Generate Time of Day Events': Could not cast NULL to org.eclipse.smarthome.core.library.types.DateTimeType; line 59, column 26, length 36

==> /var/log/openhab2/events.log <==

2019-09-22 17:40:33.004 [vent.ItemStateChangedEvent] - CurrentTime changed from 2019-09-22T17:39:32.955-0400 to 2019-09-22T17:40:32.963-0400

2019-09-22 17:40:33.013 [vent.ItemStateChangedEvent] - Current_DateTime changed from 2019-09-22T17:39:32.955-0400 to 2019-09-22T17:40:32.963-0400

2019-09-22 17:41:32.988 [vent.ItemStateChangedEvent] - CurrentTime changed from 2019-09-22T17:40:32.963-0400 to 2019-09-22T17:41:32.968-0400

2019-09-22 17:41:32.997 [vent.ItemStateChangedEvent] - Current_DateTime changed from 2019-09-22T17:40:32.963-0400 to 2019-09-22T17:41:32.968-0400

==> /var/log/openhab2/openhab.log <==

2019-09-22 17:41:33.004 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Generate Time of Day Events': Could not cast NULL to org.eclipse.smarthome.core.library.types.DateTimeType; line 59, column 26, length 36

If I understand correct it should update at power on or when a binding is stop/start.
So is there something I did change in the correctly, in one of the config files? There should only be dawn,day,dusk,night.

ntp.items

DateTime CurrentTime	"Date [%1$tA, %1$tm/%1$td/%1$tY %1$tT]"	<calendar>	{ channel="ntp:ntp:local:dateTime" }

astro.items

Group Astro <sun_clouds> (W66a)
//
DateTime    Current_DateTime     "Today [%1$tA, %1$td.%1$tm.%1$tY]"                <clock>  (Astro) {channel="ntp:ntp:local:dateTime"}
//
DateTime    Sunset_Time          "Sunset [%1$tH:%1$tM]"                            <sun>    (Astro) {channel="astro:sun:home:set#start"}
DateTime    Sunrise_Time         "Sunrise [%1$tH:%1$tM]"                           <sun>    (Astro) {channel="astro:sun:home:rise#end"}
DateTime    Evening_Time         "Evening [%1$tH:%1$tM]"                                    (Astro) {channel="astro:sun:minus90:set#start"}
//
String      Day_Phase            "Phase of Day [MAP(astro.map):%s]"                         (Astro) {channel="astro:sun:home:phase#name"}
Switch      Night_State          "Night"                                                    (Astro)
//
String      Season_Name          "Season [MAP(astro.map):%s]"                               (Astro) {channel="astro:sun:home:season#name"}
String      Zodiac_Sign          "Zodiac [MAP(astro.map):%s]"                      <zodiac> (Astro) {channel="astro:sun:home:zodiac#sign"}
Number      Sun_Elevation        "Sun Elevation [%.1f °]"                          <sun>    (Astro) {channel="astro:sun:home:position#elevation"}
Number      Moon_Elevation       "Moon Elevation [%.1f °]"                         <moon>   (Astro) {channel="astro:moon:home:position#elevation"}
String      Moon_Phase           "Moon Phase [MAP(astro.map):%s]"                  <moon>   (Astro) {channel="astro:moon:home:phase#name"}
DateTime    Moon_Next_Full       "Next Full Moon [%1$td.%1$tm.%1$tY, %1$tH:%1$tM]" <moon>   (Astro) {channel="astro:moon:home:phase#full"}
DateTime    Moon_Next_New        "Next New Moon [%1$td.%1$tm.%1$tY, %1$tH:%1$tM]"  <moon>   (Astro) {channel="astro:moon:home:phase#new"}


/// 

//Design Pattern: time-of-day-events
// https://community.openhab.org/t/time-of-day-events/14554

// Times of Day
String TimePeriodOfDay              "Time of Day [%s]"

DateTime DawnStart_Time             "Dawn Start [%1$tH:%1$tM]"              { channel="astro:sun:local:civilDawn#start" }
DateTime DawnStop_Time              "Dawn Stop [%1$tH:%1$tM]"               { channel="astro:sun:local:civilDawn#end" }
DateTime DayStart_Time              "Day Start [%1$tH:%1$tM]"               { channel="astro:sun:local:daylight#start" }
DateTime DayStop_Time               "Day Stop [%1$tH:%1$tM]"                { channel="astro:sun:local:daylight#end" }
DateTime DuskStart_Time             "Dusk Start [%1$tH:%1$tM]"              { channel="astro:sun:local:civilDusk#start" }
DateTime DuskStop_Time              "Dusk Stop [%1$tH:%1$tM]"               { channel="astro:sun:local:civilDusk#end" }
DateTime NightStart_Time            "Night Start [%1$tH:%1$tM]"             { channel="astro:sun:local:night#start" }
DateTime NightStop_Time             "Night Stop[%1$tH:%1$tM]"               { channel="astro:sun:local:night#end" }
//edit DateTime MorningNightStart_Time     "MorningNightStart [%1$tH:%1$tM]"       { channel="astro:sun:local:morningNight#start" }
//edit DateTime MorningNightStop_Time      "MorningNightStop [%1$tH:%1$tM]"        { channel="astro:sun:local:morningNight#end" }
//edit DateTime EveningNightStart_Time     "EveningNightStart [%1$tH:%1$tM]"       { channel="astro:sun:local:eveningNight#start" }
//edit DateTime EveningNightStop_Time      "EveningNightStop [%1$tH:%1$tM]"        { channel="astro:sun:local:eveningNight#end" }

// Events
Switch DawnStart_Event              "Start of Dawn Event"
Switch DayStart_Event               "Start of Day Event"                         
Switch DuskStart_Event              "Start of Dusk Event"                            
Switch NightStart_Event             "Start of Night Event"                           

// Items
Switch Dawn                         "Dawn [%s]"                             // After Dawn and before Day
Switch Day                          "Day [%s]"                              // After Day and before Dusk
Switch Dusk                         "Dusk [%s]"                             // After Dusk and before Night
Switch Night                        "Night [%s]"                            // After Night and before Dawn

Number SunAzimuth                   "Azimuth [%.0f °]"                      { channel="astro:sun:local:position#azimuth" }
Number SunElevation                 "Elevation [%.0f °]"                    { channel="astro:sun:local:position#elevation" }

String MoonPhase                    "MoonPhase [%s]"                        { channel="astro:moon:local:phase#name" }

The error point to DawnStart_ in the .rules
“val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis)”

“[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Generate Time of Day Events’: Could not cast NULL to org.eclipse.smarthome.core.library.types.DateTimeType; line 59, column 26, length 36”

time-of-day.rules

import java.util.Date

// Time of Day Rules
// Requires time update every 60 seconds

val String RFN = "time-of-day.rules"

rule "Get time period for right now"
when
    System started
then
    val now = new Date()
    val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis)
    val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)
    val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)
    val night = new Date((NightStart_Time.state as DateTimeType).calendar.timeInMillis)

    val String initStr = "Initializing time period. The time of day is "

    if(now.after(dawn) && now.before(day)) {
        logInfo(RFN, initStr + "Dawn: " + now)
        Dawn.sendCommand(ON)
        Day.sendCommand(OFF)
        Dusk.sendCommand(OFF)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Dawn")
    }
    else if(now.after(day) && now.before(dusk)) {
        logInfo(RFN, initStr + "Day: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(ON)
        Dusk.sendCommand(OFF)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Day")
    }
    else if(now.after(dusk) && now.before(night)) {
        logInfo(RFN, initStr + "Dusk: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(OFF)
        Dusk.sendCommand(ON)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Dusk")
    }
    else {
        logInfo(RFN, initStr + "Night: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(OFF)
        Dusk.sendCommand(OFF)
        Night.sendCommand(ON)
        TimePeriodOfDay.postUpdate("Night")
    }
end

rule "Generate Time of Day Events"
when
    Item CurrentTime received update
then
    val now = new Date()
    val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis)
    val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)
    val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)
    val night = new Date((NightStart_Time.state as DateTimeType).calendar.timeInMillis)
    //edit val sunset = new Date((SunsetStart_Time.state as DateTimeType).calendar.timeInMillis)
    //edit val sunrise = new Date((SunriseStart_Time.state as DateTimeType).calendar.timeInMillis)

    if((now.getTime-(now.getTime%60000)) == (dawn.getTime-(dawn.getTime%60000))) {
        logInfo(RFN, "Transitioning to Dawn!!!")
        DawnStart_Event.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (day.getTime-(day.getTime%60000))) {
        logInfo(RFN, "Transitioning to Day!!!")
        DayStart_Event.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (dusk.getTime-(dusk.getTime%60000))) {
        logInfo(RFN, "Transitioning to Dusk!!!")
        DuskStart_Event.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (night.getTime-(night.getTime%60000))) {
        logInfo(RFN, "Transitioning to Night!!!")
        NightStart_Event.postUpdate(ON)
    }

    //edit if((now.getTime-(now.getTime%60000)) == (sunrise.getTime-(sunrise.getTime%60000))) {
    //edit     logInfo(RFN, "Start of Sunrise!")
    //edit    SunriseStart_Event.postUpdate(ON)
    //edit }
    //edit else if((now.getTime-(now.getTime%60000)) == (sunset.getTime-(sunset.getTime%60000))) {
    //edit    logInfo(RFN, "Start of Sunset!")
    //edit    SunsetStart_Event.postUpdate(ON)
    //edit }
end

rule "Dawn Started"
when
    Item DawnStart_Event received update ON
then
    val now = new Date()
    val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis)
    val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)

    if(now.after(dawn) && now.before(day)) {
        logInfo(RFN, "Its Dawn: " + now)
        Dawn.sendCommand(ON)
        Day.sendCommand(OFF)
        Dusk.sendCommand(OFF)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Dawn")
    }
end

rule "Day Started"
when
    Item DayStart_Event received update ON
then
    val now = new Date()
    val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)
    val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)

    if(now.after(day) && now.before(dusk)) {
        logInfo(RFN, "Its Day: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(ON)
        Dusk.sendCommand(OFF)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Day")
    }
end

rule "Dusk started"
when
    Item DuskStart_Event received update ON
then
    val now = new Date()
    val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)
    val night = new Date((NightStart_Time.state as DateTimeType).calendar.timeInMillis)

    if(now.after(dusk) && now.before(night)) {
        logInfo(RFN, "Its Dusk: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(OFF)
        Dusk.sendCommand(ON)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Dusk")
    }
end

rule "Night started"
when
    Item NightStart_Event received update ON
then
    val now = new Date()
    val night = new Date((NightStart_Time.state as DateTimeType).calendar.timeInMillis) // edit
    val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis) //edit
    //edit val eveningNightStart = new Date((EveningNightStart_Time.state as DateTimeType).calendar.timeInMillis)
    //edit val eveningNightStop = new Date((EveningNightStop_Time.state as DateTimeType).calendar.timeInMillis)

    if(now.after(night) && now.before(dawn)) {
        logInfo(RFN, "Its Night: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(OFF)
        Dusk.sendCommand(OFF)
        Night.sendCommand(ON)
        TimePeriodOfDay.postUpdate("Night")
    }
end

rule "Sunrise started"
when
    Item DawnStart_Event received update ON
then
    val now = new Date()
    logInfo(RFN, "Its Sunrise : " + now)
end

rule "Sunset started"
when
    Item DuskStart_Event received update ON
then
    val now = new Date()
    logInfo(RFN, "Its Sunset : " + now)
end

Yes, but you do need a valid channel to get an update from any binding.

Spot the difference -

DateTime    Sunset_Time ... {channel="astro:sun:home:set#start"}

DateTime DawnStart_... { channel="astro:sun:local:civilDawn#start" }

Pointing at different Thinsg there. Which Astro Things do you have? home, local?

From when I installed my pi with the demo I never deleted my astro sitemap and it show some update on that sitemap.
So I looked into my astro items file and it used this channel setup

DateTime    Sunset_Time          "Sunset [%1$tH:%1$tM]"                            <sun>    (Astro) {channel="astro:sun:home:set#start"}

and if I look in the "time of the event the channel look a little different

DateTime DawnStart_Time             "Dawn Start [%1$tH:%1$tM]"              { channel="astro:sun:local:civilDawn#start" }

I see the working point to home and not working local.

Can this be related to my problem

@rossko57 Damm you spot a problem fast.
I slowly work forward and backward.
I will change the channel from local to home.

That just make another question.
Why is there a local and/or home channel?

I look in to my paper ui astro things and I can see all the channel is set to “home”
Its a slow process for me to move forward, but I start to see the light at the end of the tunnel.

experience based … we all have similar troubles getting started.

In general, bindings model the real world as Things. Like a light switch or rollerblind.

The Things Astro is interested in are places. So you can create Things like “myHome”, “myWorkplace”, “thatCity”, “sistersHouse” etc.with any name you like and at different places on the planet, They will have sunrise at different times.

My dawn, day, dusk rule and sitemap updates working fine. But when it comes to night, nothing happen.

I have printed out my rules file, so I could get a better overview and compare line for line, but I don’t see any variation in any of the rules. Checked “caps”, bracket [] () {}. I don’t see any missing.

I have checked my night channel name, from the paper UI\things\astro is correct.
I copied it into a remark in astro.items. Looking ok.

The only different thing, I see is the from the working rules are there are in the same day, but night split at midnight.

When I search “Validation issues found in configuration” in the forum lot of the answer point to “val” and “var” type.

I only use “val” in my config and so is in the tutorials.

So I don’t understand, why it shouldn’t work now?

Log:

2019-09-23 19:06:49.574 [INFO ] [thome.model.script.time-of-day.rules] - Transitioning to Dusk!!!
2019-09-23 19:06:49.594 [INFO ] [thome.model.script.time-of-day.rules] - Its Sunset : Mon Sep 23 19:06:49 EDT 2019
2019-09-23 19:06:49.618 [INFO ] [thome.model.script.time-of-day.rules] - Its Dusk: Mon Sep 23 19:06:49 EDT 2019
2019-09-23 22:37:42.842 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'time-of-day.rules', using it anyway:
2019-09-23 22:37:42.962 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'time-of-day.rules'
2019-09-23 22:49:02.965 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'time-of-day.rules', using it anyway:
2019-09-23 22:49:03.074 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'time-of-day.rules'
2019-09-23 22:49:33.852 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'time-of-day.rules', using it anyway:
2019-09-23 22:49:33.932 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'time-of-day.rules'
2019-09-24 06:32:35.600 [INFO ] [thome.model.script.time-of-day.rules] - Transitioning to Dawn!!!
2019-09-24 06:32:35.619 [INFO ] [thome.model.script.time-of-day.rules] - Its Sunrise : Tue Sep 24 06:32:35 EDT 2019
2019-09-24 06:32:35.632 [INFO ] [thome.model.script.time-of-day.rules] - Its Dawn: Tue Sep 24 06:32:35 EDT 2019
2019-09-24 07:01:36.040 [INFO ] [thome.model.script.time-of-day.rules] - Transitioning to Day!!!
2019-09-24 07:01:36.090 [INFO ] [thome.model.script.time-of-day.rules] - Its Day: Tue Sep 24 07:01:36 EDT 2019

Astro.items

Group Astro <sun_clouds> (W66a)
//
DateTime    Current_DateTime     "Today [%1$tA, %1$td.%1$tm.%1$tY]"                <clock>  (Astro) {channel="ntp:ntp:local:dateTime"}
//
DateTime    Sunset_Time          "Sunset [%1$tH:%1$tM]"                            <sun>    (Astro) {channel="astro:sun:home:set#start"}
DateTime    Sunrise_Time         "Sunrise [%1$tH:%1$tM]"                           <sun>    (Astro) {channel="astro:sun:home:rise#end"}
DateTime    Evening_Time         "Evening [%1$tH:%1$tM]"                                    (Astro) {channel="astro:sun:minus90:set#start"}
//
String      Day_Phase            "Phase of Day [MAP(astro.map):%s]"                         (Astro) {channel="astro:sun:home:phase#name"}
Switch      Night_State          "Night"                                                    (Astro)
//
String      Season_Name          "Season [MAP(astro.map):%s]"                               (Astro) {channel="astro:sun:home:season#name"}
String      Zodiac_Sign          "Zodiac [MAP(astro.map):%s]"                      <zodiac> (Astro) {channel="astro:sun:home:zodiac#sign"}
Number      Sun_Elevation        "Sun Elevation [%.1f °]"                          <sun>    (Astro) {channel="astro:sun:home:position#elevation"}
Number      Moon_Elevation       "Moon Elevation [%.1f °]"                         <moon>   (Astro) {channel="astro:moon:home:position#elevation"}
String      Moon_Phase           "Moon Phase [MAP(astro.map):%s]"                  <moon>   (Astro) {channel="astro:moon:home:phase#name"}
DateTime    Moon_Next_Full       "Next Full Moon [%1$td.%1$tm.%1$tY, %1$tH:%1$tM]" <moon>   (Astro) {channel="astro:moon:home:phase#full"}
DateTime    Moon_Next_New        "Next New Moon [%1$td.%1$tm.%1$tY, %1$tH:%1$tM]"  <moon>   (Astro) {channel="astro:moon:home:phase#new"}


/// 

//Design Pattern: time-of-day-events
// https://community.openhab.org/t/time-of-day-events/14554

// Times of Day
String TimePeriodOfDay              "Time of Day [%s]"

DateTime DawnStart_Time             "Dawn Start [%1$tH:%1$tM]"              { channel="astro:sun:home:civilDawn#start" }
DateTime DawnStop_Time              "Dawn Stop [%1$tH:%1$tM]"               { channel="astro:sun:home:civilDawn#end" }
DateTime DayStart_Time              "Day Start [%1$tH:%1$tM]"               { channel="astro:sun:home:daylight#start" }
DateTime DayStop_Time               "Day Stop [%1$tH:%1$tM]"                { channel="astro:sun:home:daylight#end" }
DateTime DuskStart_Time             "Dusk Start [%1$tH:%1$tM]"              { channel="astro:sun:home:civilDusk#start" }
DateTime DuskStop_Time              "Dusk Stop [%1$tH:%1$tM]"               { channel="astro:sun:home:civilDusk#end" }
DateTime NightStart_Time            "Night Start [%1$tH:%1$tM]"             { channel="astro:sun:home:night#start" }
DateTime NightStop_Time             "Night Stop[%1$tH:%1$tM]"               { channel="astro:sun:home:night#end" }

//  astro:sun:home:night#start

// Events
Switch DawnStart_Event              "Start of Dawn Event"
Switch DayStart_Event               "Start of Day Event"                         
Switch DuskStart_Event              "Start of Dusk Event"                            
Switch NightStart_Event             "Start of Night Event"                           

// Items
Switch Dawn                         "Dawn [%s]"                             // After Dawn and before Day
Switch Day                          "Day [%s]"                              // After Day and before Dusk
Switch Dusk                         "Dusk [%s]"                             // After Dusk and before Night
Switch Night                        "Night [%s]"                            // After Night and before Dawn

Number SunAzimuth                   "Azimuth [%.0f °]"                      { channel="astro:sun:home:position#azimuth" }
Number SunElevation                 "Elevation [%.0f °]"                    { channel="astro:sun:home:position#elevation" }

String MoonPhase                    "MoonPhase [%s]"                        { channel="astro:moon:home:phase#name" }

time-of-day.rules

import java.util.Date

// Time of Day Rules
// Requires time update every 60 seconds

val String RFN = "time-of-day.rules"

rule "Get time period for right now"
when
    System started
then
    val now = new Date()
    val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis)
    val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)
    val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)
    val night = new Date((NightStart_Time.state as DateTimeType).calendar.timeInMillis)

    val String initStr = "Initializing time period. The time of day is "

    if(now.after(dawn) && now.before(day)) {
        logInfo(RFN, initStr + "Dawn: " + now)
        Dawn.sendCommand(ON)
        Day.sendCommand(OFF)
        Dusk.sendCommand(OFF)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Dawn")
    }
    else if(now.after(day) && now.before(dusk)) {
        logInfo(RFN, initStr + "Day: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(ON)
        Dusk.sendCommand(OFF)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Day")
    }
    else if(now.after(dusk) && now.before(night)) {
        logInfo(RFN, initStr + "Dusk: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(OFF)
        Dusk.sendCommand(ON)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Dusk")
    }
    else if(now.after(night) && now.before(dawn)) {
        logInfo(RFN, initStr + "Night: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(OFF)
        Dusk.sendCommand(OFF)
        Night.sendCommand(ON)
        TimePeriodOfDay.postUpdate("Night")
    }
end

rule "Generate Time of Day Events"
when
    Item CurrentTime received update
then
    val now = new Date()
    val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis)
    val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)
    val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)
    val night = new Date((NightStart_Time.state as DateTimeType).calendar.timeInMillis)
    
    if((now.getTime-(now.getTime%60000)) == (dawn.getTime-(dawn.getTime%60000))) {
        logInfo(RFN, "Transitioning to Dawn!!!")
        DawnStart_Event.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (day.getTime-(day.getTime%60000))) {
        logInfo(RFN, "Transitioning to Day!!!")
        DayStart_Event.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (dusk.getTime-(dusk.getTime%60000))) {
        logInfo(RFN, "Transitioning to Dusk!!!")
        DuskStart_Event.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (night.getTime-(night.getTime%60000))) {
        logInfo(RFN, "Transitioning to Night!!!")
        NightStart_Event.postUpdate(ON)
    }
   
end

rule "Dawn Started"
when
    Item DawnStart_Event received update ON
then
    val now = new Date()
    val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis)
    val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)

    if(now.after(dawn) && now.before(day)) {
        logInfo(RFN, "Its Dawn: " + now)
        Dawn.sendCommand(ON)
        Day.sendCommand(OFF)
        Dusk.sendCommand(OFF)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Dawn")
    }
end

rule "Day Started"
when
    Item DayStart_Event received update ON
then
    val now = new Date()
    val day = new Date((DayStart_Time.state as DateTimeType).calendar.timeInMillis)
    val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)

    if(now.after(day) && now.before(dusk)) {
        logInfo(RFN, "Its Day: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(ON)
        Dusk.sendCommand(OFF)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Day")
    }
end

rule "Dusk started"
when
    Item DuskStart_Event received update ON
then
    val now = new Date()
    val dusk = new Date((DuskStart_Time.state as DateTimeType).calendar.timeInMillis)
    val night = new Date((NightStart_Time.state as DateTimeType).calendar.timeInMillis)

    if(now.after(dusk) && now.before(night)) {
        logInfo(RFN, "Its Dusk: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(OFF)
        Dusk.sendCommand(ON)
        Night.sendCommand(OFF)
        TimePeriodOfDay.postUpdate("Dusk")
    }
end

rule "Night started"
when
    Item NightStart_Event received update ON
then
    val now = new Date()
    val night = new Date((NightStart_Time.state as DateTimeType).calendar.timeInMillis)
    val dawn = new Date((DawnStart_Time.state as DateTimeType).calendar.timeInMillis)
    
    if(now.after(night) && now.before(dawn)) {
        logInfo(RFN, "Its Night: " + now)
        Dawn.sendCommand(OFF)
        Day.sendCommand(OFF)
        Dusk.sendCommand(OFF)
        Night.sendCommand(ON)
        TimePeriodOfDay.postUpdate("Night")
    }
end

rule "Sunrise started"
when
    Item DawnStart_Event received update ON
then
    val now = new Date()
    logInfo(RFN, "Its Sunrise : " + now)
end

rule "Sunset started"
when
    Item DuskStart_Event received update ON
then
    val now = new Date()
    logInfo(RFN, "Its Sunset : " + now)
end

That’s a problem. NightStart_Time and DawnStart_Time are calculated for today. You will never have a case where now is both after night and before dawn when NightStart_Time and DawnStart_Time are for the same day. Just get rid of the && now.before(dawn) and it should work.

But I gotta say, this code is way longer and more complicated than it needs to be.

  • You don’t need the Switch Items. Just use changes to TimePeriodOfDay as your events.
  • The “Generate Time of Day Events” will run way more than it needs to (every minute). Just have the Rule trigger at the times it need to. They are all based on Astro channels so you can trigger the Rule from Astro directly.
  • You can get the Start events directly from the Astro binding as well.

The original post that you based this on was written before a lot of new capabilities were added to OH and the Astro binding. As written, this is probably 100-125 lines of code longer than it needs to be and it’s way more complicated than it needs to be. See Design Pattern: Time Of Day for a more concise way to achieve the same thing.

You can get rid of the Event Items and Dawn/Day/etc Items and just rely on TimePeriodOfDay. For example:

rule "Sunrise started"
when
    Item TimePeriodOfDay changed to Dawn
then
   logInfo(RFN, "It's Dawn: " + now)
   ...
end

NOTE: There already is a now implicit variable. You don’t need to redefine it.

rule "Calculate Time of Day"
when
    System started or
    Channel "astro:sun:home:civilDawn#event" triggered START or
    Channel "astro:sun:home:daylight#event" triggered START or
    Channel "astro:sun:home:civilDusk#event" triggered START or
    Channel "astro:sun:home:night#event" triggered START
then
    // Convert DateTimeTypes to Joda DateTimes for easier comparisons
    val dawn_start = new DateTime(DawnStart_Time.state.toString)
    val day_start = new DateTime(DayStart_Time.state.toString)
    val dusk_start = new DateTime(DuskStart_Time.state.toString)
    val night_start = new DateTime(NightStart_Time.state.toString)

    // Get the current time of day
    var curr_tod = "UNKNOWN"
    switch now {
        case now.isAfter(dawn_start) && now.isBefore(day_start): curr_tod = "Dawn"
        case now.isAfter(day_start) && now.isBefore(dusk_start): curr_tod = "Day"
        case now.isAfter(dusk_start) && now.isBefore(night_start): curr_tod = "Dusk"
        case now.isAfter(night_start): curr_tod = "Night" // dawn_start is for today so now will never be before it if we get to this case
    }

    if(curr_tod == "UNKNOWN") logError(RFN, "Failed to calculate current time of day!")

    if(TimePeriodOfDay.state.toString != curr_tod){
        logInfo(RFN, "Transitioning to " + curr_tod)
        TimePeriodOfDay.postUpdate(curr_tod)
    }
end

That’s it. That’s all there is to it. In places where you use the Event and time of day Switches just use TimePeriodOfDay in it’s place. For example:

rule "Sunset started"
when
    Item TimePeriodOfDay changed to Dusk
then
    logInfo(RFN, "Its Sunset : " + now)
end

// in some Rule replace if(Dawn.state == ON) with
    if(TimePeriodOfDay.state.toString == "Dawn")

153 lines with lots of duplicated code replaced with 30 lines of code that accomplishes the same thing with no duplicated code. Usually, fewer lines means easier to maintain and easier to understand.

1 Like

Everything working fine now.
Thanks for all the help I got.

@rlkoshak
I did look into your " [Design Pattern: Time Of Day]" and it was a little to step uphill for me. Since I have a little hard time to understand how the time syntax is. I will for sure take a look in the optimization of time rules, when I get more comfortable with OH.

I did a little z80 hex coding back in the early 90. That is all the programming I have done.