OpenHab Automation: Turn on and off lights based on sunset and sunrise

So, for quite some time now I have been wanting to automate the exterior lights of our house. OpenHAB2 (OH2) lets you do this quite smoothly by using the Astro binding.

Start off by fetching the Astro Binding via the OH2 controlpanel. Configuration > Bindings.
Click the Plus icon, and select Bindings in the top table – searching for Astro will reveal the binding you want (I am currently on binding-astro – 2.4.0). Select Install, and let it finish. We will now continue in the configurationfiles.

In your Openhab-Conf folder, under Things, create an empty file and call it astro.things.
In this file, at a bare minimum, you should put in this string:

astro:sun:home [ geolocation="10.12345678,5.12345678", interval=300]

The first three statements are calling the Astro function, the SUN object, for the HOME location. The two statements in the square brackets are geolocation and polling interval – here you need to find your longtitude and latitude from a map site (google maps is fine), and replace the digits in the example above.
A pollinginterval of 300 seconds is sufficient for my own environment, and I rarely need to know where the sun is for as often as every 5 minutes – but feel free to adjust this at your own need.

Save the file, and see that you dont receive any errors in your logs.

Next up, head on over to your items folder. Here we will add the sun-items based on the astro thing value you just created.

Create an empty file, call it astro.items. Here you will add three strings:

DateTime    Current_DateTime     "Today [%1$tA, %1$td.%1$tm.%1$tY]"                <clock>  (Astro) {channel="ntp:ntp:local:dateTime"}
DateTime    Sunset_Time          "Sunset [%1$tH:%1$tM]"                            <sun>    (Astro) {channel="astro:sun:home:set#start"}
DateTime    Sunrise_Time         "Sunrise [%1$tH:%1$tM]"                           <sun>    (Astro) {channel="astro:sun:home:rise#start"}

The first item tells your environment based on your geolocation what time it is right now.
The second item tells you when the sun sets.
The third item tells you when the sun rises.

Based on the second and third item, we can create rules that, in this example, turns our porch- and streetlight on and off, based on the sunset and sunrise.

Head on over to your Rules folder, and create an empty file, calling it outside_lights.rules

The first rule will turn your lights on. It looks something like this:

rule "outside lights ON"

when
    Channel 'astro:sun:home:set#event' triggered START
then
    if (outside_lights.toggle.state == OFF)){
        logInfo("Outside_lights", "All lights are off, turning them on.")
        outside_lights.toggle.sendCommand(ON)
    }
    else {
        logInfo("Outside_lights", "All lights are on - doing nothing.")
    }
end

And for the rule that turns the lights off, we get something like this:

rule "outside lights OFF"

when
    Channel 'astro:sun:home:rise#event' triggered START
then
    if (outside_lights.toggle.state == ON)){
        logInfo("Outside_lights", "All lights are oN, turning them OFF.")
        outside_lights.toggle.sendCommand(OFF)
    }
    else {
        logInfo("Outside_lights", "All lights are off - doing nothing.")
    }
end

Save these in the same outside_lights.rules file, and save it. See that you don’t receive any errors in the log.

Remember to chagnge the rules to fit your light items – but this should be obvious 🙂

Now, if all is configured correctly, you will see that your lights will turn off and on, based on the sunrise and sunset in your area.

Happy automating!

-F