Node that runs after restart?

I’m looking for a way to trigger a flow after restarting Home Assistant.

I have tried the inject node set run once after 60 seconds but it never seems to work.

Is there a better way?

1 Like

Hmm,

just tried that code but got a big list of errors

Will have alook tomorrow i think

The flow, when set up correctly, works perfectly.

An HA restart, without a full machine restart, does not restart the running Apps, so Node-RED continues to run [hence any inject nodes set to trigger on NR restart will not fire] and this flow can be used to refresh graphs etc.

The flow, as provided, includes manual option for creating the necessary boolean switch and automation in Home Assistant, however you can follow the instructions and do this manually.

1 Like

Well, i created the helper, done that lots before but never even looked at automations before and have no idea how to create one from the code in the link :slight_smile:

No idea why the code failed before but it did not look like the image you show above?

Sorted, thanks.

Just need to creat the Node Red bits and that should be it. I never realised Node Red continued running when HA was restarted, that explains a lot.

Still working on this.

I must have simplified too much as my restart was not caught by NR.

I have the automation running in HA, it shows it was triggered upon restart so that part is ok, the boolean that it triggers was set as expected so thats ok.

In NR i have an events:state node looking at the boolean but it did not catch the event.

One reason might be that the boolean was already on when i restarted, not sure how but it would explain it. I have reset it and also turned off the “ignore if state is same as previous” on the state node.

The only bits that I have ‘simplified’ have been to remove the auto-generate code for building the automation and input_boolean (and sub-flow) which I prefer to do by hand myself. The flow executes and monitors a specific sequence of triggers and events and cannot really be further simplified.

As far as I understand the flow, it works in two parts.

In Home Assistant

A. The automation “home assistant restarts” is triggered by the Home Assistant restart. The standard list of triggers include ‘home assistant’ for when HA starts or shuts down, this automation uses the ‘starts’ trigger. The action then taken is to turn on an input_boolean (switch) “home assistant restarted”

B. The input boolean “home assistant restarted” is therefore set to ‘on’ by HA when HA restarts. However, this switch can also be turned on manually. Note too that any input_boolean retains the state (has it restored) by HA on restart, so if it was left ‘on’ before restart it will be restored to ‘on’ at restart [which we actually don’t want]

*Note: you have to add these bits yourself or use the code to do this for you. Create input_boolean “home assistant restarted” first before creating the automation

In Node-RED

  1. The Events: all node listens for ‘home_assistant_client’ events, which are put on the event bus by HA and include events like ‘starting’ and ‘running’ and ‘ready’. When HA restarts, these events appear, and the node picks them up, with the Switch node filtering out just the event ‘running’ which is a good indication that HA has restarted and is now up and running.
    Then the input_boolean switch is checked to see that it is ‘on’ which shows that not only has HA restarted, but the “HA restarted automation” has also run and set the switch.

  2. Separately, the Events: state node listens for events on the specific “home assistant restarted” input_boolean switch. Set for if state as on, this node will therefore trigger a message if the switch has gone (from off or unavailable) to “on” [caused either by HA restarting or by a manual set to on]

The rest of the flow uses either of the two triggers, to use an Action node to turn the switch ‘off’, thus effectively toggling the input_boolean switch “off-on-off”, either from a manual turn-on or from the HA restarting automation turn-on.

Both triggers are required, and the switch must be reset to ‘off’ as part of the flow.

I would always [strongly] suggest using the flow from the link I provided. However here is a cut-down copy, with the code for creating the automation and input-boolean removed, and the nodes updated to the latest schema. You will need to add back your HA server, add the input_boolean entity-id you have used in HA, and then redeploy to update all the nodes.

[{"id":"4a91394f75614da1","type":"server-events","z":"60bdd784a3412b28","name":"","server":"","version":3,"exposeAsEntityConfig":"","eventType":"home_assistant_client","eventData":"","waitForRunning":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"}],"x":240,"y":2040,"wires":[["95e9d47ed2be5007"]]},{"id":"95e9d47ed2be5007","type":"switch","z":"60bdd784a3412b28","name":"running?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"running","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":472,"y":2040,"wires":[["4b7a32290de02cf7"]]},{"id":"648dd9ac108befdc","type":"api-call-service","z":"60bdd784a3412b28","name":"","server":"","version":7,"debugenabled":false,"action":"","floorId":[],"areaId":[],"deviceId":[],"entityId":["input_boolean.home_assistant_restarted"],"labelId":[],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","blockInputOverrides":false,"domain":"input_boolean","service":"turn_off","x":850,"y":2060,"wires":[["3754cbf709193071"]]},{"id":"4b7a32290de02cf7","type":"api-current-state","z":"60bdd784a3412b28","name":"has restarted?","server":"","version":3,"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":[],"for":0,"forType":"num","forUnits":"minutes","x":660,"y":2040,"wires":[["648dd9ac108befdc"],[]]},{"id":"f89ae68a99bb4e65","type":"server-state-changed","z":"60bdd784a3412b28","name":"restarted?","server":"","version":6,"outputs":2,"exposeAsEntityConfig":"","entities":{"entity":["input_boolean.home_assistant_restarted"],"substring":[],"regex":[]},"outputInitially":false,"stateType":"str","ifState":"on","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[],"x":680,"y":2100,"wires":[["648dd9ac108befdc"],[]]},{"id":"b5993f580d9eb5b8","type":"comment","z":"60bdd784a3412b28","name":"Home Assistant Restarted","info":"","x":250,"y":1980,"wires":[]},{"id":"3754cbf709193071","type":"link out","z":"60bdd784a3412b28","name":"Home Assistant Restarted","links":["c67631f89e6451f4"],"x":1110,"y":2060,"wires":[],"l":true},{"id":"971894a2fa258341","type":"global-config","env":[],"modules":{"node-red-contrib-home-assistant-websocket":"0.80.3"}}]

I have tested this flow and it all appears to work correctly, but again the best route is to download the flow from the link and use that. I know that it generates a few warnings, but that is only because the node schemas have changed since this flow was first created, and all it requires is a re-deploy to update them.

1 Like

Thanks for that,

I’m trying to grasp the need to check the Events:all state at present. If the HA automation turns the boolean on then I would have thought it was enough as that automation can only trigger once HA has restarted?

Does the automation need resetting once triggered? I know the boolean does and that is already in my flow. I did not think to ensure it was off before i restarted though; this has now been done as well.

I’ll forget about manually triggering the boolean as that will not happen; no reason to mess with it really.

I like to figure out what does what :slight_smile:

You really need to ask Kermit, who wrote the flow, as to exactly what is going on.

The exact sequence of an HA restart can include a full machine reboot, with HA restarting, and the Node-RED (as an App) restarting later.

Last to come back up will be the WebSocket connection between HA and Node-RED. Since all the HA nodes require this connection, the HA automation will have run long before the flow is able to monitor it. Whilst the HA automation will have turned the input_boolean switch ‘on’, the Event: state node will also miss this. My guess is that the flow requires the Events: all to respond to a late-passed event to get the flow started. I think that the events are queued, and at NR restart the Homeassistant events that occur at start are returned.

There are several events associated with an HA restart - I don’t know which one the HA trigger is associated with, but Node-RED sees several before the “running” and finally “ready”.

The input_boolean switch has to be checked to ensure that any queued event is related to a real restart, rather than just getting the WebSocket connection restarted (which can happen at any time).

HA automations do not have a concept of ‘resetting’. An HA automation is triggered by an event (eg HA restart) and runs to completion.

If you want to know more then you will have to ask Kermit!

1 Like

The events: all node is waiting for Home Assistant to reach its running state.

Home Assistant accepts WebSocket connections before it is fully started, so Node-RED can connect while HA is still loading integrations and restoring state. At that point, some entities, services, or integrations may not be available yet.

By waiting for the running state, you help ensure Home Assistant has finished startup and that most integrations and data are loaded and ready to accept commands.

1 Like

Thanks all, it’s making sense now and i will amend my flow to suit. Very interesting to learn how it all works in the background.

Seems to be working now, thanks all