Stopping / Delaying a timer

Hi all,

I’ve got a script that runs on a time of day, at a certain time of day, (midnight), it sends a message to display on my TV and then starts a 10 second timer to turn off the TV and then a 5 minute timer, (to allow the user to get upstairs etc), before doing all the other night time bits, turn down thermostat, turn on cameras etc.

rule "End of Day routine (Alexa / vTimeOfDay / Switch - Should Only run if vPresent = ON)"
when
	Item aEndofDay changed 
then
    if(aEndofDay.state == ON)
        {
        logInfo(logName, "End of Day routine - started")
        TV_LG_Toast.sendCommand("Switching on cameras in 5 minutes - Goodnight!")
        *Turn Down Thermostat*
        createTimer(now.plusSeconds(10),
            [
            *Turn Off TV command* 
            createTimer(now.plusMinutes(5),
                [
		        *Turn On Cameras command*
		        logInfo(logName, "End of Day Routine ON - Cameras ON")
                ])
            ])
        }
	else
        {
        logInfo(logName, "End of Day routine - ended")
        vIFTTTArlo_ONOFF.sendCommand(OFF)
		logInfo(logName, "End of Day Routine OFF - Cameras OFF")
        }
end

Now, this works absolutely perfectly so no problems there, but the wife is getting annoyed when we are still up after midnight on a weekend and she has to turn on the TV and cancel the cameras etc…

So, I’m thinking of having an Alexa command, (we use Alexa - and the openhab Alexa binding - extensively in our house), where someone can ‘Alexa, cancel goodnight’ within the 10 seconds before the TV turns off, (I may also extend the 10 seconds to 30 or something to give us more time), which will set a dummy variable to ON and then this will stop the timer.

However, I’ve never had to stop a timer on Openhab once I’ve started one so is this possible?

Ideally I’d like to delay the timer - or start a new one - so that the night time routine happens later and doesn’t need to be manually called.

Any thoughts, ideas or methods greatfully received…

Create another switch item called CancelTimerSwitch
Turn it on with alexa
Check the state of this item in the timer execution routine. If ON, cancel timer or reschedule, if OFF do the normal routine.

But can you do that once the timer has already started? How do you break into a timer?

You don’t. The timer will execute regardless. You can’t stop that. What you need to do is check for the state of another item in the time execution routine.

Ah ok, I was hoping to be able to break into the timer but I’ll just have the next command after a timer as a if item.state = ON then delay for an hour else OFF = continue with night time routine…

Thanks :+1:

You could do this.
Create another item CancelTimerSwitch

In your existing rule:

var Timer myTimer1 = null
var Timer myTimer2 = null

rule "End of Day routine (Alexa / vTimeOfDay / Switch - Should Only run if vPresent = ON)"
when
	Item aEndofDay changed 
then
    if(aEndofDay.state == ON) {
        logInfo(logName, "End of Day routine - started")
        TV_LG_Toast.sendCommand("Switching on cameras in 5 minutes - Goodnight!")
        *Turn Down Thermostat*
        myTimer1 = createTimer(now.plusSeconds(10), [ |
            *Turn Off TV command* 
            myTimer2 = createTimer(now.plusMinutes(5), [ |
                *Turn On Cameras command*
		logInfo(logName, "End of Day Routine ON - Cameras ON")
                myTimer2.cancel
                myTimer2 = null
            ])
            myTimer1.cancel
            myTimer = null
        ])
    } else {
        logInfo(logName, "End of Day routine - ended")
        vIFTTTArlo_ONOFF.sendCommand(OFF)
	logInfo(logName, "End of Day Routine OFF - Cameras OFF")
    }
end

rule "Cancel timer"
when
    Item CancelTimerSwitch changed to ON
then
    if (myTimer1 !== null) {
        myTimer1.cancel
        myTimer1 = null
        myTimer2.cancel
        myTimer2 = null
    }
    CancelTimerSwitch.postUpdate(OFF)
end

PS - NOT TESTED

Ah, now that I like! SImple and effective and gives plenty of room for flexibility if I wanted to cancel different timers…

Thanks!

you can also use timer.reschedule()

Have a var Timer outside of your rule (see code example from @vzorglub) and have a second rule to either cancel or reschedule the timer (myTimer.cancel() / myTimer.reschedule())

Hi,

Thanks all, couple more questions:

What are the reschedule() options / can someone point me to some online documentation on it…can’t find anything useful.

Is it best practice to do all timers this way, (ie var and then separate rule), or only if there is a need to cancel them?

There;'s only one option, the new time when you want the Timer to trigger.

If it’s a fire and forget Timer, I skip the global variable.

There is no need to cancel a Timer inside the lambda function called by the Timer. You can just set myTimerX to null.

Finally, a rhetorical question for you to ponder. Is there a way you can flip this automation around so that openHAB knows when to run it and does it at the right time based on events rather than requiring you to cancel it? Then this whole problem goes away.

Thanks, I guess I saw it done somewhre and though it was better to be safe than sorry.
That will save a few lines of code.

Hi, thanks for the answers…and for the rhetorical question…it’s the one I’ve been pondering, my current thoughts:

In essence it this is my ‘night time’ routine rule so if I could come up with a different trigger based on our movement then yes but the issue I have is that I can’t see one that is guaranteed enough over a simple time trigger. (Ps, it’s actually based on your excellent ToD rule that I ammeded to have a ‘BED’ time which triggers bedtime routines)…

I was going to set it so that it triggers when both our iphones go on charge but if one of us is away from home then that falls down, and sometimes we have both our phones charging in the evening so it will trigger too early.

I tried to work on a lack of movement from our hallway / landing motion sensors but our evening isn’t routine enough to guarantee regular motion / no motion.

I contemplated if TV = off and no motion after x minutes sort of rule but we don’t always have the TV on in the evening.

I don’t have room level presence detection and I’m pretty sure we’d forget to press a button to set it - plus I want it automated ‘set and forget’ style.

Anyone got any other ideas?

It’s very like the “presence detection” conundrum. There simply isn’t a single reliable method, so you make the most of what you have.

At the crudest, you might run a timer for “presence”. Any activity - motion, manual TV or lights on or off, door opening, phone detection - starts/restarts the timer. This works pretty well - if you can make enough sensing opportunities.

The basic approach can be enhanced of course - you could add “wasp in a box” features, or change the rules depending on ToD.

Yes, totally agree with that, which is why I plumped for the midnight rule as it’s only really a Friday or Saturday night that we’ve up past midnight so for my needs it’s the most reliable method to use…

I was going to work my way thorugh a full gambit of IF statements but I just want to keep it simple and avoid having extra steps that bring their own complications and errors.

I might add if ToD = BED, both are present and both our phones aren’t on charge to reduce the error slightly but I might just put up with my wife tutting at me…gives her something to moan about at least. :stuck_out_tongue_winking_eye:

The ‘is it nighttime’ question cannot be answered well enough by any algorithm whatsoever to satisfy all but those few people that consider any automation to be better than no automation.
You cannot read people’s mind, and you mustn’t extrapolate their behavior. Even if your ruleset-KI-machine learning genius code did correctly guess yesterday’s end because say I switched off the TV, today I might behave the same but will have changed my mind and want to stay up for another hour after switching off the TV to read a good book.

Let alone that it won’t work at all if there’s multiple persons to live there to be involved.

Sometimes simple solutions are best. I have established a scene switch that I triple-click when I go to bed.

Sure. This is where the “sufficient sensors” part comes in. “The algorithm” could actually handle that IF there were a reliable wasp-in-box (that in turn would depend on exit detection somehow). Maybe it also decides you cannot have gone to bed if no-one has been in the bathroom or hallway etc.
It’s all guesswork, more info produces better guesses.

I suppose the take away should be not to be rigid, err on the side of caution, and always always have a manual override - a wall switch for the lights at least :smiley:

Yes. But I have not yet seen any system to be good enough at guessing (or at least with zero false positives as these are way more annoying than when it doesn’t switch off lights).
That’s including my own system where at least I can code some of my human erratic behavior into.
That’s why I recommend the button. It does exactly work the way I want it to :wink:

I don’t but I wanted you to think about it in case there was some sort of event already present.

Some ideas I can think of that might work:

  • pressure sensor under the matriss
  • a button on the bedside table to kick off the bed routine
  • combination of events and states that you check for at BED time and unless most enough of them are true set a timer to check again later (e.g TV is off, at least one phone is charging (preferably the person who is home), and there has been no motion for awhile)

You can extend ToD to be day of week aware and move BED to 1am or later for those days.

I’m not quite so pessimistic. Some people have a very set routine and generate a very easily detectable set of events when going to bed. For those it’s very possible to reliably determine the right time. Similarly, with enough of the right sensors generating the right events it is also possible.

It’s worth at least thinking about and looking at one’s event’s log to see if there is a reliable pattern. There very likely is not but that doesn’t make the exercise pointless.

Ah, now that’s something I hadn’t even considered and might actually be a very good way around it…it might also work for a few other weekday / weekend rules I’ve got planned and tie them all nicely into an existing rule…:thinking: