[SOLVED] RULE QUESTION: After sunset do this

Hey guys,

I have been playing with the astro binding and sunset events.

Im trying to create a rule that does this:

rule "Do stuff on button press if its after sunset"
    when

        Item alarm_switch received command ON

    then
        
        if sunset.started {

        do this
  
        } else {

        do this
     }

end

I have tried using formulas others have stated but no luck.

Thanks.

Assuming this is pseudocode since the syntax is pretty wrong.

See Design Pattern: Time Of Day for a comprehensive way to centralize all of you time calculations.

Even if you don’t adopt the DP, it will show how to do all the operations with Astro trigger channels and time comparisons you need.

Yeh it is indeed pseudocode. Thanks for the link.

EDIT. This might be a bit beyond me :confused:

Basically you need three rules and another item

The item:

Switch IsItDark

The rules:

rule "Sunset"
when
    Channel 'astro:sun:home:set#event' triggered START
then
    IsItDark.postUpdate(ON)
end

rule "Sunrise"
when
    Channel 'astro:sun:home:rise#event' triggered START
then
    IsItDark.postUpdate(OFF)
end

rule "Do stuff on button press if its after sunset"
when
    Item alarm_switch received command ON
then
    if (IsItDark.state == ON) {
        // do this
    } else {
        // do that
    }
end
3 Likes

It may be for now.
My advice.
Read the DP and then copy the code. I don’t mean copy and paste. I mean write the code yourself, line by line.
I my experience this is still the best way to understand code.
Write it or copy it. Understand it. When you copy it line by line your understanding of it will come.

Good luck

2 Likes

Yes, thanks I will do that. I appreciate the help and advice. Its like I can picture the things that need to happen but I struggle with the details.