Astro binding to move switches at sunset/sunrise

My objective is to turn two switches ON at sunset OFF at sunrise.


Switch RELAY1 "Front Can Lights" { gpio="pin:2" }
Switch RELAY2 "South Flood Lights" { gpio="pin:3" }
Switch RELAY3 "Relay 3" { gpio="pin:4" }
Switch RELAY4 "Relay 4" { gpio="pin:17" }
Switch RELAY5 "Relay 5" { gpio="pin:27" }
Switch RELAY6 "Relay 6" { gpio="pin:22" }
Switch RELAY7 "Relay 7" { gpio="pin:10" }
Switch RELAY8 "Relay 8" { gpio="pin:9" }

// Sunrise & Sunset

DateTime SunsetTime "Sunset [%1$tH:%1$tM]" <sun> (Astro) { channel="astro:sun:home:set#start" }
DateTime SunriseTime "Sunrise [%1$tH:%1$tM]" <sun> (Astro) { channel="astro:sun:home:rise#end" }
Number   SunElevation  "Elevation [%.1f �]"  <sun>  (Astro) { channel="astro:sun:home:position#elevation" }
Switch   NightState    "Night"


rule "OpenHAB system started - astro"
    System started
    createTimer(now.plusSeconds(180)) [ |
        if (now.isAfter((SunsetTime.state as DateTimeType).calendar.timeInMillis) ||
            now.isBefore((SunriseTime.state as DateTimeType).calendar.timeInMillis)
        ) {
            postUpdate(NightState, ON)
        } else {
            postUpdate(NightState, OFF)

rule "Update NightState"
    Item SunElevation changed
    if(SunElevation.state >  0){
        if(NightState.state != OFF) postUpdate(NightState, OFF)
    } else {
        if(NightState.state != ON) postUpdate(NightState, ON)

rule "Night has started"
    Item NightState changed to ON
    logInfo("RULE", "Night has started!")
    sendCommand(RELAY1, ON)
    sendCommand(RELAY2, ON)

rule "Night has ended"
    Item Nightstate changed to OFF
    logInfo("RULE", "Night has ended!")
    sendCommand(RELAY1, OFF)
    sendCommand(RELAY2, OFF)


astro:sun:home  [ geolocation="36.15,-95.99", interval=60]
astro:moon:home [ geolocation="36.15,-95.99", interval=60]


2016-12-25 07:36:05.835 [ItemStateChangedEvent     ] - SunElevation changed from -0.61 to -0.43
2016-12-25 07:37:05.844 [ItemStateChangedEvent     ] - SunElevation changed from -0.43 to -0.25
2016-12-25 07:38:05.837 [ItemStateChangedEvent     ] - SunElevation changed from -0.25 to -0.08
2016-12-25 07:39:05.839 [ItemStateChangedEvent     ] - SunElevation changed from -0.08 to 0.10
2016-12-25 07:39:05.865 [ItemStateChangedEvent     ] - NightState changed from ON to OFF
2016-12-25 07:40:05.835 [ItemStateChangedEvent     ] - SunElevation changed from 0.10 to 0.27
2016-12-25 07:41:05.836 [ItemStateChangedEvent     ] - SunElevation changed from 0.27 to 0.45
2016-12-25 07:42:05.839 [ItemStateChangedEvent     ] - SunElevation changed from 0.45 to 0.62
2016-12-25 07:43:05.840 [ItemStateChangedEvent     ] - SunElevation changed from 0.62 to 0.80


2016-12-24 21:15:01.850 [INFO ] [arthome.ui.paper.internal.PaperUIApp] - Started Paper UI at /ui
2016-12-24 21:15:02.054 [INFO ] [panel.internal.HABPanelDashboardTile] - Started HABPanel at /habpanel
2016-12-24 21:15:02.252 [INFO ] [ui.habmin.internal.servlet.HABminApp] - Started HABmin servlet at /habmin
2016-12-24 21:15:04.343 [INFO ] [ding.astro.handler.AstroThingHandler] - Scheduled astro DailyJob at midnight for thing astro:moon:local
2016-12-24 21:15:04.349 [INFO ] [ding.astro.handler.AstroThingHandler] - Scheduled astro DailyJob at midnight for thing astro:sun:local
2016-12-24 21:15:05.805 [INFO ] [ding.astro.handler.AstroThingHandler] - Scheduled astro DailyJob at midnight for thing astro:sun:home
2016-12-24 21:15:05.814 [INFO ] [ding.astro.handler.AstroThingHandler] - Scheduled astro PositionalJob with interval of 60 seconds for thing astro:sun:home
2016-12-24 21:15:05.850 [INFO ] [ding.astro.handler.AstroThingHandler] - Scheduled astro DailyJob at midnight for thing astro:moon:home
2016-12-24 21:15:06.675 [INFO ] [.eclipse.smarthome.model.script.RULE] - Night has started!
2016-12-24 21:15:06.686 [WARN ] [ding.astro.handler.AstroThingHandler] - The Astro-Binding is a read-only binding and can not handle commands
2016-12-24 21:15:06.698 [WARN ] [ding.astro.handler.AstroThingHandler] - The Astro-Binding is a read-only binding and can not handle channel updates
2016-12-24 21:15:06.701 [WARN ] [ding.astro.handler.AstroThingHandler] - The Astro-Binding is a read-only binding and can not handle commands
2016-12-24 21:15:06.712 [WARN ] [ding.astro.handler.AstroThingHandler] - The Astro-Binding is a read-only binding and can not handle channel updates
2016-12-25 10:27:23.902 [WARN ] [ding.astro.handler.AstroThingHandler] - The Astro-Binding is a read-only binding and can not handle commands
2016-12-25 10:27:23.909 [WARN ] [ding.astro.handler.AstroThingHandler] - The Astro-Binding is a read-only binding and can not handle channel updates
2016-12-25 10:27:24.661 [WARN ] [ding.astro.handler.AstroThingHandler] - The Astro-Binding is a read-only binding and can not handle commands
2016-12-25 10:27:24.668 [WARN ] [ding.astro.handler.AstroThingHandler] - The Astro-Binding is a read-only binding and can not handle channel updates

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

This will give an ON with no duration, means it will switch to OFF right after the ON.
I you need a steady ON/OFF you can use the above rule and use a proxy item.

Is there a way to use the offset in the 2.0 binding?

Not yet.

As far as I remember from the discussion on the pull request, @gerrieg is working on it, but those things take time :smile:

Yes, but only delays, see this post regarding the expire binding.

I was now not just looking for a delay.:grinning:

But you have at least 50% of your desired offsets … :slight_smile:

Event offsets are available with this PR

1 Like

I need to download it manually?
Or is automatically when I updated Openhabian?

Is there an example how to use this channel?


After doing a:

sudo apt-get update
sudo apt-get upgrade

Mine appears to be upgrading to the latest snapshot …That is potentially hazardous though, so certainly you would be doing so at your own risk.


How are the offsets used … in the example on github there is something like:

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

Then it says:
Note: Offsets for each event group can be configured in the channel properties

OK … but where … can this be done in the textual configuration files (oh please let it be so) & if so how & what is the syntax?

Is it in the above like (Note the following DOES NOT WORK!)

    Channel 'astro:sun:home:rise#event,offset=-5' triggered START

or something else?

Trying, learning, but running out of brain,

Haven’t found out yet how to do it with textual files …

To use the offset, you need the latest snapshot, in beta 5 there are only the events but not the delay included.

To configure a delay, you have to do this with PaperUI on the channel (click the pencil on the right)

But i also don’t know how to configure a channel config via config files. Maybe someone from the openHab experts can tell us.
@Kai any hint for us?

@sihui & @gerrieg :

Thank you! I do (and did) see that in the paper UI. Even so I don’t have an example on how to reference this event vs. the non offset events. Also having a predefined number (there are 13 ?) is probably not the way to go either as I don’t see that working for vast population that we all hope end up using openHAB. It could easily be a limitation of the UI as it stands currently and the devs trying to get something out there that we can use (or more probably I’m just not very smart) but it seems to be a limitation that will bite us sooner rather than later. In any case, thank you for the info. I might set something up with this once I learn more … but I’m really hoping for some textual config. Perhaps Kai will have info along this line – or at least some alternative.

I did just find Defining Channels though and that may be more what I’m looking for.

Always Learning,

Great that offsets finally become available. Thanks for your link too, Bruce! Would be great if you can “duplicate” a channel in Paper UI though (and then set properties individually).

In addition to the sample rules I will provide my easy solution for a light which switches on at 6am and goes off a couple of minutes after sunrise completes. If sunrise is already completed by 6am, the light will not go on at all.

import org.joda.time.*
import org.openhab.core.library.types.*

var Timer tMorningLightOff

rule "ruleMorningLightOnAndOff"
	Time cron "0 0 6 ? * MON-FRI"
	var DateTime dtSunRiseEnd = parse(astroSun1AmsterdamRiseEndDateTime.state.toString)
	if(now < dtSunRiseEnd){
		sendCommand(hueMediumGlobeDimmer, 30)
		tMorningLightOff = createTimer(dtSunRiseEnd.plusMinutes(15)) [|
			sendCommand(hueMediumGlobeDimmer, 0)
1 Like

In an attempt to solve this problem in a way that I actually understand, I thought, I know … I’ll just be create multiple locations that I can reference all along the same latitude. If the longitude that I reference is 15 degrees apart from my own, then the sunset times of the other location will be an hour different (360 degrees / 24hrs per day = 15 degrees per hour).

Warning (Danger Danger) - The below is not tested as of this post! Use at your own risk.

So in my things file (sanitized in this example below for my protection) I now have:

astro:sun:home [ geolocation=“45.000000,-80.000000”, interval=60 ]
astro:moon:home [ geolocation=“42.000000,-80.000000”, interval=60 ]

astro:sun:minus60 [ geolocation=“42.000000,-65.000000”, interval=60 ]
astro:sun:minus45 [ geolocation=“42.000000,-68.750000”, interval=60 ]
astro:sun:minus30 [ geolocation=“42.000000,-72.500000”, interval=60 ]
astro:sun:minus07 [ geolocation=“42.000000,-78.250000”, interval=60 ]
astro:sun:minus05 [ geolocation=“42.000000,-78.750000”, interval=60 ]
astro:sun:plus01 [ geolocation=“42.000000,-80.250000”, interval=60 ]
astro:sun:plus05 [ geolocation=“42.000000,-81.250000”, interval=60 ]
astro:sun:plus10 [ geolocation=“42.000000,-82.500000”, interval=60 ]

Then if I want to turn on a light, say 60 mins before sunset I can simply:

//  Stairwell Lights On at Sunset - 1 hour.
rule "Stairwell Lights On - Sunset - 1 hour"
    Channel 'astro:sun:minus60:set#event' triggered START    
    sendCommand(swStairway, ON)

It looks like this should work out well until I can figure out how to do the offsets in a file. In the end I might not want them there but I don’t understand how to use them as they are - or even how to reference them if I define them.



How are you differentiating whether this is minus 60 from sunrise or sunset?

It’s the sunset in this case, otherwise it would be

Channel 'astro:sun:minus60:rise#event' triggered START

That’s a very clever hack! Will try this for my event to switch on the living lights 30 minutes before sunset.

1 Like

Ugh… sorry. Kind of obvious. Chalk it up to reading too quickly.