Sun (raised / set) indicator

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:

things/astro.thing

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

items/astro.items

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"

rules/astro.rules

rule "OpenHAB system started - astro"
when
    System started
then
    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)
        }
    ]
end
rule "Update NightState"
when
    Item SunElevation changed
then
    if(SunElevation.state >  0){
        if(NightState.state != OFF) postUpdate(NightState, OFF)
    } else {
        if(NightState.state != ON) postUpdate(NightState, ON)
    }
end

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.

Examples:

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

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