Flows restart after restart of Node Red but not after a reboot of the pi

Relatively new to Node Red so if you can answer in laymans terms or with a complete example.

I have a Raspberri Pi running Node Red and some of the flows are not restarted after the Pi is rebooted. If I do a restart of Node Red, or it crashes and restrarts itself, or I do a node-red-stop and node-red-start the flows all do work.

On a reboot of the pi I have to open Node Red and click the restart flows button for my xbox and alexa nodes to reconnect.

How do you automate that action to just have them restart automatically, is there a flag you can pass into the startup of node red, or a setting you can put in a startup file?

I've seen a couple threads a couple years old asking the same question and I didn't get anywhere with those. Such as how/where would you post a url call on startup to get the flows to restart.

Welcome to the forum @IanLauder.

There should be no difference between a reboot startup and restart. I wonder whether node-red is starting up too quickly before the network is ready, or something like that. Even then I would have expected the nodes to reconnect automatically.

Can you post the startup log you get when you stop and start node red manually, then reboot and when it is up, before doing anything else, run, in a terminal, node-red-log and post that too.

Copy/paste the logs rather than a screendump please. See this post for details of how to post code and flows so the forum does not mess them about - How to share code or flow json

There is definitely a difference. The log file on a node-red restart all looks good with everything being initialized. The log file on a reboot of the pi is showing a lot of errors trying to reach the ip addresses of the other devices. But after its rebooted and I bring up the node-red browser and click the Restart Flows they all reconnect.

I did notice while turning on SSH to copy the log files off the Pi there is a Pi config preferences setting "Network at Boot" and on mine its set to "Do not wait". If you think its firing up node red before the network then maybe that setting needs to be set to "Wait for network". I'll give that a try next.

This is the log after a reboot (the log after a restart has no connection errors): I had to cut out the urls because it was giving me errors about posting too many links.

24 Mar 09:17:32 - [info] User directory : /home/pi/.node-red
24 Mar 09:17:32 - [warn] Projects disabled : editorTheme.projects.enabled=false
24 Mar 09:17:32 - [info] Flows file     : /home/pi/.node-red/flows_raspberrypi.json
24 Mar 09:17:32 - [info] Server now running at http://127.0.0.1:1880/
24 Mar 09:17:32 - [info] Starting flows
24 Mar 09:17:32 - [info] [hubitat config:Hubitat] Starting endpoint for /hubitat/webhook
24 Mar 09:17:32 - [info] Started flows
24 Mar 09:17:32 - [warn] [hubitat config:Hubitat] Unable to fetch devices: FetchError: request to http://###.###.##.92:80/apps/api/695/devices/*?access_token=XXXXXXX-a204-49ed-b1c5-83aeb53e450b failed, reason: connect ENETUNREACH ###.###.##.92:80 - Local (0.0.0.0:0)
24 Mar 09:17:32 - [warn] [hubitat device:bebf5c78.ac667] Unable to initialize device: request to http://###.###.##.92:80/apps/api/695/devices/*?access_token=XXXXXXX-a204-49ed-b1c5-83aeb53e450b failed, reason: connect ENETUNREACH ###.###.##.92:80 - Local (0.0.0.0:0)
error: Error: connect ENETUNREACH ###.###.##.136:5222 - Local (0.0.0.0:0)
error: Error: connect ENETUNREACH ###.###.##.136:5222 - Local (0.0.0.0:0)
24 Mar 09:17:32 - [warn] [hubitat config:Hubitat] Unable to fetch devices: FetchError: request to http://###.###.##.92:80/apps/api/695/devices/*?access_token=XXXXXXX-a204-49ed-b1c5-83aeb53e450b failed, reason: connect ENETUNREACH ###.###.##.92:80 - Local (0.0.0.0:0)
24 Mar 09:17:32 - [warn] [hubitat device:e077bb9b.c73938] Unable to initialize device: request to http://###.###.##.92:80/apps/api/695/devices/*?access_token=XXXXXXX-a204-49ed-b1c5-83aeb53e450b failed, reason: connect ENETUNREACH ###.###.##.92:80 - Local (0.0.0.0:0)
24 Mar 09:17:32 - [warn] [hubitat config:Hubitat] Unable to fetch devices: FetchError: request to http://###.###.##.92:80/apps/api/695/devices/*?access_token=XXXXXXX-a204-49ed-b1c5-83aeb53e450b failed, reason: connect ENETUNREACH ###.###.##.92:80 - Local (0.0.0.0:0)
24 Mar 09:17:32 - [warn] [hubitat device:Turn Down Alexa] Unable to initialize device: request to http://###.###.##.92:80/apps/api/695/devices/*?access_token=XXXXXXX-a204-49ed-b1c5-83aeb53e450b failed, reason: connect ENETUNREACH ###.###.##.92:80 - Local (0.0.0.0:0)
24 Mar 09:17:32 - [info] [alexa-remote-account:Ian's Echos] intialising "Ian's Echos" with the COOKIE method and saved data...
24 Mar 09:17:32 - [warn] [hubitat config:Hubitat] Unable to fetch devices: FetchError: request to http://###.###.##.92:80/apps/api/695/devices/*?access_token=XXXXXXX-a204-49ed-b1c5-83aeb53e450b failed, reason: connect ENETUNREACH ###.###.##.92:80 - Local (0.0.0.0:0)
24 Mar 09:17:32 - [warn] [hubitat device:Turn Up Alexa] Unable to initialize device: request to http://###.###.##.92:80/apps/api/695/devices/*?access_token=XXXXXXX-a204-49ed-b1c5-83aeb53e450b failed, reason: connect ENETUNREACH ###.###.##.92:80 - Local (0.0.0.0:0)
24 Mar 09:17:32 - [error] [alexa-remote-init:6c43f2b4.15104c] Error while checking Authentication: Error: getaddrinfo EAI_AGAIN pitangui.amazon.com pitangui.amazon.com:443
24 Mar 09:17:32 - [warn] [hubitat config:Hubitat] Unable to fetch devices: FetchError: request to http://###.###.##.92:80/apps/api/695/devices/*?access_token=XXXXXXX-a204-49ed-b1c5-83aeb53e450b failed, reason: connect ENETUNREACH ###.###.##.92:80 - Local (0.0.0.0:0)
24 Mar 09:17:32 - [warn] [hubitat device:72a2b5d.c393a4c] Unable to initialize device: request to http://###.###.##.92:80/apps/api/695/devices/*?access_token=XXXXXXX-a204-49ed-b1c5-83aeb53e450b failed, reason: connect ENETUNREACH ###.###.##.92:80 - Local (0.0.0.0:0)
24 Mar 09:17:32 - [warn] [hubitat config:Hubitat] Unable to fetch devices: FetchError: request to http://###.###.##.92:80/apps/api/695/devices/*?access_token=XXXXXXX-a204-49ed-b1c5-83aeb53e450b failed, reason: connect ENETUNREACH ###.###.##.92:80 - Local (0.0.0.0:0)
24 Mar 09:17:32 - [warn] [hubitat device:830dc42d.03a438] Unable to initialize device: request to http://###.###.##.92:80/apps/api/695/devices/*?access_token=XXXXXXX-a204-49ed-b1c5-83aeb53e450b failed, reason: connect ENETUNREACH ###.###.##.92:80 - Local (0.0.0.0:0)
24 Mar 09:17:32 - [warn] [hubitat config:Hubitat] Unable to fetch devices: FetchError: request to http://###.###.##.92:80/apps/api/695/devices/*?access_token=XXXXXXX-a204-49ed-b1c5-83aeb53e450b failed, reason: connect ENETUNREACH ###.###.##.92:80 - Local (0.0.0.0:0)
24 Mar 09:17:32 - [warn] [hubitat device:61b661be.d5a9d] Unable to initialize device: request to http://###.###.##.92:80/apps/api/695/devices/*?access_token=XXXXXXX-a204-49ed-b1c5-83aeb53e450b failed, reason: connect ENETUNREACH ###.###.##.92:80 - Local (0.0.0.0:0)

I turned on the "Wait for network" setting and rebooted the pi and got the same errors. Waited awhile refreshing the node-red screen and the flows didn't start backup and all the same ip address connection errors in the node-red-log. Still have to manually click the Restart Flows button.

On a normal restart of node red this is the log file:

Started Node-RED graphical event wiring tool.
24 Mar 11:04:34 - [info]
Welcome to Node-RED
===================
24 Mar 11:04:34 - [info] Node-RED version: v1.2.9
24 Mar 11:04:34 - [info] Node.js  version: v10.24.0
24 Mar 11:04:34 - [info] Linux 5.4.83-v7l+ arm LE
24 Mar 11:04:36 - [info] Loading palette nodes
24 Mar 11:04:39 - [info] Settings file  : /home/pi/.node-red/settings.js
24 Mar 11:04:39 - [info] Context store  : 'default' [module=localfilesystem]
24 Mar 11:04:39 - [info] User directory : /home/pi/.node-red
24 Mar 11:04:39 - [warn] Projects disabled : editorTheme.projects.enabled=false
24 Mar 11:04:39 - [info] Flows file     : /home/pi/.node-red/flows_raspberrypi.json
24 Mar 11:04:39 - [info] Starting flows
24 Mar 11:04:39 - [info] [hubitat config:Hubitat] Starting endpoint for /hubitat/webhook
24 Mar 11:04:39 - [info] Started flows
24 Mar 11:04:39 - [info] Server now running at http://127.0.0.1:1880/
24 Mar 11:04:39 - [info] [alexa-remote-account:Ian's Echos] intialising "Ian's Echos" with the COOKIE method and saved data...
24 Mar 11:04:41 - [info] [hubitat device:bebf5c78.ac667] Initialized. switch: off
24 Mar 11:04:41 - [info] [hubitat device:61b661be.d5a9d] Initialized. pushed: 1
24 Mar 11:04:41 - [info] [hubitat device:e077bb9b.c73938] Initialized. switch: off
24 Mar 11:04:41 - [info] [hubitat device:Turn Down Alexa] Initialized. switch: off
24 Mar 11:04:41 - [info] [hubitat device:Turn Up Alexa] Initialized. switch: off
24 Mar 11:04:41 - [info] [hubitat device:72a2b5d.c393a4c] Initialized. switch: off
24 Mar 11:04:41 - [info] [hubitat device:830dc42d.03a438] Initialized. switch: off

Did you install node red using the recommended script from the node red docs?

It was already installed on the Rasberry Pi when I got it. Under the raspberry desktop icon > Programming > Node-Red is where it was located. I just ran that.

I did run the "sudo systemctl enable nodered.service" command awhile ago that's shown on the node-red-start screen.

And before that when I was reading forums on how to get it to autostart on boot up I had made a unit file using an example I found on starting node-red that way in the /lib/systemd/system folder. Maybe that's the problem. I'll try removing that startup file I manually created.

I removed the original unit file I had added to auto start Node Red. used systemctl to stop and then disable it, deleted the unit file and reloaded systemctl then ran the "sudo systemctl enable nodered.service" again and rebooted. I'm still getting the errors.

Once I get to a terminal open and type "node-red-restart" manually it does initialize my flows. So maybe if I can't figure out the timing I can add a startup command with a minute or so delay to call "node-red-restart".

It is difficult to recommend anything when you haven't got a standard install.
The standard script includes an optional line which waits for the system clock to be initialised, which would indicate that the network is up.

I just fixed it right when your last email came in.

I made a new systemd unit file that calls node-red-restart when the network is available and as soon as it powers up and I check the node-red-log its all restarted without errors and bringing up the node red in a browser shows the xbox and alexa devices all connected right after the reboot.

I made a file /etc/systemd/system/nrrestart.service

[Unit]
Description=Restart Node Red that requires network
After=network.target

[Service]
Type=oneshot
ExecStart=/home/pi/nrrestart.sh

[Install]
WantedBy=multi-user.target

And my /home/pi/nrrestart.sh file is just one line to call node-red-restart

#!/bin/bash
node-red-restart

Not sure about the Node Red install since it came with the device vs how a "standard" install is different but I seem to have it working now.

Glad it is working.

In fact the real problem is that the hubitat node does not recover when the network comes up - if that is indeed the case. Are you using this node - node-red-contrib-hubitat (node) - Node-RED

If so then you should submit an issue against the node. You can do that at Issues · fblackburn1/node-red-contrib-hubitat · GitHub

I am using node-red-contrib-hubitat and it seems to be working fine including right after the reboot now.

I just flipped the switch on the pi and restarted it, and kept refreshing the browser till it came up and it showed the nodes all initialized and the log file isn't showing errors, including on the hubitat connections.

When I issue an alexa command that flips a hubitat virtual switch that I monitor in the Node Red flow it shows it being flipped and fires off the node red flow now. Without me having to manually restart anything anymore on the reboot.

Do you mean it is all now working without the stuff you added to restart node-red after the network comes up?

No, that is what got it working, by adding the restart-node-red in the systemd file on startup.

Right, so there is a bug in the hubitat node that if it starts up before the network is up then it does not recover. At lest that is what I thought you had deduced.

It was my harmony hub and alexa nodes there were also not initialized and those are not tied to hubitat. Those are using node-red-contrib-alexa-cakebaked and node-red-contrib-harmony. Those devices are also in the error messages that they couldn't be reached either. They are mixed in with the hubitat connection errors.

OK, so did you actually work out exactly where the problem was that meant it needed a restart?

No, I didn't figure out the root cause. To answer that question about if NR was installed per the instructions you mentioned I'd have to uninstall it and reload everything just to see if it starts working without the need to launch the restart on boot. And its not that important for me to figure out the how and why when I figured out a workaround. I've done a bunch of reboots and its working every time now. If I ever have to start from scratch though, I know where to go to follow the NR install instructions.

Okay, maybe I just did spot something. Note my use of unix is about 20 years rusty so it takes me awhile to poke around and remember how some of this worked, and a lot of it is new to what I used to do on these systems.

I found the /lib/systemd/system/nodered.service file which is the one NR is using to startup I believe.

There is a line you mentioned about using a time sync to wait before starting but its commented out.

I uncommented that line for the time sync check and removed my node red restart and now it is working properly.

Whoever created the Node Red install on this system (I got as a kit from Labists) must have set it up with that option disabled.

1 Like

The default is far that to be disabled. It causes the Node-RED start to wait for the time to sync, which may never happen if you aren’t connected to the internet. So it’s safer to not wait