Timer to start something after x seconds

Hi.
I want to set timer from HA Lovelace to turn off my TV after X seconds.
I have created inputnumber in Lovelace (which responds for seconds) and created a following flow. But Wait Untill node does not work as I expect. It continues to wait and does not fire.

[{"id":"70568eed.1baae","type":"inject","z":"bab4db30.79f0a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":300,"wires":[["3ab171cd.84e32e"]]},{"id":"4b50dd5e.6e2574","type":"debug","z":"bab4db30.79f0a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":810,"y":340,"wires":[]},{"id":"3ab171cd.84e32e","type":"function","z":"bab4db30.79f0a8","name":"","func":"const states = global.get('homeassistant').homeAssistant.states;\nmsg.delay = msg.payload + (parseInt(states['input_number.delay_turning_off'].state) * 1000);// * 60);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":300,"wires":[["60183c24.ffc4d4"]]},{"id":"bd76f561.b190f8","type":"ha-wait-until","z":"bab4db30.79f0a8","name":"","server":"9255c353.0ab86","outputs":1,"entityId":"sensor.time","entityIdFilterType":"exact","property":"state","comparator":"is","value":"gotime","valueType":"msg","timeout":0,"timeoutType":"num","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":740,"y":280,"wires":[["4b50dd5e.6e2574"]]},{"id":"60183c24.ffc4d4","type":"moment","z":"bab4db30.79f0a8","name":"","topic":"","input":"delay","inputType":"msg","inTz":"Europe/Moscow","adjAmount":0,"adjType":"days","adjDir":"add","format":"","locale":"C","output":"gotime","outputType":"msg","outTz":"Europe/Moscow","x":520,"y":300,"wires":[["4b50dd5e.6e2574","bd76f561.b190f8"]]},{"id":"9255c353.0ab86","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Hi, i am using the following to do what you want. But it uses a different logic:

Thank you. But it is not exactly what I want.
Can you share your logic?

Try this

[{"id":"f8198361.55336","type":"server-state-changed","z":"51045e07.bbf87","name":"Input Number in Seconds","server":"d177950.2a5aa68","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"inputnumber","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","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,"x":190,"y":2300,"wires":[["2ec756db.42705a"]]},{"id":"2ec756db.42705a","type":"function","z":"51045e07.bbf87","name":"","func":"msg.delay = parseInt(msg.payload) * 6000; //milliseconds\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":380,"y":2300,"wires":[["cedc85a9.7401e8"]]},{"id":"cedc85a9.7401e8","type":"delay","z":"51045e07.bbf87","name":"","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":520,"y":2300,"wires":[["bbf7d7ec.329898"]]},{"id":"bbf7d7ec.329898","type":"api-call-service","z":"51045e07.bbf87","name":"Turn off Tv","server":"d177950.2a5aa68","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"tv_power","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":670,"y":2300,"wires":[[]]},{"id":"d177950.2a5aa68","type":"server","name":"Home Assistant","legacy":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}] ```

The Node and Screenshots are linked in the post. Just click on it to see more.

Why not use the helper “timer”.
It’s really great and counts down to time up in the state.

I use those as our timers and all you need to do is activate the timer and delay 2 seconds just to be sure the state changes.
Then wait until state is idle.

Here is my timer using node red, timer helper and IFTTT to capture the minutes I want the timer set on.

[{"id":"4b7ecf4a.24816","type":"server-events","z":"4f357a5f.1c8d34","name":"","server":"4bbca37b.1700ec","event_type":"ifttt_webhook_received","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"x":180,"y":660,"wires":[["f2e81b8d.78aca8"]]},{"id":"c5c37d4a.70f3d","type":"api-call-service","z":"4f357a5f.1c8d34","name":"","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"timer","service":"start","entityId":"timer.custom_timer","data":"{\"entity_id\":\"timer.custom_timer\",\"duration\":\"00:{{minutes}}:00\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":500,"y":740,"wires":[["488be745.846ed8"]]},{"id":"488be745.846ed8","type":"delay","z":"4f357a5f.1c8d34","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":680,"y":740,"wires":[["ec67306a.e07c8"]]},{"id":"ec67306a.e07c8","type":"ha-wait-until","z":"4f357a5f.1c8d34","name":"","server":"4bbca37b.1700ec","outputs":1,"entityId":"timer.custom_timer","entityIdFilterType":"exact","property":"state","comparator":"is","value":"idle","valueType":"str","timeout":"","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":840,"y":740,"wires":[["e04a68a9.07b0a8"]]},{"id":"e04a68a9.07b0a8","type":"api-call-service","z":"4f357a5f.1c8d34","name":"Play sound file","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"media_player","service":"play_media","entityId":"media_player.nedervaningen","data":"{\"entity_id\":\"media_player.nedervaningen\",\"media_content_id\":\"URL to my Nabu casa chime MP3 file\",\"media_content_type\":\"audio/mp3\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1020,"y":740,"wires":[["fcde9458.dc0f78"]]},{"id":"fcde9458.dc0f78","type":"delay","z":"4f357a5f.1c8d34","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1200,"y":740,"wires":[["f7e2cbd6.f48fd8"]]},{"id":"f7e2cbd6.f48fd8","type":"ha-wait-until","z":"4f357a5f.1c8d34","name":"","server":"4bbca37b.1700ec","outputs":2,"entityId":"media_player.nedervaningen","entityIdFilterType":"exact","property":"state","comparator":"is","value":"idle","valueType":"str","timeout":"5","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":1340,"y":740,"wires":[["2f5bfa85.5126b6"],["2f5bfa85.5126b6"]]},{"id":"30460677.c472aa","type":"api-call-service","z":"4f357a5f.1c8d34","name":"Speak message","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"tts","service":"cloud_say","entityId":"media_player.nedervaningen","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":2000,"y":740,"wires":[["1b3f3221.bef36e"]]},{"id":"4d40cdcb.dcf714","type":"api-call-service","z":"4f357a5f.1c8d34","name":"","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"media_player","service":"turn_on","entityId":"media_player.nedervaningen","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":660,"y":660,"wires":[["8abb96fd.d05778"]]},{"id":"4623bcca.b19a74","type":"api-current-state","z":"4f357a5f.1c8d34","name":"","server":"4bbca37b.1700ec","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"media_player.vardagsrum_mini","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"vardagsrum","override_data":"msg","blockInputOverrides":false,"x":1120,"y":660,"wires":[["595f61d.fec9da"]]},{"id":"595f61d.fec9da","type":"api-current-state","z":"4f357a5f.1c8d34","name":"","server":"4bbca37b.1700ec","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"media_player.kok_mini","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"koket","override_data":"msg","blockInputOverrides":false,"x":1470,"y":660,"wires":[["7070ce28.4995d"]]},{"id":"7070ce28.4995d","type":"api-call-service","z":"4f357a5f.1c8d34","name":"","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"media_player","service":"volume_set","entityId":"media_player.nedervaningen","data":"{\"entity_id\":\"media_player.nedervaningen\",\"volume_level\":0.55}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1790,"y":660,"wires":[["1b77a6de.77c739"]]},{"id":"d21a19f6.c3ace8","type":"api-call-service","z":"4f357a5f.1c8d34","name":"Speak message","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"tts","service":"cloud_say","entityId":"media_player.nedervaningen","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":2380,"y":660,"wires":[["f5c17220.a6c01"]]},{"id":"1d059eba.9e4961","type":"api-call-service","z":"4f357a5f.1c8d34","name":"","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"media_player","service":"volume_set","entityId":"media_player.kok_mini","data":"{\"entity_id\":\"media_player.kok_mini\",\"volume_level\":\"{{koket.attributes.volume_level}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":3070,"y":660,"wires":[["9d907b65.c89608"]]},{"id":"9d907b65.c89608","type":"api-call-service","z":"4f357a5f.1c8d34","name":"","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"media_player","service":"volume_set","entityId":"media_player.vardagsrum_mini","data":"{\"entity_id\":\"media_player.vardagsrum_mini\",\"volume_level\":\"{{vardagsrum.attributes.volume_level}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":3370,"y":660,"wires":[["c5c37d4a.70f3d"]]},{"id":"8abb96fd.d05778","type":"delay","z":"4f357a5f.1c8d34","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":860,"y":660,"wires":[["4623bcca.b19a74"]]},{"id":"2f5bfa85.5126b6","type":"api-call-service","z":"4f357a5f.1c8d34","name":"","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"media_player","service":"volume_set","entityId":"media_player.nedervaningen","data":"{\"entity_id\":\"media_player.nedervaningen\",\"volume_level\":0.55}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1570,"y":740,"wires":[["c8a8686a.3bbed8"]]},{"id":"439b619d.4f347","type":"api-call-service","z":"4f357a5f.1c8d34","name":"","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"media_player","service":"volume_set","entityId":"media_player.kok_mini","data":"{\"entity_id\":\"media_player.kok_mini\",\"volume_level\":\"{{koket.attributes.volume_level}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":2730,"y":740,"wires":[["8dbebdc3.6e629"]]},{"id":"8dbebdc3.6e629","type":"api-call-service","z":"4f357a5f.1c8d34","name":"","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"media_player","service":"volume_set","entityId":"media_player.vardagsrum_mini","data":"{\"entity_id\":\"media_player.vardagsrum_mini\",\"volume_level\":\"{{vardagsrum.attributes.volume_level}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":3030,"y":740,"wires":[[]]},{"id":"f5c17220.a6c01","type":"delay","z":"4f357a5f.1c8d34","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2560,"y":660,"wires":[["201df694.4fadda"]]},{"id":"201df694.4fadda","type":"ha-wait-until","z":"4f357a5f.1c8d34","name":"","server":"4bbca37b.1700ec","outputs":1,"entityId":"media_player.vardagsrum_mini","entityIdFilterType":"exact","property":"state","comparator":"is","value":"idle","valueType":"str","timeout":"","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":2720,"y":660,"wires":[["32e71124.c8b2ee"]]},{"id":"1b3f3221.bef36e","type":"delay","z":"4f357a5f.1c8d34","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2180,"y":740,"wires":[["bbf66495.f67f78"]]},{"id":"bbf66495.f67f78","type":"ha-wait-until","z":"4f357a5f.1c8d34","name":"","server":"4bbca37b.1700ec","outputs":1,"entityId":"media_player.vardagsrum_mini","entityIdFilterType":"exact","property":"state","comparator":"is","value":"idle","valueType":"str","timeout":"","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":2340,"y":740,"wires":[["731a8b5d.b037d4"]]},{"id":"f2e81b8d.78aca8","type":"change","z":"4f357a5f.1c8d34","name":"","rules":[{"t":"set","p":"minutes","pt":"msg","to":"payload.event.minutes","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":660,"wires":[["4d40cdcb.dcf714"]]},{"id":"1b77a6de.77c739","type":"function","z":"4f357a5f.1c8d34","name":"Build message to speak json string","func":"if(msg.minutes == 1){\n    msg.payload = {\n        data:{\n            \"entity_id\": \"media_player.nedervaningen\",\n            \"message\": \"En tajmer på \" + msg.minutes + \" minut är startad\",\n            \"language\": \"sv-SE\"\n        }\n    }\n}else{\n    msg.payload = {\n        data:{\n            \"entity_id\": \"media_player.nedervaningen\",\n            \"message\": \"En tajmer på \" + msg.minutes + \" minuter är startad\",\n            \"language\": \"sv-SE\"\n        }\n    }\n}\n\nmsg.topic = \"\";\nmsg.message = msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":2100,"y":660,"wires":[["d21a19f6.c3ace8"]]},{"id":"32e71124.c8b2ee","type":"function","z":"4f357a5f.1c8d34","name":"","func":"msg.payload = \"\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":2860,"y":660,"wires":[["1d059eba.9e4961"]]},{"id":"c8a8686a.3bbed8","type":"function","z":"4f357a5f.1c8d34","name":"","func":"msg.payload = msg.message;\nmsg.payload.data.message = msg.message.data.message.replace(\"startad\", \"avslutad\");\nmsg.payload.data.message = msg.payload.data.message.replace(\"En tajmer\", \"Tajmern\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1820,"y":740,"wires":[["30460677.c472aa"]]},{"id":"731a8b5d.b037d4","type":"function","z":"4f357a5f.1c8d34","name":"","func":"msg.payload = \"\";\nmsg.message = \"\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":2500,"y":740,"wires":[["439b619d.4f347"]]},{"id":"4bbca37b.1700ec","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

With this settings in IFTTT:

So, I say “Hey google. 5 minutes” and it calls IFTTT and IFTTT does a http request that creates an event in HA/node red uses to set a timer of the said minutes and then speaks a message when the time is up.

Ok, I think I need to describe the whole idea.
I want my MiBand to track my sleeping state (it is done). And to turn off my TV when I fall asleep. (But mibamd is not very accurate and that is why I want to add timer. As sometimes MiBand thinks that I am already sleeeping too early). I also want to be able to stop timer if MiBand changes my sleeeping state. For exaple when I go to bathroom before I really fall asleep.

I have never worked in NR with time before. And I have no idea how to realise that.

I have found the answer.

Indeed, that is a bit of a different use case than all of us had in mind :wink:

In that case you can basically use any countdown, or timer node within NodeRed.
Just make sure to cancel it once your MiBand detects not_sleeping again.

I use a subflow that uses the same logic with a 10sec timer for my motion detectors:

[{"id":"38ecdf53.b21a","type":"function","z":"914a9b73.c28298","name":"on/off","func":"var stop = {payload:\"STOP\"}\n\nif (msg.payload === \"on\") {\n   return [ msg, stop ];\n} else {\n   return [ null, msg ];\n}\nreturn msg\n\n","outputs":2,"noerr":0,"x":270,"y":140,"wires":[["ed97cd7e.57ca7"],["2ce8c212.97a0ce"]]},{"id":"e5afe76f.aa45a8","type":"inject","z":"914a9b73.c28298","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":90,"y":80,"wires":[["38ecdf53.b21a"]]},{"id":"deefcb6a.ad8a38","type":"inject","z":"914a9b73.c28298","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":90,"y":40,"wires":[["38ecdf53.b21a"]]},{"id":"2ce8c212.97a0ce","type":"stoptimer","z":"914a9b73.c28298","duration":"10","units":"Second","payloadtype":"str","payloadval":"","name":"","x":430,"y":180,"wires":[["ed97cd7e.57ca7"],[]]},{"id":"ed97cd7e.57ca7","type":"rbe","z":"914a9b73.c28298","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":590,"y":140,"wires":[[]]}]

I solved this by using the “wait until” node. I just picked a random entity like sun.sun and set the timeout to 5 seconds.