How to clear or reset a timer?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
User avatar
dynamicdave
Normal user
Posts: 257
Joined: 30 Jan 2017, 20:25
Location: Hampshire, UK

How to clear or reset a timer?

#1 Post by dynamicdave » 04 Nov 2021, 14:47

Is there a way to clear a timer that is running?
Say, for example I have a timer that runs for 30-secs and I want to clear it so... On Rules#Timer=1 Do fires
What I'm doing at the moment (which works fine) is setting the timer to 1-second ( timerSet,1,1) so one second later the rule fires.
Just wondered if there was a neater way to achieve this?

User avatar
Ath
Normal user
Posts: 3419
Joined: 10 Jun 2018, 12:06
Location: NL

Re: How to clear or reset a timer?

#2 Post by Ath » 04 Nov 2021, 15:14

The official, and AFAIK documented, way is to set it to 0 ;)

Edit: Documentation: https://espeasy.readthedocs.io/en/lates ... mmand.html (Search for the TimerSet command)
/Ton (PayPal.me)

User avatar
dynamicdave
Normal user
Posts: 257
Joined: 30 Jan 2017, 20:25
Location: Hampshire, UK

Re: How to clear or reset a timer?

#3 Post by dynamicdave » 04 Nov 2021, 18:50

In the documentation it says it 'disables' the timer (which is what seems to happen in my rule-set).

TimerSet,<timernr>,0 (disables the timer)

I just wanted to clear or reset the timer.
I'll try the TimerSet_ms,<timernr>,<time in msec> command as I didn't know that existed.

User avatar
Ath
Normal user
Posts: 3419
Joined: 10 Jun 2018, 12:06
Location: NL

Re: How to clear or reset a timer?

#4 Post by Ath » 04 Nov 2021, 20:21

Then I don't fully comprehem why you wrote 'clear' as in my understanding that would not trigger the timer.
If you want to execute that event 'now', then just use an "event,rules#timer=1" command. Usually, the last command in that handler would be to re-initiate the timer, but if that is not desired you could use that timerset,1,0 just before the event command to prevent it being triggered at its original delay.
If the event is to be executed after the current event is finished, the execution can be queued by using the asyncevent command.

The timerset_ms is the same command as timerset, except it doesn't multiply the time with 1000 :)
/Ton (PayPal.me)

TD-er
Core team member
Posts: 8644
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: How to clear or reset a timer?

#5 Post by TD-er » 04 Nov 2021, 21:03

To clear, as in no longer get the timer event, you have to set the timer to 0.
To reset, as in re-set, you simply call timerset with the new interval you need. The nr of sec or msec, depending on calling TimerSet or TimerSet_ms from now.

User avatar
dynamicdave
Normal user
Posts: 257
Joined: 30 Jan 2017, 20:25
Location: Hampshire, UK

Re: How to clear or reset a timer?

#6 Post by dynamicdave » 05 Nov 2021, 08:22

Thanks for the suggestion to use... event,rules#timer=1
That works really well for my current project.

Basically what I have is a 300-second timer.
Every time it expires (every 5-mins) it will collect a temperature reading and sends it, via MQTT, to Node-RED where it is stored in a MySQL db and shown on a graph.
Node-RED can send an event to change the time interval to say 15 (seconds) so the updates happen more rapidly.
What I was trying to do was make the change to the time-interval become active from NOW, rather than having to wait until the current time runs out.

Using the... event,rules#timer=1 achieves this and means the system's response is very rapid.

I can now go on and apply the same technique to setting the max and min setpoints from Node-RED.

The final project is going to be a Room Temperature Display system (RTD).

Here's a pic of the prototype and the PCB (which has two RTDs and a piece of spare circuitry).
I've also attached a section of the 'test flow' that runs in Node-RED.

Thanks to everyone for their help and advice.
Attachments
Screen Shot 11-05-21 at 07.31 AM.GIF
Screen Shot 11-05-21 at 07.31 AM.GIF (26.66 KiB) Viewed 6428 times
A_Room_temp_and_display.GIF
A_Room_temp_and_display.GIF (71.01 KiB) Viewed 6435 times
breadboard_A.jpg
breadboard_A.jpg (226.67 KiB) Viewed 6435 times
Last edited by dynamicdave on 05 Nov 2021, 08:33, edited 2 times in total.

TD-er
Core team member
Posts: 8644
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: How to clear or reset a timer?

#7 Post by TD-er » 05 Nov 2021, 08:27

One more tip:
Please also have a look at the "asyncevent" command.
It is similar to the "event" command, with only difference that it will be put to the event queue and thus not handled immediately (but still fast enough).
The advantage is that it will not "stack" up in event execution as creating an event will initiate a new rule processing step, thus using more resources and with events calling events it may take quite a long time to process.
Most events (almost any, but especially this use case of yours) can be done using asyncevent and it will keep the ESP more responsive.

User avatar
dynamicdave
Normal user
Posts: 257
Joined: 30 Jan 2017, 20:25
Location: Hampshire, UK

Re: How to clear or reset a timer?

#8 Post by dynamicdave » 05 Nov 2021, 08:30

Thanks I did wonder about events stacking up.

Just tried using... AsyncEvent,rules#timer=1
Works really well.

Thank you.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 36 guests