Rule to check if a light switch is ´kept on for too long

Anyone have an idea for a rule which checks if a light is turned on for i.e. > 15 min - and then act if it is ? The action could be turning the light off - or sending a tweet - or something else…

I’d start a timer when the switch is turn on.
When the timer is run for more then xx do your stuff. If the switch is turned off by any other means reset the timer.
For a timer example look herehttps://github.com/openhab/openhab/wiki/Samples-Rules

val Timer lightOn = null

rule "Light turned on"
when
    Item Light changed
then
    if(Light.state == ON) {
        if(lightOn == null || lightOn.hasTerminated){
            lightOn = createTimer(now.plusMinutes(15), [|
                // light was left on too long
                lightOn = null
            ])
        }
        else {
            lightOn.reschedule(now.plusMinutes(15))
        }
    }

    else {
        if(lightOn != null && lightOn.hasTerminated){
            lightOn.cancel
        }
        lightOn = null
    }
end
1 Like