[SOLVED] Simple Rule - but i am rusty

hey guys, please help me out - i thinks its an easy task but didnt find some quick-infos.
i want to turn ON my garden light at sunset or 9 pm and turn the lights OFF at 01:00 am.

this is my rule so far - do i need to make another rule to turn it off ?
i think it should work just in 1 rule, but dont get it…

Thanks!

rule "Gartenbeleuchtung einschalten"
    when
        Item NightState received update
    then    
        if (NightState.state == "ON")
        {
            HK_IT02_Gartenlampen.sendCommand(ON)
        }
    else
        if (NightState.state == "OFF")
        >>>> how to get a specific time in here ? <<<<
        {
            HK_IT02_Gartenlampen.sendCommand(OFF)
        }
end

I’d use two rules. One triggered by nightfall (changed to ON), one triggered at 01:00 (cron).

If you must, you could trigger a single rule from both conditions and use if-else to determine what action to take, but there’s no benefit in that.

1 Like

How is NightState updated?

I think th syntax is wrong. I use this one:

if (NightState.state == ON)

State is not a string in therefore do not compare with a string “ON”

1 Like

As Vincent indicates, we can’t really answer until we know how/when NightState gets updated.

If it changes to ON at 9pm and OFF at 1am then the Rule is a simple one liner:

rule "Gartenbleuchtung einschalten"
when
    Item NightState changed
then
    HK_IT02_Gartenlampen.sendCommand(NightState.state)
end
1 Like

my NIGHTSTATE rule

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

thanks for the hint!

OK, since NightState has nothing to do with 9pm and 1am then you should use two Rules, a cron triggered rule that triggers at 9pm and sets the lamp to ON and another that triggers at 1am that sets the lamp to OFF. NightState has nothing to do with it because NightState doesn’t change based on time but based on sun position. NightState will not turn on at 9pm so we can’t use it to turn on the lamp and it doesn’t turn off at 1am so we can’t use it to turn off the lamp.

1 Like

thats correct, but when sunset is before 9pm it should trigger the lamps to ON as well :wink:
this is why i want to use the nightstate + cron

OK, you need a separate Rule with a cron trigger that goes off at 1am to turn off the light.

I’d also change the first Rule trigger to Item NightState changed to ON.

See https://www.openhab.org/docs/configuration/rules-dsl.html#defining-rules for how to make a cron triggered Rule.

You might benefit from Design Pattern: Time Of Day to centralize your time based calculations.

1 Like

Can’t you use the astro binding for that problem. Using a trigger-channel with an latest offset should do the trick.

thanks guys for the help! - thats my result for now - and i think this will do the trick

rule "Gartenbeleuchtung einschalten"
    when
        Item NightState received command ON or
        Time cron "0 0 21 * * ?" // Jeden Tag um 21 Uhr Nachts
    then    
        HK_IT02_Gartenlampen.sendCommand(ON)
end

rule "Gartenbeleuchtung ausschalten"
    when
        Time cron "0 0 1 * * ?"  // Jeden Tag um 1 Uhr Nachts
    then    
        HK_IT02_Gartenlampen.sendCommand(OFF)
end

changed to ON - or - received command ON !? whats the better choice ?

what a nice solution - i will give it a try - thanks for this!

It depends. Your Rule that updates NightState uses postUpdate so received command would never trigger the Rule. That is why I used changed to.

With postUpdate and your original “Update NightState” rule NightState gets updated every time SunElevation changes. But you don’t really care if NightState updated to control your light, you only care when it changes. And when you break the Rules up into two, you only care when it changes to ON.

1 Like

i’ve tested it and works well - thanks to all!