Timer - start when an switch turned on before executing the rule

Tags: #<Tag:0x00007f386dbaa7f0>

OpenHab 2, Snapshot 2.5 on a PI3+, z wave devices!

I want to create a rule: when bedroom light is turned on between 21:00 and 23:00, for the next 15 minutes (timer) pause before turning off the whole house lights.

I have this:

rule “Going_to_Bed”
when
Time cron “0 0/15 21-23 * * ?”
then
if (Light_FF_Bed_Wall.state == ON)
{
Light_FF_Bath_Vanity.sendCommand(OFF)
Light_FF_Dining_Wall.sendCommand(OFF)
Light_FF_Kitchen_Sink.sendCommand(OFF)
Light_LR_West_Master.sendCommand(OFF)
Light_FF_Bed_Wall.sendCommand(OFF)
}
else{}
end

But the above checks that status every 15 minutes not really creating a timer. The expiring binding is set for seconds, is there a problem calculating it out to 15 minutes? Or is there a more proper way?

First, please use code fences

Second:

rule “Going_to_Bed”
when
    Item Light_FF_Bed_Wall changed to ON
then
    if (now.getHourOfDay() >= 21 && now.getHourOfDay() < 23) {
        createTimer(now.plusMinutes(15), [ |
            Light_FF_Bath_Vanity.sendCommand(OFF)
            Light_FF_Dining_Wall.sendCommand(OFF)
            Light_FF_Kitchen_Sink.sendCommand(OFF)
            Light_LR_West_Master.sendCommand(OFF)
            Light_FF_Bed_Wall.sendCommand(OFF)
        ])
    }
end

How that works.
As you said yourself, you want to detect if the bedroom light is turned ON so that’s the rule trigger
Then we check if the hour of the day is >= 21 or < 23
Then we create a timer that will run in 15 minutes time containing the commands to turn the lights OFF

Enjoy

Thanks Vincent but I still haven’t successfully created a working “delay” timer. I have spent soooo many hours reading, copy & pasting, adding and subtracting characters, nothing is working.
Simply put = When a light switch is detected ‘switching off’, between a certain time of day, turn off another light, then wait 10 seconds to turn off other lights. See this code:

// Home.Rules
var Timer timer = null

rule “Going_to_Bed”
when
Time cron “0 0/1 21-23 * * ?”
then
if (Light_Bed_Wall.state == ON)
{
Light_Porch_Light.sendCommand(OFF)
Light_Deck_Light.sendCommand(OFF)
Light_East_Door.sendCommand(OFF)
timer = createTimer(now.plusSeconds(10)) [ |
Light_West_Door.sendCommand(OFF)
Light_Bed_Wall.sendCommand(OFF)
]
}
else{}
end

All the functions after the timer do not work…
Please help b4 I Lose more hair

Your rule trigger is wrong. You say it yourself “When a light is detected switching off”. That’s your trigger. Then in the rule you check for the hour of day then you do your commands. Your commands bit is correct.

rule “Going_to_Bed”
when
    Item Light_Bed_Wall changed to OFF
then
    if (now.getHourOfDay() >= 21 && now.getHourOfDay() < 23) {
        Light_Porch_Light.sendCommand(OFF)
        Light_Deck_Light.sendCommand(OFF)
        Light_East_Door.sendCommand(OFF)
        createTimer(now.plusSeconds(10), [ |
            Light_West_Door.sendCommand(OFF)
            Light_Bed_Wall.sendCommand(OFF)
        ])
    }
end

Thank you for your quick response, sad to say I copied and pasted your code (exactly) and changed the time 21 to 13 since it’s 3:30PM here AND it does not work! I am nearly bald!!!

Forgot to say nothing turns off like rule is not working at all!

In that case add some logging and check what does and what doesn’t work in your logs:

rule “Going_to_Bed”
when
    Light_Bed_Wall changed to OFF
then
    logInfo("Test", "Light bed wall changed to off")
    if (now.getHourOfDay() >= 21 && now.getHourOfDay() < 23) {
        logInfo("Test", "Light bed wall: time between 21 and 23")
        Light_Porch_Light.sendCommand(OFF)
        Light_Deck_Light.sendCommand(OFF)
        Light_East_Door.sendCommand(OFF)
        logInfo("Test", "Light bed wall: setting timer")
        createTimer(now.plusSeconds(10), [ |
            logInfo("Test", "Light bed wall: timer expired")
            Light_West_Door.sendCommand(OFF)
            Light_Bed_Wall.sendCommand(OFF)
        ])
    }
end

Rolf, As soon as I load your version of the rule using visual studio without running it I get the following

2019-02-17 22:16:51.252 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘home.rules’ has errors, therefore ignoring it: [4,6]: no viable alternative at input ‘“’

Item is missing in the trigger statement

Sascha, can you be more specific? I am new. Maybe fix the rule?

OK, I found it! Rolf There was a missing term “Item” in the when statement. Thank you the rest of the rule works great.

Ah yeah it was late last night already :slight_smile: Great you got it working.