Sun (raised / set) indicator

Hi Geeks,

I searched a lot, but didn’t find the one hint… even if it’s out there…?!

First of all I want to place a kind of KPI or light bulb on my sitemap to show if the sun is raised or not!

  • Sun raised -> bulb = ON
  • Sun not raised/or sun set -> bulb = OFF

…and later I want to draw this “switch” in a chart (Off = 0 , ON = 100).
I’m not sure where the calculation of ON -> 100 has to be done: items, rules, or somewhere else??

somebody out there with a hint or who could help me, please!


The Astro Binding will get you started in producing a switch that turns on and off for sunrise and sunset. A persistence tool like mySQL can be used to show graphs of switch items easily.

Hy Ben,

thanks for your advice. So, my fault, I didn’t mention that I’m familiar with creating charts (persistance rrjd4).
But how to setup the switch “listening” to the sun??

And how to bring the “switch-status” on a chart?

thx, anyway!

Well the 1.9 Astro binding has two event switches that are useful to you, we’ll setup a third and fourth item for simplicity later:

Switch    Sunrise_Event   "Sunrise Event"    {astro="planet=sun, type=rise, property=start, offset=0"}
Switch    Sunset_Event    "Sunset Event"     {astro="planet=sun, type=set, property=end, offset=0"}

Switch    Sun_Is_Up       "The sun is up"
Number    Sun_Chart       "Sun Chart"

These will switch on and then immediately off when triggered, so you can use this to trigger your Sun_Is_Up Switch; as well as this, I don’t believe rrjd4 can persist non-numerical items (someone please correct me if I’m wrong), so for clarity we can update a separate item for a chart. We’ll make two rules for this:

rule "When Sunrise Happens"
	Item Sunrise_Event received update ON

rule "When Sunset Happens"
	Item Sunset_Event received update ON

Even after sunset, depending where you are on the planet you may not be in darkness until a bit later; If you want to trigger when it would usually be dark, you can put an arbitrary delay in minutes in the item for the event, I’ve left it at 0 in the example above.

1 Like

Hi Ben,

this sounds very good, and is showing me my misstakes!

Just ome last question… because rules are new to me!
Is it possible to keep both shown rules in a single .rules file???
Or must they beeing seperated in different files?

again, thx

Yep, you can have any number of rules in a rule file, in this the import, var, rule order for example:

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*

var String sharedVar = "This string can be read and changed by any rule in this file!"
var Number testNumber

rule "Rule A"
	Item Item_One received update
	testNumber = Item_One.state as DecimalType

rule "Rule B"
	Item Item_Two changed from ON to OFF
	var String localVar = "This string can only be used in this rule"
	localVar = sharedVar

If in doubt, use the openhab designer as it will show you where you’re going wrong for rules.

There were some changes with openHAB2 and the sunrise / sunset events have been left out for yet another feature which is sadly not finished yet.

That said, here’s a workaround you may use for now:


astro:sun:home  [ geolocation="50.12345,10.12345", interval=300]
astro:moon:home [ geolocation="50.12345,10.12345", interval=300]


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)) [ |
        logInfo("RULE", "--> astro init")
        if (now.isAfter((SunsetTime.state as DateTimeType).calendar.timeInMillis) ||
            now.isBefore((SunriseTime.state as DateTimeType).calendar.timeInMillis)
        ) {
            logInfo("RULE", "--> Night_State ON")
            postUpdate(NightState, ON)
        } else {
            logInfo("RULE", "--> Night_State OFF")
            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)

With this combination you will get an item “NightState” which is ON at night and OFF by day. The first rule is needed to set NightState after openHAB was started or reloaded, the second will switch NightState at sunrise/sunset.

In yet another rule you can now react on the item NightState. You can either trigger a rule at sunrise or sunset or use NightState inside a rule as a condition.


rule "Night has started"
    Item NightState changed to ON
    logInfo("RULE", "It's getting dark!")

rule "Bathroom door opened"
    Item BathDoor changed to OPEN
    if (NightState.state == ON) {
        sendCommand(BathLight, ON)
1 Like

Thank you!
I’ll go for it and test it…

@SirMaulwurf: I just realized that the logic was wrong in the above code (NightState ON at day). That’s fixed now.

Sorry - i worked on this code for all the afternoon… but no results. No way to trigger light power on on sunset.
Any suggestion about what to check?

If you are coming from openHAB1: use your old rules and install the Astro binding for version 1.
Works like a charm … :slight_smile:

I started some day ago with openhab2, never used version 1 :confused:

tnx anyway

Hello Paglia

Set the Switch NightState on your sitemap.
Then you can Test, if your rule works manually (by switch it) and if the Update NightState-Rule is fireing.

Thanks everybody, the “nighstate” solution works like a charm. Sorry for my late reply.

@ThomDietrich in your example how can i insert a time offset? For example, to trigger the rule 30 minutes after sunset?

Astro 2 binding is now supporting events and offsets natively:

Wow this is great! OH2 is growing fast!
After your advice i tested a sunrise/sunset configuration, like this:


DateTime Sunrise_Time “Sunrise [%1$tl:%1$tM %1$tp]” {astro=“planet=sun, type=rise, property=start”}
DateTime Sunset_Time “Sunset [%1$tl:%1$tM %1$tp]” {astro=“planet=sun, type=set, property=end”}
Switch Sunrise_Event {astro=“planet=sun, type=rise, property=start, offset=0”}
Switch Sunset_Event {astro=“planet=sun, type=set, property=end, offset=0”}


rule "Example Rule at sunrise"
Item Sunrise_Event received update ON
sendCommand(Switch_Landscape, OFF)

rule "Example Rule at sundown"
Item Sunset_Event received update ON
sendCommand(Switch_Landscape, ON)

no idea why, rule is not fired ans unsrise/sunset.
But if i manually trigger switches, works.

You can’t bind events to switch items anymore, you have to directly use them in your rules:

rule "Example Rule at sunrise"
       Channel 'astro:sun:home:rise#event' triggered START 
       sendCommand(Switch_Landscape, OFF)

uh… i suppose i should start reading release notes instead of old forums… 2017 resolution!
I’ll use “triggered”, and give a feedback.

thank you

1 Like

works! Sunrise event triggered!