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

Me either. :confused:

This only covers a few default imports:

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?


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.



For yours, I think it should be

1 Like

I think that is it! I see events firing now and no more errors.

In my .things file I do have defined:

astro:sun:home   [ geolocation="37.540868, -122.309395", interval=60 ]
astro:moon:home  [ geolocation="37.540868, -122.309395", interval=60 ]

so I guess I tweaked the setting a bit. This is the problem of hacking at stuff without understanding what I am doing :wink:

Thanks for all the help everyone!

@ThomDietrich I am going to try your suggestion as well. Thanks for the advice!

Glad we got it working. Let me know if you want the other rules/items for handling Day, Night, Dawn, and Dusk.

@mhilbush Would you take the time to summarize what was said here and the additional elements in a tutorial?


1 Like

Here you go.

1 Like

Great! Thank you. I’ll include the simple solution underneath as an alternative.

Seems I am still having a problem with sunset. I tracked it down to this:
At 00:00:00 GMT on the transition from 10/2 to 10/3,which is 5p my time the sunset time changes from 2016-10-03T01:49:00.000+0000 to 2016-10-04T01:48:00.000+0000. Problem is: this is sunset Tomorrow, not today. This happens every day so I never hit that sunset time. Any ideas why this is happening and how to fix it @mhilbush or anyone else?

My hack to get around this is today “if the time is sunset or the time is sunset minus 1 day…” like

else if( ((now.getTime-(now.getTime%60000)) == (sunset.getTime-(sunset.getTime%60000))) ||
         ((now.getTime-(now.getTime%60000)) == ((sunset.getTime-(sunset.getTime%60000)) - 86400000 ))) {

but that is just a hack (and I am not sure if it will work yet)

Not sure what’s happening. I know the Astro daily job is scheduled to run at midnight, which is where it updates the values for sunset, sunrise, dawn, etc., I believe. I would think the Astro daily job would run at midnight your time, not midnight GMT. Just a guess on my part, but is the timezone set correctly in the NTP thing?

The Raspberry Pi this is running on it set to UTC.

I am changing the .things file to say:
ntp:ntp:home [ hostname="", refreshInterval=60, refreshNtp=60 timeZone="America/Los_Angeles" ]

adding the timeZone part. I will see if that helps. For now my “today or yesterday at this same time” hack seems to be working


Ok, let us know how it works out.

Yeah, still not working. My ntp setting is as above but the recompute of sunrise / sunset still happens at midnight GMT. I’ll stick with my hack since it is working for now and try to troubleshoot my sitemap problems instead :wink:

I missed this earlier. Maybe it’s because of this?

The Astro binding sets the daily job to midnight using the following expression, which is midnight on the system clock.

Trigger trigger = newTrigger().withIdentity("dailyJobTrigger", thingUid)
        .withSchedule(CronScheduleBuilder.cronSchedule("0 0 0 * * ?")).build();

Have you considered changing the Pi to your local timezone?

I did end up setting my system clock to my local timezone. On Raspian on the Raspberry Pi this meant making a file /etc/timezone that contained the string America/Los_Angeles alone. I was concerned this would mess up other things but it didn’t: sunrise / sunset now trigger at the right time, Kodi clock is right and filesystem was not corrupted. Sunrise / sunset times were adjusted at the real midnight.

Sky did not fall :wink: