Node Red timer that persists after HA restart

Node Red timer that will survive an HA restart, a Node Red deploy or restart. Timer goes on and it creates an entity in HA with the remaining time. The sensor does update the remaining time in lovelace.

Example for 15 minutes timer.

You will need: ‘node-red-contrib-stoptimer-varidelay’

[{"id":"eebfa303.35e34","type":"stoptimer-varidelay","z":"3ebeb767.8778f8","duration":"5","durationType":"num","units":"Second","payloadtype":"num","payloadval":"0","name":"Timer in Node Red","reporting":"every_second","persist":true,"x":510,"y":240,"wires":[[],[],["b358ed04.66d79"]]},{"id":"b358ed04.66d79","type":"ha-entity","z":"3ebeb767.8778f8","name":"Timer entity in HA","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"your timer name"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":730,"y":240,"wires":[[]]},{"id":"7342425d.d702bc","type":"inject","z":"3ebeb767.8778f8","name":"Start","props":[{"p":"delay","v":"900","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":270,"y":220,"wires":[["eebfa303.35e34"]]},{"id":"3e3a6ffd.0306f","type":"inject","z":"3ebeb767.8778f8","name":"Stop","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"stop","payloadType":"str","x":270,"y":280,"wires":[["eebfa303.35e34"]]},{"id":"9405c3fe.d0a6c","type":"server","z":"","name":"Home Assistant","addon":true}]
5 Likes

Hey… this is great.

That is all.

1 Like

Exactly what I am looking for.
Thank you.

Stumbled across this when looking up articles for Node Red. It’s great, just what I needed!

1 Like

I must be missing something, I have the timer above that works good unless I restart HA or node red, so this sounds like what I was looking for, but I am getting an error on my test. I included my original working timer above the test for reference.

Share the code please so I can import it into my instance.

I installed node red companion via HACS and that solved the error message. I saw the message but it took a long time to sink in what it was saying, since I knew I had node red installed. wasn’t really aware of the HACS companion. the other thing though is why when I downloaded your code it says 5 seconds in the timer settings, but that turns out to be 15 minutes. if I change to 15 minutes and test, it makes a timer for 15 hours.

the delay of the varidelay node is overridden by the incoming msg.delay, if any. The unit of the given delay is set whithin the varidelay node, you can choose whichever serves you better.

Within the inject node Start in my example there’s a msg.delay that equals 15 minutes in seconds I believe. Using msg.delay to set your timers allows setting variable lengths whithin a single node.

I don’t know how or why this wizardry works, but that is stinkin’ amazing!!! Just replaced my fragile timer with a bullet proof one!

1 Like

Hiya

This looks great, and I have your sample working, but would you mind giving an example config for the input to be set, for a variable delay timer in HA?

E.g. for an adjustable delay kitchen timer driven by an input_number?

Thanks!

The incoming msg.delay is set whithin the event: state node. Set the unit of varidelay according to the unit of the input.

[{"id":"97172930.9af048","type":"stoptimer-varidelay","z":"937ef78.ec6b408","duration":"5","durationType":"num","units":"Second","payloadtype":"num","payloadval":"0","name":"Timer in Node Red","reporting":"every_second","persist":true,"x":430,"y":1500,"wires":[[],[],["5a1f5078.23d29"]]},{"id":"5a1f5078.23d29","type":"ha-entity","z":"937ef78.ec6b408","name":"Timer entity in HA","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"your timer name"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":650,"y":1500,"wires":[[]]},{"id":"6849e1aa.631e","type":"server-state-changed","z":"937ef78.ec6b408","name":"Input","server":"9405c3fe.d0a6c","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_number.your_input","entityidfiltertype":"exact","outputinitially":false,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"delay","propertyType":"msg","value":"","valueType":"entityState"}],"x":230,"y":1500,"wires":[["97172930.9af048"]]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

Hi,

Nice work, but I have some questions:
How do you change the minute’s timer? How can I send the input_number helper within 15 minutes?
How do I send from HA to start? How do I send from HA to stop?
I have in Node-red a switch node that’s waiting 1 when starts and 0 when stops. How do I send from the timer 1 when is ON?

Thank you

Cool Dude…

I’ve been using the varidelay node in other context. What I really need is a rate limiter that persists restarts.

did you manage to have this solved?

Could you elaborate a bit? there might be a way…

Sure - I have a morning briefing set up in Node-RED that announces various conditions through the Sonos system: mostly weather & calendar events. I have a regular Delay node configured to rate limit the briefing to once every 18 hours (effectively, once per day since flow is triggered during a few-hour window in the morning, depending on the day). If I am playing with flows and redeploying Node-RED, the briefing keeps going off during that window. A couple of days this week, for example, the briefing was announced 8-10 times.

This is a non-issue if I’m not messing with Node-RED or restarting HA. And for close to a year, I didn’t touch HA other than to perform updates… but I changed up my heating systems earlier this year (adding redundancy & backup), so I’m finding I need to tweak my climate control automations as the weather starts getting colder. As a result, I’m doing a lot of redeploying, usually in the mornings to support wakeup routines.

In short… a node in place of the Delay node (below) that remembers its rate limit through restarts would be nice.

[{"id":"6ee02756128465f4","type":"delay","z":"b199f89f.aefe08","g":"befd91fe2a77100e","name":"Once daily","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"18","rateUnits":"hour","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"allowrate":false,"outputs":1,"x":1010,"y":2540,"wires":[["422259873da0c729"]]}]

Correct if I’m wrong, you want to rate limit a flow to execute once every 18 hours?

you can easily achieve that with varidelay. First create a native binary sensor in NR. Then when the flows triggers, only execute the briefing if that binary_sensor is on. If that binary sensor is on => execute briefing => set binary sensor to off… Then 18 hours later you can set that binary sensor back on with varidelay, which will never break.

[{"id":"21c12d44a359c6fc","type":"inject","z":"878e74c2.7f39c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280,"y":200,"wires":[["4c375d044a69871f"]]},{"id":"4c375d044a69871f","type":"api-current-state","z":"878e74c2.7f39c8","name":"binary on?","server":"9405c3fe.d0a6c","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":430,"y":200,"wires":[["6fa23cd81feaa8f6","7cb541699bcbf68b"],[]]},{"id":"6fa23cd81feaa8f6","type":"api-call-service","z":"878e74c2.7f39c8","name":"briefing","server":"9405c3fe.d0a6c","version":5,"debugenabled":false,"domain":"","service":"","areaId":[],"deviceId":[],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":600,"y":180,"wires":[["16dea6c5758aed06"]]},{"id":"16dea6c5758aed06","type":"change","z":"878e74c2.7f39c8","name":"binary to off","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":810,"y":180,"wires":[["3dfee7205c0ca520"]]},{"id":"7cb541699bcbf68b","type":"stoptimer-varidelay","z":"878e74c2.7f39c8","duration":"18","durationType":"num","units":"Hour","payloadtype":"num","payloadval":"0","name":"","reporting":"none","reportingformat":"human","persist":true,"ignoretimerpass":false,"x":620,"y":240,"wires":[["56b04f9d4b4486bb"],[],[]]},{"id":"56b04f9d4b4486bb","type":"change","z":"878e74c2.7f39c8","name":"binary to on","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":810,"y":240,"wires":[["3dfee7205c0ca520"]]},{"id":"6e945e157e2cae11","type":"debug","z":"878e74c2.7f39c8","name":"binary sensor should go here","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1120,"y":220,"wires":[]},{"id":"3dfee7205c0ca520","type":"junction","z":"878e74c2.7f39c8","x":960,"y":220,"wires":[["6e945e157e2cae11"]]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

I have this set this exact way for my vacuum, so it only goes out once a day despite me leaving the house more than once.

1 Like

I’m going to play with this later today, thanks.

Very useful in a home automation context, thanks a lot!

Is there a possibility to have a single instance of the varitimer handle multiple timers at once?
Like the regular trigger node with has the “Handling each” option.

Or do I need to put multiple varitimers in the NR flow?

Thanks in advance a for your help