Add time to Timer?

Is it possible to add time to a timer?
Say a timer is started for 10 mins… then 3 mins later a rule runs and adds another 10mins to the timer so total time is now 13mins.

Example of code i want to implement it on.

When my door lock unlocks create an auto lock timer for 20mins. If the door opens, reschedule the timer by adding another 20mins to the timer. So if you open the door 2 mins after unlocking it, the auto lock timer would be at 38mins.

The Timer interface is very simple so you can’t simply extend an already scheduled Timer.

There are ways and means though. I assume you are keeping a global reference to the Timer instance when you create it. At that point you could also capture a global ‘now’ timestamp’ and a global ‘minutesSet’ for the Timer.

At any point in the future when your rule triggers and the Timer is not null you know it’s active. Then you .cancel that instance and do a little bit of maths to compute your new delay.
minutesSet - ('now' - captureTimestamp) = length left on Timer

Add that to your standard 20mins and you’re ready to create the Timer again. Obviously you also update your globals again so they are correct for the next rule execution if required.

At any point the Timer executes the first thing it should do is null the global Timer reference.

@Python and @RayMYP, the simple answer is yes.

MyTimer.reschedule(now.plusMinutes(10))

Of course, you will have to keep a timestamp like @RayMYP demonstrates to do the math to calculate the number to put into the plusMinutes.

But I have to ask, do those two minutes really matter? Why not simplify the logic and if the door opens while the lock timer is active to just schedule the timer for 40 minutes after when the door opens? It would be sooooo much simpler to implement and without knowing more details, provide basically the same capability.

1 Like

That is unusual, as opposed to just resetting to 20 mins from now.

An alternative approach is to build your own timer, based around a Number Item.
Set up a cron triggered rule that decrements the Item once a minute until it reaches zero, then does whatever (autolocks).
Rules to start or change the ‘timer’ can examine its current value and do what you wish with it.

Once basically running, you’d probably want to implement a reentrant Lock to stall changes during the split second a decrement is actually in progress, but thats just prettifying.

Hi Rich,

That’s what i do right now, but i run into problems (yesterday was installing new windows on the house and running inside and out) where i would be going in and out multiple times so it resets the timer but there are times when i’m outside or inside longer then 20mins and i run back into the house to smack my face on the door cause its locked lol.

I have a global variable/switch that i can use to disable auto lock but just trying to come up with a way that requires less user intervention

The way i see this method is based on high user activity which would probably predict ill be using the door more often and should make the auto lock timer run longer than normal.

The 2 minutes don’t matter but if it adds 18 mins it means the door wont lock for a longer time

Which is why I set the timer for 40 minutes after the door opens if the unlocked timer is running.

My point is that rather than doing a bunch of math and sitting a bunch of timestamps, just use a hard-coded value that gets you close to what you are after.

  1. Door unlocks,
  2. Timer set for 20 minutes from now
  3. Two minutes later door opens
  4. Timer rescheduled for 40 minutes from now
  5. Timer goes off in 42 minutes from door unlocking

No need for timestamps, math, or anything like that.

Yeah true maybe i’ll just do that… as i’m over complicating things i think lol :slight_smile: