Simply turn on at sunrise, turn off at sunset in openhab2

I am new to openhab2 and I am a bit stuck. I’m trying to get some of my zwave lights to turn on at sunset and off at sunrise. Should be simple right? I think I am mixing up openhab1 and openhab2 methods and it is not working

In my items file I have:
Switch Sunrise_Event {astro=“planet=sun, type=rise, property=start, offset=0”}
Switch Sunset_Event {astro=“planet=sun, type=set, property=end, offset=0”}

an example zwave lamp is:
Dimmer BedroomLamp "Bedroom Lamp" (Garden,Lights) { channel="zwave:device:3e29eba6:node6:switch_dimmer:switch_dimmer" }

in my rules file I have:

rule "Bedroom Light On At Sunset"
        when
                Item Sunset_Event received update ON
        then
                postUpdate(BedroomLamp, 90)
end

but this rule never fires. In the openhab.log and events.log I don’t even see the Sunrise_Event or Sunset_Event fire.

Any idea what I am doing wrong?

Thanks in advance!

1 Like

Simple question first, you do have the Astro Binding installed and configured for your location, right?

Nothing, it simply doesn’t work that way.
Two solutions:
1:

2:

Open issue:

You can use the 1.9 astro binding in OH2 instead of the native OH2 one. This works in the same way as for OH 1.8, and events based on sunrise/sunset etc trigger fine.

1 Like

The events you are looking for are not available with openHAB2 anymore, as a better solution is underway but not available yet. Here’s yet another workaround:

@Kevin_Lieb

Here’s what I did to generate sunrise/sunset events. It relies on the NTP binding being set up to refresh the time every 60 seconds. I think there may still be a boundary condition in my math, but so far I haven’t seen it occur in over a couple months… I use the same approach for several other events, such as start of Day, Night, Dawn, Dusk, etc., which is why I didn’t use Sun Elevation as mentioned above. However, I’ve removed all those other items and rules for the sake of brevity.

// Sunrise & Sunset
DateTime SunriseStart_Time          "Sunrise Start [%1$tH:%1$tM]"       { channel="astro:sun:local:rise#start" }
DateTime SunsetStart_Time           "Sunset Start [%1$tH:%1$tM]"        { channel="astro:sun:local:set#start" }

Switch SunriseStart_Event           "Sunrise Event"
Switch SunsetStart_Event            "Sunset Event"

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

rule "Generate Time of Day Events"
when
    Item CurrentTime received update
then
    val now = new Date()
    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)) == (sunrise.getTime-(sunrise.getTime%60000))) {
        logInfo("time-of-day.rules", "Start of Sunrise!")
        SunriseStart_Event.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (sunset.getTime-(sunset.getTime%60000))) {
        logInfo("time-of-day.rules", "Start of Sunset!")
        SunsetStart_Event.postUpdate(ON)
    }
end

rule "Sunrise started"
when
    Item SunriseStart_Event received update ON
then
    logInfo("time-of-day.rules", "Its Sunrise")
end

rule "Sunset started"
when
    Item SunsetStart_Event received update ON
then
    logInfo("time-of-day.rules", "Its Sunset")
end
2 Likes

Thanks for the advice everyone! @mhilbush I am going to try your fix first. I see those NTP updates every minute in the logs so that should work fine.

I don’t want to downgrade Astro if I don’t have to. I’ll let you know how it works out.

Its still not working. Not sure why
@mhilbush the log statement logInfo(“time-of-day.rules”, “Its Sunrise”) should go to my openhab.log in the userdata/logs folder right? If so its not going. I even did a final else saying “its neither sunrise or sunset” and it didn’t work. I get minute by minutes update of time like:
2016-09-25 05:43:07.973 [ItemStateChangedEvent ] - ntp_ntp_local_dateTime changed from 2016-09-25T05:42:07.954+0000 to 2016-09-25T05:43:07.954+0000
2016-09-25 05:43:07.977 [ItemStateChangedEvent ] - ntp_ntp_local_string changed from 2016-09-25 05:42:07 UTC to 2016-09-25 05:43:07 UTC

so it is firing time change events. Any hints?

Thanks!

Correct. You also should be seeing an entry in the events.log for CurrentTime

Did you put these lines in a .items file? If so, you should be seeing entries in events.log every time CurrentTime is updated.

// Sunrise & Sunset
DateTime SunriseStart_Time          "Sunrise Start [%1$tH:%1$tM]"       { channel="astro:sun:local:rise#start" }
DateTime SunsetStart_Time           "Sunset Start [%1$tH:%1$tM]"        { channel="astro:sun:local:set#start" }

Switch SunriseStart_Event           "Sunrise Event"
Switch SunsetStart_Event            "Sunset Event"

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

I would suggest you do the following:

Put this in a .items file:

// Sunrise & Sunset
DateTime SunriseStart_Time          "Sunrise Start [%1$tH:%1$tM]"       { channel="astro:sun:local:rise#start" }
DateTime SunsetStart_Time           "Sunset Start [%1$tH:%1$tM]"        { channel="astro:sun:local:set#start" }

Switch SunriseStart_Event           "Sunrise Event"
Switch SunsetStart_Event            "Sunset Event"

Put this in a .rules file:

rule "Generate Time of Day Events"
when
    Item ntp_ntp_local_dateTime received update
then
    val now = new Date()
    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)) == (sunrise.getTime-(sunrise.getTime%60000))) {
        logInfo("time-of-day.rules", "Start of Sunrise!")
        SunriseStart_Event.postUpdate(ON)
    }
    else if((now.getTime-(now.getTime%60000)) == (sunset.getTime-(sunset.getTime%60000))) {
        logInfo("time-of-day.rules", "Start of Sunset!")
        SunsetStart_Event.postUpdate(ON)
    }
end

ok I made a separate .rules and .items file for these things. But I am getting an error:
Rule 'Generate Time of Day Events': An error occured during the script execution: null

Seems to be happening at:
val now = new Date()

(I put a debug line in before this line and it fires, but after does not fire)

Any idea why?

Is there a language reference for whatever scripting language the .rules files are in?

Thanks!!

Try putting this at the beginning of the .rules file.

import java.util.Date

Hey Kevin,
just wanted to remind you: Marks rule sure has more to offer but if you are looking for a simple “at sunset / at sunrise” solution, mine is way smaller and less resource hungry.

I thought in openHAB2 imports are not necessary any more?

Me either. :confused:

This only covers a few default imports: http://docs.openhab.org/features/automation/ruledsl.html#the-syntax

1 Like

Thanks for the clarification, @ThomDietrich.

You’ve done a lot of great work on the docs. We should read them. :sunglasses:

I didn’t take a look at the OH2 docs for weeks, even months … because the pages were mostly empty.
As I can see now this has changed significantly! Thanks for that :ribbon:

1 Like

ok, still having problems. In my rule I am printing SunsetStart_Time and it doesn’t seem to be set. Like:
logInfo("sunrisesunset.rules","SunsetStart_Time is " + SunsetStart_Time)
and it prints:
(Type=DateTimeItem, State=NULL, Label=Sunset Start, Category=null)
so seems it is not being initialized.

i see events:
2016-09-26 05:40:02.835 [INFO ] [ding.astro.handler.AstroThingHandler] - Scheduled astro DailyJob at midnight for thing astro:sun:home
2016-09-26 05:40:02.885 [INFO ] [ding.astro.handler.AstroThingHandler] - Scheduled astro DailyJob at midnight for thing astro:moon:home

at startup so maybe eventually Astro will be initialized :wink:

Any other hints?

Thanks!

You might need a startup rule to get your items initialized (quicker). I have added a few extra details to my solution:

Looks like your Astro channel definition is different than mine. In your .items file, try changing “local” to “home” in your channel definition.

Was

channel="astro:sun:local:rise#start"
channel="astro:sun:local:set#start"

For yours, I think it should be

channel="astro:sun:home:rise#start"
channel="astro:sun:home:set#start"
1 Like