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

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?

Sure.

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="nl.pool.ntp.org", 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

Thanks!!

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:

Thanks!

Excellent! It’s always good when the sky doesn’t fall. :sweat_smile:

I do not understand why you didn’t do that to begin with!?

1 Like

I wanted all the files on my system to be in UTC. All the other servers I work on I keep in UTC always, regardless of where they are in the world.

I had exactly the same problems.
I started with openhab1.8
I initially forgot to add the astro binding
I had real problems getting the syntax for creating timers based on the sunrise/sunset times.
Then discovered these were already set up as “Events” so I set up 2 switches
1 for sunrise and 1 earlier by 30 minutes to give me more useful “Darkness”

// schedules a event which starts at sunrise, updating the Switch with ON, followed by a OFF
switch sSunrise_Event {astro=“planet=sun, type=rise, property=start”}
// schedules a event which starts 30 minutes BEFORE sunrise
Switch sSunrise_EventD {astro=“planet=sun, type=rise, property=start, offset=-30”}

and 2 more for sunset.

This was then used in a simple rule, to set a switch “Darkness”, that could then be used in many other rules. You could also manually change the “Darkness” switch, for testing.
Testing cron and timers is difficult without a time machine or a 24 hour delay.

rule "Darkness start at sunset"
when
Item sSunset_Event received update ON
then
logInfo(“DMM”,"Sunset_Event occurred ")
if(sOutsideLightFAutoDelay.state == OFF) sendCommand(Darkness, ON)
sendCommand(StrTimer,sSunset_Event.state.toString)
end

rule "Darkness start delayed after sunset"
when
Item sSunset_EventD received update ON
then
logInfo(“DMM”,"Sunset_EventD occurred ")
if(sOutsideLightFAutoDelay.state == ON) sendCommand(Darkness, ON)
end

This worked correctly for months.

I tried this in Openhab2 and discovered the same problem, the “Events” are not supported.
So I ignored openHab2, for a long while.

Recently I’ve spent time to understand how to define and use timers.
So I’ve set up a 2nd switch to use timers.

I then had the same problem as you, that sunrise was before now, and so never triggered.
I just check the times to now(), and added 24 hours.
Tomorrows sunrise is near enough the same as today, at least from the point of view of turning on outside lights.

I also discovered that you need to specifically reset these on startup.
So I added a rule on startup to set the timers

rule "Set THINGS on StartUP"
when
System started

I also needed a rule to check if the 30minute delay, to give twilight or sunset/sunrise was required. Then if it changed, recalculate and reset the timers.

Now I’ve got all these working to replace the Sunset_Event. I will try Openhab2.

I also await the new rules engine and how Astro is going to be interpreted.

1 Like

… or use the astro1 binding with openHAB2 and you can stay with the switch events :sunglasses:

This is my workaround:

Items:

DateTime	Sunrise_Start       "Sunrise Start [%1$tH:%1$tM]"  { channel="astro:sun:home:rise#start" }
DateTime	Sunset_Start        "Sunset Start [%1$tH:%1$tM]"   { channel="astro:sun:home:set#start" }

Rule:

rule "Generate Time of Day Events"
when
	Time cron "8 * * * * ?"
then
	val now_ms = now.millis
	val sunrise = (Sunrise_Start.state as DateTimeType).calendar.timeInMillis
	val sunset = (Sunset_Start.state as DateTimeType).calendar.timeInMillis

	logInfo("Astro Rules", "Millis till sunrise: "+(now_ms-sunrise))
	logInfo("Astro Rules", "Millis till sunset: "+(now_ms-sunset))

	if ( (-1)*(now_ms - sunrise) < 60*1000 && (now_ms - sunrise) < 0 ) {
		logInfo("Astro Rules", "Sunrise was within the last 60s")
	}
	if ( (-1)*(now_ms - sunset) < 60*1000 && (now_ms - sunset) < 0 ) {
		logInfo("Astro Rules", "Sunset was within the last 60s")
	}
end

Hi all,

I see there has been an update which includes the offset functionality

The information is here.

If possible, could anyone post an example of the channel using the offset?

The example rule:

rule "example trigger rule"
when
    Channel 'astro:sun:home:rise#event' triggered START 
then
 
end

I ran it as per the example usng :home: but it didn’t trigger. I’ll try today changing :home: to :local: and see if it works.

My rule:

rule "Sunrise rule"
when
	Channel 'astro:sun:local:rise#event triggered START
then
   logInfo("Sunrise", "Sunrise rule trigerred!")
end

The event in my log registered as such:
2017-01-11 08:30:00.014 [ChannelTriggeredEvent ] - astro:sun:local:rise#event triggered START

I have nothing defined in the .items for this. Is that what’s missing maybe?

Any clarifications would be welcome, and thanks again for the awesome functionality :slight_smile: