Automation - Node-Red

From Da Nerd Mage Wiki
Jump to navigation Jump to search
Proven on:
Logo Mint.png
Logo LMDE.png
Logo Debian.png
Logo Ubuntu.png
Logo Sparky.png
19.3 / 20.3 4 11 (bullseye) 20.04.3 5.11






Node-Red Web Site

As always...

Start with:

  • sudo apt update
  • sudo apt upgrade

Installing Node-Red

Start by installing nodejs and the javascript package manager (npm)

NOTE: The Ubunto repositories serve up an outdated version of Node.js. (v10.19.0)

Node-Red 2 requires v12...

Getting nodejs v12

  • sudo apt install nodejs
  • sudo apt-get install -y build-essential
  • sudo npm install npm@latest -g
    • (Doesn't actually seem to change anything on Debian... Updates to 8.3.0 on Ubuntu.)

Use npm to install Node-RED

  • sudo npm install -g node-red

Run it...

  • node-red

Adding Autostart capability using SystemD

Then download three required files to their correct locations

  • sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/nodered.service -O /lib/systemd/system/nodered.service
  • sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-start -O /usr/bin/node-red-start
  • sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop -O /usr/bin/node-red-stop

Note: These 3 files are kinda rpi specific...

To run as a user other than Pi, you need to edit the nodered.service file. To edit this use sudo to edit the file /lib/systemd/system/nodered.service and change the lines 11, 12 and 13.

  • sudo vi /lib/systemd/system/nodered.service

Replace the user name "pi" with a user on your actual system...

make the two scripts executable

  • sudo chmod +x /usr/bin/node-red-st*

reload the systemd daemon

  • sudo systemctl daemon-reload

enable Node-RED to run automatically

  • sudo systemctl enable nodered.service

Start Node-RED

  • sudo systemctl start nodered.service

Securing Node-RED

Useful management stuff

Learn about the current service status

  • sudo systemctl status nodered.service

(Re-)Start the Node-RED background service

  • sudo systemctl restart nodered.service

Stop the Node-RED background service

  • sudo systemctl stop nodered.service

Disable the Node-RED background service

  • sudo systemctl disable nodered.service

Get the service log since the last boot

  • sudo journalctl -f -u nodered.service -b -o cat

Also

Node-RED can then be started and stopped by using the commands:

  • node-red-start

and

  • node-red-stop

Updating

Apparently, just run the install again...

  • sudo npm install -g node-red

(pretty simple...)

Debugging Node-Red issues

ECONNRESET caused by MQTT

Error: Client network socket disconnected before secure TLS connection was established
Error: read ECONNRESET
Error: read ECONNRESET
Error: Client network socket disconnected before secure TLS connection was established

Hhhmmm...

ECONNREFUSED caused by alexa-home-conf

[error] [alexa-home-conf:919d2660.bd9218] Error: connect ECONNREFUSED 34.240.81.189:8883

Strange response from the creator: https://github.com/hardillb/node-red-contrib-alexa-home-skill/issues/51

Doesn't actually explain what's going on, but it's allegedly harmless. My question is: Why in fuck does it want to connect to a non-functional "alternate endpoint"?

Graceful restarts (maybe)

This is likely not needed when running under systemd...

From the Node-Red website

You can start a script as a daemon. But first install forever

sudo npm install -g --unsafe-perm forever

Then issue command:

forever start /usr/local/lib/node_modules/node-red/red.js

Things to do with Node-RED once it's installed

WIP:  These pages will appear as I get things updated from what's on the older wiki...

Some useful Node-Red tips

Some interesting Node-Red Add-Ons

  • Machine learning package for node-red
  • node-red-contrib-boolean-logic
    • Create "Buffer" from "Invert"
  • node-red-contrib-google-home-notify
  • node-red-dashboard
  • node-red-node-ping
    • There seem to be other versions... possibly better...
  • node-red-contrib-plex

Moving Node-Red to a new server

When creating a NEW server

Pretty easy actually...

Build the server & install everything as normal.

Then stop the node-red service and replace ~/.npm & ~/node-red with copies from the old server.

When Renaming a server

(i.e.: Moving or cloning a VM...)

Seems to lose the flows. :(

They're not actually lost. :)

The flow files are named with the server name.

You can select a flow file in ~/.node-red/settings.js

Uncomment the line that says: //flowfile: 'flows.json', and change flows.json to the name of your actual flow file. (in the same folder...)

Then restart node-red

sudo systemctl restart nodered.service

If you are using other services on the server (i.e.: MQTT), you may run into further weirdness like having to tell nodes where to find these services. More research still required on this...