Need help refactoring a flow

Hi everyone! I am starting learning how to use Node Red in my home automations. I made a flow to do some interaction between HomeAssistant automation and user, but i think the flow that i created is't the best way to do. I've created with some nodes and concepts that i already learned (only few ones), but i'm looking for other more interesting/elegant solutions.

Description of the flow:

After 9 minutes of inactivity detected by the presence sensor, I would like the Home Assistant to send me a notification to my cell phone (iOS, iPhone) advising that within 1 min, if nothing is done, it will turn off the lights in my room. From this moment 3 situations can occur:

  • Time (1min) expires -> bedroom lights must be turned off
  • User clicks in this notification -> chooses "ignore" -> room lights must remain on
  • User clicks in this notification -> chooses "turn off now" -> room lights must be turned off immediately

Code:

[{"id":"6a3798ac.fd87a8","type":"subflow","name":"Desliga quarto","info":"","category":"","in":[{"x":40,"y":80,"wires":[{"id":"3f9f2171.86c7ae"},{"id":"bf125c1.8eff2a"},{"id":"d5fcf690.302268"}]}],"out":[],"env":[],"color":"#DDAA99"},{"id":"bf125c1.8eff2a","type":"api-call-service","z":"6a3798ac.fd87a8","name":"Notificação: quarto desligado!","server":"d7586871.879718","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_iphone_de_kin","entityId":"","data":"{\"message\":\"Os equipamentos do quarto foram desligados!\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":290,"y":140,"wires":[[]]},{"id":"d5fcf690.302268","type":"api-call-service","z":"6a3798ac.fd87a8","name":"Desliga quarto","server":"d7586871.879718","version":1,"debugenabled":false,"service_domain":"homeassistant","service":"turn_off","entityId":"group.quarto","data":"","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":240,"y":200,"wires":[[]]},{"id":"3f9f2171.86c7ae","type":"api-call-service","z":"6a3798ac.fd87a8","name":"Alexa avisa que o quarto foi desligado","server":"d7586871.879718","version":1,"debugenabled":false,"service_domain":"notify","service":"alexa_media_echo_dot_do_quarto","entityId":"","data":"{\"message\":\"O quarto foi desligado com sucesso!\", \"data\":{\"type\":\"tts\"}}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":310,"y":80,"wires":[[]]},{"id":"b8360c57.2d4c8","type":"api-call-service","z":"44bf5bfc.853b74","name":"Notification: warns that the room will be shut down in 1 min","server":"d7586871.879718","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_iphone_de_kin","entityId":"","data":"{\"message\": \"Room equipment is turned off in 1 minute!\", \"data\": {\"push\": {\"category\": \"SHUTDOWNVERIFY\"}}}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1860,"y":1960,"wires":[["bdc83f38.ed63f","2d2d35d.7047eca"]]},{"id":"d831986d.af62c8","type":"server-events","z":"44bf5bfc.853b74","name":"Action was chosen in the notification","server":"d7586871.879718","event_type":"ios.notification_action_fired","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"x":1780,"y":2080,"wires":[["e7a748ab.e88118"]]},{"id":"becc271a.569bd8","type":"inject","z":"44bf5bfc.853b74","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1480,"y":1960,"wires":[["b8360c57.2d4c8"]]},{"id":"bdc83f38.ed63f","type":"delay","z":"44bf5bfc.853b74","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2220,"y":1960,"wires":[["f33037e6.a25c08"]]},{"id":"2d2d35d.7047eca","type":"change","z":"44bf5bfc.853b74","name":"Sets chosen_action to NONE","rules":[{"t":"set","p":"chosen_action","pt":"flow","to":"NONE","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2280,"y":1880,"wires":[[]]},{"id":"e7a748ab.e88118","type":"change","z":"44bf5bfc.853b74","name":"Define chosen_action with user choice","rules":[{"t":"set","p":"chosen_action","pt":"flow","to":"payload.event.actionName","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2130,"y":2080,"wires":[["c7e0df38.6b775"]]},{"id":"f33037e6.a25c08","type":"switch","z":"44bf5bfc.853b74","name":"No answer (chosen_action = NONE)?","property":"chosen_action","propertyType":"flow","rules":[{"t":"eq","v":"NONE","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":2490,"y":1960,"wires":[["5c8d49a.e2481b8"]]},{"id":"5c8d49a.e2481b8","type":"subflow:6a3798ac.fd87a8","z":"44bf5bfc.853b74","name":"shutdown","env":[],"x":2780,"y":1960,"wires":[]},{"id":"c7e0df38.6b775","type":"switch","z":"44bf5bfc.853b74","name":"IGNORE / SHUTDOWN ?","property":"chosen_action","propertyType":"flow","rules":[{"t":"eq","v":"IGNORE","vt":"str"},{"t":"eq","v":"SHUTDOWNNOW","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":2430,"y":2080,"wires":[["83c8166f.fd0068"],["d603839.5e1dd8"]]},{"id":"d603839.5e1dd8","type":"subflow:6a3798ac.fd87a8","z":"44bf5bfc.853b74","name":"shutdown","env":[],"x":2660,"y":2120,"wires":[]},{"id":"83c8166f.fd0068","type":"api-call-service","z":"44bf5bfc.853b74","name":"Alexa says: ok, I will not turn off","server":"d7586871.879718","version":1,"debugenabled":false,"service_domain":"notify","service":"alexa_media_echo_dot_do_quarto","entityId":"","data":"{\"message\":\"Ok! I'll not turn off\", \"data\":{\"type\":\"tts\"}}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":2730,"y":2060,"wires":[[]]},{"id":"d7586871.879718","type":"server","name":"Home Assistant","addon":true}]

Questions:

  1. Does the current flow work?
  2. Is it reliable?
  3. Does it have or cause performance or other issues?

Overoptimization is the curse of programmers everywhere. :grin:

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.