Simulate Astrobinding Events in OH 2

Hi There,

since the Astro binding in OH 2 does not longer support the events, i have to build a simulation of this.

Goal ist to define some switches to check if it is day or night or coffe break time, based on some Astro values. But i am stuck at the moment.

With a rule i cehck every Minute if some values are i a defined range (e.g. now is before Night_start and after Day_end -> dusk_Switch to ON). Currently my problem is that night starts one day and end on the next day, but the Astro bindung gives me all the values for one day …

Does anyone has build a similar solution?


I’ve posted how I do this on this thread.

I’m still on OH 1 so the main thing that drives the change in Time of Day are the events (bummer the went away), but when OH has restarted or the rules file is reloaded I don’t know how long OH was down so need to redetermine the current time of day in a System started rule.

The way I handle the situation for Night is to test everything else first and if it isn’t any of those times it must be Night by default. This lets you sidestep the problem where a time period stretches over midnight by forming your conditions such that you never have to test for it directly. Instead it is a process of elimination.

Also, I advise using a String Item to store the current time of day as opposed to a bunch of Switches. It greatly simplifies the rules and your items and it makes it easier to add or remove named time periods. I too started out with separate switches but when, upon the advice of @watou, I switched to using a String everything became much much simpler and more flexible.

One note about the code linked, there is a minor known bug in it when sunrise occurs before 6 am which I’m currently just living with right now but will correct someday. Ultimately I need to skip over the Morning state in the summer when sunrise is before 6.

Another alternative is to split the test into two tests. Astro recalculates its times at midnight. So you can test to see if it is between Night_start and midnight or if now is between midnight and Morning_start. If it is after midnight, Morning_start will have been recalculated for the current day.

If you use this approach, I recommend you set up your cron trigger to trigger on second 30 or 45 instead of 0 to give Astro time to recalculate the new times before your rule executes immediately after midnight.

Is there any reason you are not using the 1.9 astro binding instead of the 2.0 one? Triggered events are working fine using this, in the same way they did in OH 1.8.

That is how i’m doing it right now with the 2.0 binding

rule "astro"

when Time cron "0 */1 * * * ?"  // every 1 minute

    //logInfo("astro.rule", "checking sunrise & sunset events")

    var timerFreq = 1 //timer frequence in minutes, adjust if you change cron

    var DateTime dtNow = now // save now at starting of the rule ...
    var DateTime daystart = new DateTime((Sunrise_Time.state as DateTimeType).calendar.timeInMillis)
    var DateTime dayend = new DateTime((Sunset_Time.state as DateTimeType).calendar.timeInMillis)
    var int offset = 0

    /***************   before sunset   **********************/                                                                                                                      
    offset = (Lights_delay.state as DecimalType).intValue //minutes after sunset

    if(dtNow.isEqual(dayend.minusMinutes(offset)) || (dtNow.isAfter(dayend.minusMinutes(offset)) && dtNow.isBefore(dayend.minusMinutes(offset - timerFreq)))) {
            logInfo("astro.rule", "firing LightsOn_Event at offset:" + offset)

    /***************   after sunset   ***********************/
    //offset = 15 //minutes after sunset
    offset = (Shutter_delay.state as DecimalType).intValue //minutes after sunset
    if(dtNow.isEqual(dayend.plusMinutes(offset)) || (dtNow.isAfter(dayend.plusMinutes(offset)) && dtNow.isBefore(dayend.plusMinutes(offset + timerFreq)))) {
            logInfo("astro.rule", "firing ShutterWinDown_Event at offset:" + offset)

On the Item.rule, you look i.e. for an update of the Item LightsOn_Event.

rule     "light"
    Item LightsOn_Event received update ON

Since any/everyone needs this logic or functionality (I.e. event switches) and will probably build similar custom rules, wouldn’t it be useful to issue a feature request for the Astro binding to re-add this?

A feasibly way would be a separate channel or to be able to define a Switch item for the current time channels ( just like a switch item for the hue color channel)

This feature has not yet been implemented intentionally, because we are waiting for to be implemented first - this is then the ideal mechanism for supporting events in the Astro 2.0 binding.

Could you confirm that the feature is not implemented?
I would like to use the sunset event to set a switch. In OH1 I made that without problems.
In the documentation I found:
rule "example trigger rule"
Channel ‘astro:sun:home:rise#event’ triggered START


I tried that but it seems to not trig

It’s implemented in the newer snapshots (not in beta5 as far as I know):

Read down to the bottom, very helpful information!

1 Like

I will try the latest snapshot tomorrow otherwise I will adopt the temporary solution described in the post you gently reported.