Rule to turn light when door opened based on time of day

  • Platform information:
    • Hardware: RP3 Openhab Server
    • OS: Raspian
    • openHAB version: 2.4.0-1

I have created a rule to turn on my outside light when the side door of my garage is opened and stay on for 10 minutes. I only want this to occur between certain hours such as 5pm and 7am.

This is the current version of my rule but it doesn’t seem to be functioning correctly.

//Turn on Outside Light for 10 mins when Access Door is opened
rule "Outside Light Auto On/Off Access Door"
when
Item GarageAccessDoorStatus received update OPEN
then
    if ((hour <=7) || (hour >= 17))
    Sonoffth16.sendCommand(ON)
    timer = createTimer(now.plusMinutes(10)) [|
        Sonoffth16.sendCommand(OFF)
                     timer = null
    ]
end

Can anyone see where I’m going wrong here?

Thanks

Apart from the brackets around everything within the if statement (if(...){...}) , I would want to know where and how you define hour.

Ahh yea good point. At the top of my rules file I have the following.

var Number  hour = now.getHourOfDay

I’ll try adjusting the brackets.
Thanks

In that case, the variable hour will only be evaluated once the rules file gets parsed, so you should put the variable inside the rule to make it work.

Does this really gives you the expected value?

var Number  hour = now.getHourOfDay

I use:

val DateTime aktuelleZeit = now
val Number  hour = aktuelleZeit.getHourOfDay

Not sure if getHourOfDay can be used without initialize with now.

Yes it can, that is no problem whatsoever.

Your right, it work

I think the problem is very easy. Try

if ((hour <=7) || (hour >= 17))
{
    Sonoffth16.sendCommand(ON)
    timer = createTimer(now.plusMinutes(10)) [|
        Sonoffth16.sendCommand(OFF)
                     timer = null
    ]
}

I think the syntax of if was used wrong. If no {} are used only the first line after the if is in the if.

In your version the timer is running even if the time is out of range if the if clause.

No, the fundamental problem, as I already indicated, that the structure should be this:

rule ""
when ...
then
   var Number  hour = now.getHourOfDay
    if(){
        ...
    }
end

This worked! Thank you…much appreciated.

Kind Regards