Starting a flow after a Home Assistant restart
Use a Home Assistant automation to set an input boolean to on
then from Node-RED check if that input boolean is on
after a successful connection or at any time it changes to on
.
[{"id":"fb5296f3c4de2a23","type":"subflow","name":"Create HA Helpers","info":"","category":"HA Actions","in":[{"x":84,"y":96,"wires":[{"id":"fc18bdbb57e2fd63"}]}],"out":[],"env":[{"name":"serverName","type":"str","value":"Home Assistant","ui":{"label":{"en-US":"HA Server Name"},"type":"input","opts":{"types":["str"]}}},{"name":"helpers","type":"json","value":"[]","ui":{"label":{"en-US":"Helpers"},"type":"input","opts":{"types":["json"]}}}],"meta":{},"color":"#46B1EF","status":{"x":246,"y":48,"wires":[{"id":"ea6dc8ad6d9ece17","port":0}]}},{"id":"fc18bdbb57e2fd63","type":"function","z":"fb5296f3c4de2a23","name":"process helpers","func":"const serverName = toCamelCase(env.get('serverName'));\nconst haServer = global.get(\"homeassistant\")[serverName];\nif(!haServer) {\n node.error(\"Invalid HA server name\");\n return;\n}\nconst states = haServer.states;\nconst helpers = env.get(\"helpers\");\n\nhelpers.forEach(h => {\n const entityId = `${h.type}.${h.id}`;\n if(!states[entityId]) {\n const {id, type, ...data} = h;\n const apiData = {\n entity: h,\n payload: { \n data \n }\n };\n apiData.payload.data.type = `${type}/create`;\n \n node.send(apiData);\n node.status({text: `Creating ${entityId}`});\n }\n});\n\nnode.done();\n\nfunction toCamelCase(str) {\n return str.replace(/(?:^\\w|[A-Z]|\\b\\w|\\s+)/g, (match, index) => {\n if (+match === 0) return '';\n return index === 0 ? match.toLowerCase() : match.toUpperCase();\n });\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":224,"y":96,"wires":[["2b8e82c6d93d38e5"]]},{"id":"2b8e82c6d93d38e5","type":"ha-api","z":"fb5296f3c4de2a23","name":"create helper","server":"","version":1,"debugenabled":false,"protocol":"websocket","method":"get","path":"","data":"","dataType":"json","responseType":"json","outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"results"}],"x":390,"y":96,"wires":[["767624d22ee0c819"]]},{"id":"372232586f5e138f","type":"ha-api","z":"fb5296f3c4de2a23","name":"rename helper entity id","server":"","version":1,"debugenabled":false,"protocol":"websocket","method":"get","path":"","data":"{\t \"type\":\"config/entity_registry/update\",\t \"entity_id\": entity.type & \".\" & payload.id,\t \"new_entity_id\": entity.type & \".\" & entity.id\t}","dataType":"jsonata","responseType":"json","outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"results"}],"x":788,"y":96,"wires":[[]]},{"id":"767624d22ee0c819","type":"switch","z":"fb5296f3c4de2a23","name":"need to rename?","property":"payload.id","propertyType":"msg","rules":[{"t":"neq","v":"entity.id","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":570,"y":96,"wires":[["372232586f5e138f","815fe31e9d239e53"]]},{"id":"ea6dc8ad6d9ece17","type":"status","z":"fb5296f3c4de2a23","name":"","scope":["fc18bdbb57e2fd63","815fe31e9d239e53"],"x":124,"y":48,"wires":[[]]},{"id":"815fe31e9d239e53","type":"function","z":"fb5296f3c4de2a23","name":"rename status","func":"const oldEntityId = `${msg.entity.type}.${msg.payload.id}`;\nconst newEntityId = `${msg.entity.type}.${msg.entity.id}`;\n\nnode.status({text: `Renaming ${oldEntityId} to ${newEntityId}`});","outputs":1,"noerr":0,"initialize":"","finalize":"","x":768,"y":144,"wires":[[]]},{"id":"4a91394f75614da1","type":"server-events","z":"8e0ea5b84a1a9a3a","name":"","server":"","version":1,"event_type":"home_assistant_client","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"}],"x":200,"y":320,"wires":[["95e9d47ed2be5007"]]},{"id":"95e9d47ed2be5007","type":"switch","z":"8e0ea5b84a1a9a3a","name":"running?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"running","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":412,"y":320,"wires":[["4b7a32290de02cf7"]]},{"id":"648dd9ac108befdc","type":"api-call-service","z":"8e0ea5b84a1a9a3a","name":"","server":"","version":3,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.home_assistant_restarted","data":"","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":808,"y":320,"wires":[["3754cbf709193071"]]},{"id":"4b7a32290de02cf7","type":"api-current-state","z":"8e0ea5b84a1a9a3a","name":"has restarted?","server":"","version":2,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.home_assistant_restarted","state_type":"str","blockInputOverrides":false,"outputProperties":[],"x":576,"y":320,"wires":[["648dd9ac108befdc"],[]]},{"id":"f89ae68a99bb4e65","type":"server-state-changed","z":"8e0ea5b84a1a9a3a","name":"restarted?","server":"","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.home_assistant_restarted","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[],"x":588,"y":368,"wires":[["648dd9ac108befdc"],[]]},{"id":"b5993f580d9eb5b8","type":"comment","z":"8e0ea5b84a1a9a3a","name":"Home Assistant Restarted","info":"","x":190,"y":224,"wires":[]},{"id":"3754cbf709193071","type":"link out","z":"8e0ea5b84a1a9a3a","name":"Home Assistant Restarted","links":["c67631f89e6451f4"],"x":1070,"y":320,"wires":[],"l":true},{"id":"c67631f89e6451f4","type":"link in","z":"8e0ea5b84a1a9a3a","name":"Home Assistant Restarted","links":["3754cbf709193071"],"x":190,"y":416,"wires":[["33ccbe3fca679b80"]],"l":true},{"id":"33ccbe3fca679b80","type":"debug","z":"8e0ea5b84a1a9a3a","name":"do stuff","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":412,"y":416,"wires":[]},{"id":"bcfea616e335904d","type":"inject","z":"8e0ea5b84a1a9a3a","name":"Click to create","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":166,"y":272,"wires":[["447721875af4431a","5a92fd8241edd308"]]},{"id":"447721875af4431a","type":"ha-api","z":"8e0ea5b84a1a9a3a","name":"HA automation","server":"","version":1,"debugenabled":false,"protocol":"http","method":"post","path":"/api/config/automation/config/nrrestartnotification","data":"{\t \"alias\": \"Home Assistant Restart\",\t \"description\": \"\",\t \"trigger\": [\t {\t \"platform\": \"homeassistant\",\t \"event\": \"start\" \t } \t ],\t \"condition\": [],\t \"action\": [\t {\t \"service\": \"input_boolean.turn_on\",\t \"data\": {\t \"entity_id\": \"input_boolean.home_assistant_restarted\" \t }\t } \t ],\t \"mode\": \"single\" \t}","dataType":"jsonata","responseType":"json","outputProperties":[],"x":432,"y":224,"wires":[[]]},{"id":"5a92fd8241edd308","type":"subflow:fb5296f3c4de2a23","z":"8e0ea5b84a1a9a3a","name":"","env":[{"name":"helpers","value":"[{\"id\":\"home_assistant_restarted\",\"type\":\"input_boolean\",\"name\":\"Home Assistant Restarted\",\"icon\":\"mdi:restart-alert\"}]","type":"json"}],"x":442,"y":272,"wires":[]}]
Tips
The input boolean and automation can be created directly from Node-RED using the inject node found in the exported flow.
Create an input boolean using the Helpers menu (Configuration -> Helpers)
input_boolean:
home_assistant_restarted:
name: Home Assistant Restarted
Create the Home Assistant automation
alias: Home Assistant Restart
description: ""
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: input_boolean.turn_on
data: {}
entity_id: input_boolean.home_assistant_restarted
mode: single