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

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"
Item sSunset_Event received update ON
logInfo(“DMM”,"Sunset_Event occurred ")
if(sOutsideLightFAutoDelay.state == OFF) sendCommand(Darkness, ON)

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

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


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 "Generate Time of Day Events"
	Time cron "8 * * * * ?"
	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")

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"
    Channel 'astro:sun:home:rise#event' triggered START 

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"
	Channel 'astro:sun:local:rise#event triggered START
   logInfo("Sunrise", "Sunrise rule trigerred!")

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:

PaperUI -> Configuration -> Things -> select Astro sun data channel and fill in your offset value under “Range event”

Exactly what I was looking for thank you sir.

I will set up a test for sunset :slight_smile:

Always the place to look:

True. It was my brain that totally failed to make the connection to PaperUI for channel properties :blush:

Still to have the original question answered, it would be interested to know how the offset can be set in a config file. @sihui any input on that?

No, haven’t it figured out yet …
If Kai and Gerhard don’t know, who else should know it?:grinning:


Hello all,

Got stuck with a syntax error while copied Marks example:

22:11:55.083 [INFO ] [marthome.event.ItemStateChangedEvent] - SunriseStart_Time changed from NULL to 2017-02-11T08:05:00.000+0100
22:11:55.100 [INFO ] [home.event.ItemChannelLinkAddedEvent] - Link 'SunriseStart_Time-astro:sun:local:rise#start' has been added.
22:11:55.115 [INFO ] [marthome.event.ItemStateChangedEvent] - SunsetStart_Time changed from NULL to 2017-02-11T17:41:00.000+0100
22:11:55.134 [INFO ] [marthome.event.ItemStateChangedEvent] - SunsetStart_Time changed from NULL to 2017-02-11T17:41:00.000+0100
22:11:55.139 [INFO ] [home.event.ItemChannelLinkAddedEvent] - Link 'SunsetStart_Time-astro:sun:local:set#start' has been added.
22:11:55.155 [INFO ] [marthome.event.ItemStateChangedEvent] - DateT changed from 2017-02-11T22:11:03.051+0100 to 2017-02-11T22:11:55.096+0100
22:11:55.165 [INFO ] [marthome.event.ItemStateChangedEvent] - CurrentTime changed from NULL to 2017-02-11T22:11:55.096+0100
22:11:55.170 [INFO ] [marthome.event.ItemStateChangedEvent] - LocalTime changed from 2017-02-11 22:11:41 CET to 2017-02-11 22:11:55 CET
22:11:55.172 [INFO ] [home.event.ItemChannelLinkAddedEvent] - Link 'CurrentTime-ntp:ntp:local:dateTime' has been added.
22:11:55.204 [INFO ] [marthome.event.ItemStateChangedEvent] - MoonPhase changed from NULL to FULL
22:11:55.224 [INFO ] [marthome.event.ItemStateChangedEvent] - SunriseStart_Time changed from NULL to 2017-02-11T08:05:00.000+0100
22:11:55.251 [INFO ] [marthome.event.ItemStateChangedEvent] - DateT changed from 2017-02-11T22:11:55.096+0100 to 2017-02-11T22:11:55.133+0100
22:11:55.261 [INFO ] [marthome.event.ItemStateChangedEvent] - CurrentTime changed from 2017-02-11T22:11:55.096+0100 to 2017-02-11T22:11:55.133+0100
22:12:03.072 [INFO ] [marthome.event.ItemStateChangedEvent] - DateT changed from 2017-02-11T22:11:55.133+0100 to 2017-02-11T22:12:03.051+0100
22:12:03.081 [INFO ] [marthome.event.ItemStateChangedEvent] - CurrentTime changed from 2017-02-11T22:11:55.133+0100 to 2017-02-11T22:12:03.051+0100
22:12:03.092 [INFO ] [marthome.event.ItemStateChangedEvent] - LocalTime changed from 2017-02-11 22:11:55 CET to 2017-02-11 22:12:03 CET
22:12:04.271 [INFO ] [thome.model.script.time-of-day.rules] - CurrentTime received update
22:12:04.271 [INFO ] [thome.model.script.time-of-day.rules] - CurrentTime received update
22:12:04.279 [INFO ] [thome.model.script.time-of-day.rules] - CurrentTime received update
22:12:04.370 [WARN ] [.rule.jvmmodel.RulesJvmModelInferrer] - Duplicate field: 'CurrentTime'. Ignoring 'org.eclipse.smarthome.core.library.items.DateTimeItem'.
22:12:04.483 [WARN ] [.rule.jvmmodel.RulesJvmModelInferrer] - Duplicate field: 'CurrentTime'. Ignoring 'org.eclipse.smarthome.core.library.items.DateTimeItem'.
22:12:17.658 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'astro.rules'
22:12:17.671 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'astro.rules' is either empty or cannot be parsed correctly!
22:12:17.805 [WARN ] [.rule.jvmmodel.RulesJvmModelInferrer] - Duplicate field: 'CurrentTime'. Ignoring 'org.eclipse.smarthome.core.library.items.DateTimeItem'.

Where can I find the linenr, characternr, variable, … which is causing the syntax error?
Using Openhab Designer, though only having ‘could not resolve issues’-errors.

In my opinion the items file in Openhab2 should be replace by the items editor of the PaperUI. But in my opinion there are some looses en while editing/deleting existing items.

Though after looking a little further in the logs, and after adding some LogInfo-s the rule is being run, though sunset/rise I will see tomorrow (I hope).

Actually that was a workaround because at that time the astro2 binding did not support events.
That has changed and can now simply be accomplished by:

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

Which reminds me that I should put a note at the top of my post indicating that the Astro binding now has this functionality.


The syntax for adding the offset in the config files is now documented in the same place too:

astro:sun:home [ geolocation="xx.xxxxxx,xx.xxxxxx", altitude=100, interval=60 ] {
        Type rangeEvent : rise#event [

Oddly though, I can’t see the offset option in Paper UI (using snapshot from 10th Feb), just start, end and duration

It’s called “Range Event”, not “Offset” or something like that …

All: thanks for your input!
I will give it another try.

Though one questions remain:

Now that offset’s are supported would something like this work?

rule "sunrise_offset_30"
Channel “astro:sun:home:rise#event, offset=30” triggered START
logInfo(“Sunrise”, “Sunrise rule triggered 30 minutes after sunrise”)

No, see the examples in the docs for proper setup:

1 Like

thanks, that helped a bunch, I got all my rules re-factored for OH2 now.

1 Like