Stopping / Delaying a timer

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:

Take a look at the new Ephemeris capability. It’s not well documented yet and support for JSR223 Rules isn’t implemented yet, but it’s purpose is to mark the day type. See Ephemeris binding? for details. You can mark weekend days in PaperUI (under Configuration -> System) and other types of days in an ephemeris.cfg file under $OH_CONF/services.

I’m building a reusable Python version of Time of Day and plan on incorporating this to have different times for different types of days.

Ok, granted there are people with a strict routine and there might be more to accept a suboptimum solution.
Maybe that was a bit too subjective of an answer, but I HATE being told or guessed by machines what is my current intention. Quite a number of those keep trying (such as those annoying online advertizers), and they all fail regularly.

Point is, the simple manual solution turned out to by far be the best (at least for me who doesn’t want to be patronized (infantilized?) by some computer even if it was me to knowingly write that code).
And pretty effortless.

Wow, you have been busy…just seen the 9 parter for python. I’ll take a leisurely read through it all and see if it gets the creative mind flowing.

Love the concept of converting, just need to find some time.

Hi all,

Just to add an update to this for posterity:

I had a little play with the rule after experiencing the routine a few times in real life and realised that if we are up after midnight when the rule is called we are watching the end of a film / TV program so I could use the fact that we simply turned the TV back on to cancel the rule…

So, I incorporated that back in, it’s a simple loop;

  • Routine runs at midnight, sends a message to the TV to inform us it’s about to run the routine
  • After 5 seconds of the message it turns the TV off.
  • It then sets a timer for 10 seconds
  • If the TV is still OFF, it continues with the routine, turning on the camera and turning down the thermostat
  • If the TV is back ON it sends a message to the TV confirming the routine has been delayed
  • It then resets the aEndofDay item switch back to OFF
  • Sets a timer for 30 minutes and then turns the aEndofDay item to ON
  • The whole process starts again
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("End of Day routine started - turn TV back on to delay routine")
        createTimer(now.plusSeconds(5),
            [
            TV_LG_Power.sendCommand(OFF)
                createTimer(now.plusSeconds(10),
                [
                    if(TV_LG_Power.state == OFF)
                    {
                        ThermostatSetPoint.sendCommand(16.5)
                        createTimer(now.plusMinutes(5),
                        [
		                    CameraOn.sendCommand(ON)
		                    logInfo(logName, "End of Day Routine ON - Cameras ON")
                        ])
                    }
                    else
                    {
                        TV_LG_Toast.sendCommand("End of Day routine delayed for 30 minutes")
                        aEndofDay.sendCommand(OFF)
                        createTimer(now.plusMinutes(30),
                        [
		                    aEndofDay.sendCommand(ON)
                        ])
                    }
                ])
            ])
        }
	else
        {
        logInfo(logName, "End of Day routine - ended")
        CameraOn.sendCommand(OFF)
		logInfo(logName, "End of Day Routine OFF - Cameras OFF")
        }
end