Difference between revisions of "Automation - Node-Red"

From Da Nerd Mage Wiki
Jump to navigation Jump to search
Line 194: Line 194:


=== When Renaming a server ===
=== When Renaming a server ===
(i.e.: Moving or cloning a VM...)
No longer seems to be an issue with current version of Node-Red...


Seems to lose the flows. :(
<span style="text-decoration: line-through;" >(i.e.: Moving or cloning a VM...)</span>


They're not actually lost. :)
<span style="text-decoration: line-through;" >Seems to lose the flows. :(</span>


The flow files are named with the server name.
<span style="text-decoration: line-through;" >They're not actually lost. :)</span>


You can select a flow file in <code>~/.node-red/settings.js</code>
<span style="text-decoration: line-through;" >The flow files are named with the server name.</span>


Uncomment the line that says: <code>//flowfile: 'flows.json',</code> and change '''flows.json''' to the name of your actual flow file. (in the same folder...)
<span style="text-decoration: line-through;" >You can select a flow file in <code>~/.node-red/settings.js</code></span>


Then restart node-red
<span style="text-decoration: line-through;" >Uncomment the line that says: <code>//flowfile: 'flows.json',</code> and change '''flows.json''' to the name of your actual flow file. (in the same folder...)</span>


<code>sudo systemctl restart nodered.service</code>
<span style="text-decoration: line-through;" >Then restart node-red</span>
 
<span style="text-decoration: line-through;" >
<code>sudo systemctl restart nodered.service</code></span>


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...
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...

Revision as of 19:02, 24 January 2022

Proven on:
Logo Debian.png
11 (bullseye)


Logo LMDE.png
4


Logo Mint.png
19.3 / 20.3


Logo Ubuntu.png
20.04.3 issues


Logo Sparky.png
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 -y
  • sudo apt-get install -y build-essential
  • sudo apt install npm -y
  • 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

No longer seems to be an issue with current version of Node-Red...

(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...