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:

http://aa.usno.navy.mil/cgi-bin/aa_altazw.pl?form=1&body=10&year=2016&month=4&day=15&intv_mag=10&state=NY&place=buffalo

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.

items

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" }

rules

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) {
                TimeOfDay.postUpdate(tod)
                PreviousTimeOfDay.postUpdate(ptod)
        }
        else {
                TimeOfDay.sendCommand(tod)
                PreviousTimeOfDay.sendCommand(ptod)
        }
]

rule "Get time period for right now"
when
        System started
then
    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)
end

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

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

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

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

rule "Night started"
when
        Time cron "0 0 23 * * ? *"
then
    updateTimeOfDay.apply("Night", TimeOfDay.state.toString, false)
end
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:

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:

Guys,

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 ********** */
  when
          Channel 'astro:sun:local:daylight#event' triggered START
            
  then
         OutsideLights.sendCommand("OFF")

  end 

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

  then
          OutsideLights.sendCommand("ON")

  end

===============================================================