Astro daylight - Check on startup

I need OpenHab to check if it is day or night on startup in a rules file. I’ve spent the last several hours scouring this forum and the documentation to figure out how. But nothing works. All examples seem to be old and not applicable. This seems like something that should be incredibly straight forward.

Can someone enlighten me how I test to see if the current time is between various Astro channels?

Assuming you assign the sunrise Channel to the DateTime Item Sunrise and do the same for Sunset:

val sunset = new DateTime(Sunset.state.toString)
val sunrise = new DateTime(Sunrise.state.toString)
if(now.isAfter(sunrise) && now.isBefore(sunset)){
    // It's daylight

There are other ways to do this as well. If you have more than one rule that cares about this you should look at the Time of Day Design Pattern.

Awesome, thank you!! I will give this a shot when I get home this afternoon. That is so much shorter and simpler than basically everything else I’ve read.

It’s really only the startup rule that requires this, and it sets a switch to ON for day and OFF for night. All other rules simply check the switch state. And the switch state changes on the sunrise/sunset event channels every day.

With the OH3 DateTime Conversion, the following changes worked for me…


val sunrise = (Sunrise_Time.state as DateTimeType).getZonedDateTime()
val sunset = (Sunset_Time.state as DateTimeType).getZonedDateTime()
if(now.isAfter(sunrise) && now.isBefore(sunset)){
        // It's daylight


DateTime         Sunrise_Time       "Sunrise [%1$tH:%1$tM]"                   { channel="astro:sun:local:rise#start" }
DateTime         Sunset_Time        "Sunset [%1$tH:%1$tM]"                    { channel="astro:sun:local:set#start" }

Another option is to use the phase#name channel provided by the Astro binding. It contains either DAYTIME or NIGHT.


String AstroPhase "Day/Night"   {channel="astro:sun:local:phase#name"}


if (AstroPhase=="DAYTIME") { 
  // its daylight