Reset timer in rule when switch is pressed again


I want to program a switch so that it is automatically switched off after a certain time (in my example 90 minutes) after being switched on.
rule “myswitch_in_livingroom”

    Item myswitch_power changed
    if (myswitch_power.state == ON) {
      createTimer(now.plusMinutes(90)) [
        myswitch_power.sendCommand(OFF) ]

The problem is that when I turn the switch on a timer starts counting 90 minutes, and this timer won’t be reset even when I turn to switch off and on again during there 90 minutes. So when I turn the switch off after 60 minutes and turn it on immediately again it will be turned off after 30 minutes by that first timer.
How can I reset the timer in the rule every time the switch is turned on?

Thank you very much!

I currently use the expire v1 addon with a virtual item for a timer. I hesitate to recommend this because it is likely v1 addons may not function with openHAB 3.

Use the expire binding for exactly this kind of thing

The OP is not running OH3 so it hardly matters.

This will be useful. As you are already using a rule and createTimer, you can modify that.
The trick is to keep a handle for the timer that you create, so that it survives between different runs of your rule. That will allow you to rechedule your timer when another trigger comes along.

True, but OH3 will be the next version, according to the blog. Only patches to addons are planned for 2.5.

I try the expire-addon for a quick solution and then some modifying in the rules after, thank you very much!

Well, when the best solution for OH3 is known then it will be a smart suggestion to use that in OH2 as well, if it is possible. Meantime, “Do nothing” isn’t really helpful today.

Which is why your link in included a solution that does not use the Expire binding… I do d not have that link handy. (I corrected a typo in the original.)

var Timer occupancyTimer = null

rule "MotionDetector1 received ON"
    Item MotionDetector1 received update ON
    if(occupancyTimer === null || occupancyTimer.hasTerminated()) {
        occupancyTimer = createTimer(now.plusMinutes(5), [|
            occupancyTimer = null
    else {
        occupancyTimer.reschedule(now.plusMinutes(timeoutMinutes ))

There is a drop in replacement for Expire 1.x submitted to the helper libraries. I don’t think there should be any hesitation to recommend using Expire for now and until whatever the OH 3 replacement for it will be. Even if Expire 1.x doesn’t run on OH 3 (which I think is a pretty much a done deal, the AC voted on it’s removal) the exact same functionality will be available.

There will also be a way to run 1.x bindings in parallel with OH 3 using MQTT event bus so there is yet another way one can continue to use Expire.

Recommending the use of Timers in Rules has the same problem. Joda DateTime is going away so everyone is going to have to change their Rules code to use java.util.DateTime at some point too.

There is no recommendation one can make right now that isn’t going to require some rework when it comes to moving to OH 3. But OH 3 is at least a year away and we have no idea what Expire/Timers will look like in OH 3. “Ignore the problem and wait a year” isn’t a good recommendation either. Users need something that works now.

Once we know what the OH 3 replacement becomes, we will have a good recommendation. Until then the same thing we would have recommended for OH 2.5 should be what we recommend now.

1 Like