Sun set/dawn

Hey all,

I’ve been trying to use the sun dawn/dusk calculator rule from here:

and am getting crazy numbers.

For example according to this website:

My Sun Height at 6:40am is supposed to be 0.8deg and azimuth is supposed to be 76.7deg.

OpenHAB is reporting these as height = -37.8deg and azimuth as 8.9deg

Anyone know if there is any known issues with this calculator? Do I have to adjust since my longitude is a negative number here in North America?

It might be that I just don’t know how to use it properly, but the issue I have with the astro binding is that all it really does it fire at dawn and dusk. I tried using those events to then set a “isDark” variable to true at dusk and to false at dawn. The problem is if you edit site maps/rules or restart openHAB etc, isDark has to wait until the next dawn or dusk event before it is set. This rule re-calculates sun angle every 5 minutes (configurable) so I can recalc “isDark” based on sun angle at that same frequency. This seems much more reliable.

So don’t use a variable and use a persisted Item instead. This lets you centralize your time of day calculations and use the value throughout your rules files and in your sitemap.

Create a System started rule to recalculate the current time period.

Also, instead of a boolean I highly recommend using a String Item and use the name of the time period to indicate what time period you are in. It is much easier to expand and adjust without impacting the rules that care about time of day.


String          TimeOfDay
String          PreviousTimeOfDay

DateTime        Date                    "Date and Time: [%1$tm/%1$td/%1$tY %1$tT]"      <calendar>      (Weather)       { ntp="America/Denver:en_EN" }
Switch          Twilight_Event                                                          (Weather)       { astro="planet=sun, type=set, property=start, offset=-90" }
DateTime        Twilight_Time   "Twilight [%1$tr]"  <moon>  (Weather)       { astro="planet=sun, type=set, property=start, offset=-90" }
Switch          Sunset_Event                                (Weather)   { astro="planet=sun, type=set, property=start" }
DateTime        Sunset_Time             "Sunset [%1$tr]"        <moon>  (Weather)       { astro="planet=sun, type=set, property=start" }
Switch          Sunrise_Event                                                           (Weather)       { astro="planet=sun, type=rise, property=start" }
DateTime        Sunrise_Time    "Sunrise [%1$tr]"       <sun>   (Weather)       { astro="planet=sun, type=rise, property=start" }


import org.openhab.core.library.types.*
import org.joda.time.*
import org.eclipse.xtext.xbase.lib.*

val Functions$Function3 updateTimeOfDay = [String tod, String ptod, boolean update |
        logInfo("Weather", "Setting PreviousTimeOfDay to \"" + ptod + "\" and TimeOfDay to \"" + tod + "\"")
        if(update) {
        else {

rule "Get time period for right now"
        System started
    val morning = now.withTimeAtStartOfDay.plusHours(6).millis
    val sunrise = new DateTime((Sunrise_Time.state as DateTimeType).calendar.timeInMillis)
    val twilight = new DateTime((Twilight_Time.state as DateTimeType).calendar.timeInMillis)
    val evening = new DateTime((Sunset_Time.state as DateTimeType).calendar.timeInMillis)
    val night = now.withTimeAtStartOfDay.plusHours(23).millis

    if(now.isAfter(morning) && now.isBefore(sunrise))       updateTimeOfDay.apply("Morning", "Night", true)
    else if(now.isAfter(sunrise) && now.isBefore(twilight)) updateTimeOfDay.apply("Day", "Morning", true)
    else if(now.isAfter(twilight) && now.isBefore(evening)) updateTimeOfDay.apply("Twilight", "Day", true)
    else if(now.isAfter(evening) && now.isBefore(night))    updateTimeOfDay.apply("Evening", "Twilight", true)
    else                                                    updateTimeOfDay.apply("Night", "Evening", true)

rule "Morning start"
        Time cron "0 0 6 * * ? *"
    updateTimeOfDay.apply("Morning", TimeOfDay.state.toString, false)

rule "Day start"
        Item Sunrise_Event received update ON
    updateTimeOfDay.apply("Day", TimeOfDay.state.toString, false)

rule "Twilight start"
        Item Twilight_Event received update ON
    updateTimeOfDay.apply("Twilight", TimeOfDay.state.toString, false)

rule "Evening start"
        Item Sunset_Event received update ON
        updateTimeOfDay.apply("Evening", TimeOfDay.state.toString, false)

rule "Night started"
        Time cron "0 0 23 * * ? *"
    updateTimeOfDay.apply("Night", TimeOfDay.state.toString, false)
1 Like

Thank you so much for you example. Now I understand the binding much better. Smart way to use it!

In the following line I get an error:

val sunrise = new DateTime((Sunrise_Time.state as DateTimeType).calendar.timeInMillis)


Error during the execution of startup rule 'Get time period for right now': Cannot cast org.openhab.core.types.UnDefType to org.openhab.core.library.types.DateTimeType

Any idea?

Hi folks,

found a solution in this thread you have been involved, too :wink:


This is great. Appreciate the help on this. Hoping to have a deeper look and try tin implement in my system tonight.

Openhab2 on Linux Mint 18.3 with Astro binding installed.
My first rule! Here’s what worked for me to turn my outside lights on at night and off during the day. The log files on Linux Mint are stored at /var/log/openhab2. Looking through the logs helped me a lot.
My outside lights are assigned a group identity of “OutsideLights” in my items file.

====================== RULES FILE ===============================

   rule "Daylight Start Event" /* ********** TURN OUTSIDE LIGHTS OFF ********** */
          Channel 'astro:sun:local:daylight#event' triggered START


  rule "Daylight End Event" /* ********** TURN OUTSIDE LIGHTS ON ********** */
          Channel 'astro:sun:local:daylight#event' triggered END