[SOLVED] Check the time after Sunset

Hi, guys

My OH grows up and I have new problem)))
I would like to turn on the light after sunset (with astro binding) but if anyone present in house (presence item I already have). Here is the code. It works well.

rule "Light ON on Sunset"
when
    Channel 'astro:sun:home:set#event' triggered START
then
    if (PresenceAnyone.state == ON) {
          gLight.sendCommand(OFF)
    }    
End

But if at sunset was nobody at home and he/she will come later the light must be also ON, how can i describe it? Yes i can make trigger that is sunset has already came and check it. But if OH restart and loose state of this item.
So I need to check something like this

CurrentTime >= 'astro:sun:home:set#event'

But how it will be right from the syntax side?

Or in other words how can I check at this moment if

 'astro:sun:home:set#event'

altready trigged or not (even if OH was offline in the time when trigger fired)?

Thanks a lot!

Trigger the Rule to run also when PresenceAnyone changes to ON.

You need to link the Channel to a DateTime Item and then see Rules | openHAB.

You might find [Deprecated] Design Pattern: Time Of Day useful.

1 Like

Create another Switch item:

Switch SunIsUp

Change your rule:

rule "Light ON on Sunset"
when
    Channel 'astro:sun:home:set#event' triggered START
then
    SunIsUp.postUpdate(OFF)
    if (PresenceAnyone.state == ON) {
          gLight.sendCommand(ON)
    }    
end

Another rule to reset the SunIsUp item

rule "Light ON on Sunset"
when
    Channel 'astro:sun:home:rise#event' triggered START
then
    SunInUp.postUpdate(ON)
end

And finally a presence rule:

rule "Someone came home"
when
    Item PresenceAnyone changed to ON
then
    if (SunIsUp.state == OFF) { //After sunset
        gLight.sendCommand(ON)
    }
end

This is a very simplified example of:

3 Likes

Thanks lot!
But the main problem here is if OH will be offline when trigger fired then SunInUp will not get right state till next day. Or am i wrong?

You are right.
openHAB should be running 24/7

1 Like

If you look at the Time of Day DP, you will see what it does is it triggers at all of the important times AND at System started. So if OH was down during one of the time events, the correct state still gets calculated when OH comes back up.

1 Like

It runns, but everything can happens…

So question is open.
This is really important problem, it can be used not only in this case, but in many others if OH goes offline.
How it can be solved or at least how it can be checked?

I saw it.
Ok, i will get it calculated, but how can i compare it or get trigged?

There are at least 3 examples of both triggering and comparing Asto date times to now in that design pattern.

1 Like

Thansk i will look at ti carefully!

I have made it. Thanks a lot fot help!
Now i know how to convert in Timestamp)))
But why do i get extra “000” at the end?

Item

DateTime SunSetHome  "SunSet [%1$tH:%1$tM]"  { channel="astro:sun:home:set#start" }

Rule


rule "Test"
when
    Time cron "0/10 * * * * ?"
then
    val sunRise = new DateTime(SunRiseHome.state.toString) 
    var sunriseMillis = sunRise.getMillis()
    logInfo("epoch", "SunRiseHome: " + SunRiseHome.state)
    logInfo("epoch", "SunRise: " + sunRise)
    logInfo("epoch", "Sunrise Millis: " + sunriseMillis)
end

i get

2019-01-25 09:50:00.032 [INFO ] [eclipse.smarthome.model.script.epoch] - SunRiseHome: 2019-01-25T08:15:00.000+0100

2019-01-25 09:50:00.039 [INFO ] [eclipse.smarthome.model.script.epoch] - SunRise: 2019-01-25T08:15:00.000+01:00

2019-01-25 09:50:00.047 [INFO ] [eclipse.smarthome.model.script.epoch] - Sunrise Millis: **1548400500000**

So i get Sunrise Millis: 1548400500000
It must be 1548400500

Yes i can cut it, but interesting why?

Thanks!

Understood, it it milisec, must be devided by 1000 :grinning: