Difference between revisions of "Automation - Node-Red"

From Da Nerd Mage Wiki
Jump to navigation Jump to search
 
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{{!}} class="wikitable" style="float: right; width: 322px;" border="2"
{{{!}} class="wikitable" style="float: right; width: 322px;" border="2"
{{!}}+ Proven on:
{{!}}+ Proven on:
{{!}}- <!-- Debian -->
{{!}}- --="" debian=""
{{!}} style="text-align: center; width: 72.1125px;" {{!}} [[File:Logo Debian.png{{!}}60px{{!}}link=https://www.debian.org/{{!}}center{{!}}middle{{!}}frameless]]
{{!}} style="text-align: center; width: 60px;" {{!}} [[File:Logo Debian.png{{!}}60px{{!}}link=https://www.debian.org/{{!}}center{{!}}middle{{!}}frameless]]
{{!}} 11 (bullseye)
{{!}} style="text-align: center; width: 40px;" {{!}} 11 (bullseye)
{{!}}
{{!}}
<br>
<br>
{{!}}- <!-- LMDE -->
{{!}}- --="" lmde=""
{{!}} style="text-align: center; width: 65.675px;" {{!}} [[File:Logo LMDE.png{{!}}60px{{!}}link=https://linuxmint.com/edition.php?id=279{{!}}center{{!}}middle{{!}}frameless]]
{{!}} style="text-align: center; width: 65.675px;" {{!}} [[File:Logo LMDE.png{{!}}60px{{!}}link=https://linuxmint.com/edition.php?id=279{{!}}center{{!}}middle{{!}}frameless]]
{{!}} 4
{{!}} 4
{{!}}
{{!}}
<br>
<br>
{{!}}- <!-- Linux Mint -->
{{!}}- --="" linux="" mint=""
{{!}} style="text-align: center; width: 67.5125px;" {{!}} [[File:Logo Mint.png{{!}}60px{{!}}link=https://linuxmint.com{{!}}center{{!}}middle{{!}}frameless]]
{{!}} style="text-align: center; width: 67.5125px;" {{!}} [[File:Logo Mint.png{{!}}60px{{!}}link=https://linuxmint.com{{!}}center{{!}}middle{{!}}frameless]]
{{!}} 19.3 / 20.3
{{!}} 19.3 / 20.3
{{!}}
{{!}}
<br>
<br>
{{!}}- <!-- Ubuntu -->
{{!}}- --="" ubuntu=""
{{!}} style="text-align: center; width: 65.7px;" {{!}} [[File:Logo Ubuntu.png{{!}}60px{{!}}link=https://ubuntu.com/{{!}}center{{!}}middle{{!}}frameless]]
{{!}} style="text-align: center; width: 65.7px;" {{!}} [[File:Logo Ubuntu.png{{!}}60px{{!}}link=https://ubuntu.com/{{!}}center{{!}}middle{{!}}frameless]]
{{!}} 20.04.3
{{!}} 20.04.3
{{!}} <span style="color:#8B0082">issues</span>
{{!}}         <span style="color:#8B0082">issues</span>
<br>
<br>
{{!}}- <!-- Sparky Linux -->
{{!}}- --="" sparky="" linux=""
{{!}} style="text-align: center; width: 65.7px;" {{!}} [[File:Logo Sparky.png{{!}}60px{{!}}link=https://sparkylinux.org/{{!}}center{{!}}middle{{!}}frameless]]
{{!}} style="text-align: center; width: 65.7px;" {{!}} [[File:Logo Sparky.png{{!}}60px{{!}}link=https://sparkylinux.org/{{!}}center{{!}}middle{{!}}frameless]]
{{!}} 5.11
{{!}} 5.11
{{!}}
{{!}}
<br>
<br>
{{!}}}
{{!}}}
[https://nodered.org/ Node-Red Web Site]
[https://nodered.org/ Node-Red Web Site]


Line 39: Line 42:
== Installing Node-Red ==
== Installing Node-Red ==
=== Start by installing nodejs and the javascript package manager (npm) ===
=== Start by installing nodejs and the javascript package manager (npm) ===
<span style="color:#8B0082">'''NOTE:''' The Ubunto repositories serve up an outdated version of Node.js. (v10.19.0)</span>


<span style="color:#8B0082">Node-Red 2 requires v12...</span>
<span style="color: rgb(186, 55, 42);" >'''FFFfffuuuuuu...'''</span>


[[Getting nodejs v12{{!}}Getting nodejs v12]]
nodejs & npm versions are problematic.


* <code>sudo apt install nodejs</code>
For some reason, the versions available in all of the repositories tend to be out of date AND all attempts to update them fail in weird & wonderful ways.
* <code>sudo apt-get install -y build-essential</code>
 
* <code>sudo npm install npm@latest -g</code>
[[Getting up-to-date NodeJS & NPM{{!}}I have found a procedure that seems to work]]
** (Doesn't actually seem to change anything on Debian... Updates to 8.3.0 on Ubuntu.)


=== Use npm to install Node-RED ===
=== Use npm to install Node-RED ===


* <code>sudo npm install -g node-red</code>
* <code>sudo npm install -g node-red</code>
[[File:Node-red install output (2022-05-14).png{{!}}300px{{!}}left{{!}}thumb{{!}}Latest WTF?]]
<div style="clear: both"></div>


=== Run it... ===
=== Run it... ===
Line 65: Line 68:
* <code>sudo wget <nowiki>https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop</nowiki> -O /usr/bin/node-red-stop</code>
* <code>sudo wget <nowiki>https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop</nowiki> -O /usr/bin/node-red-stop</code>


<span style="color:#4B0082">'''Note:'''</span> These 3 files are kinda rpi specific...
===== <span style="color:#4B0082">'''Note:'''</span> These 3 files are kinda rpi specific... =====


<span style="color:#4B0082">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 <code>/lib/systemd/system/nodered.service</code> and change the lines 11, 12 and 13.</span>
<span style="color:#4B0082">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 <code>/lib/systemd/system/nodered.service</code> and change the lines 11, 12 and 13.</span>
Line 72: Line 75:


<span style="color:#4B0082">Replace the user name "pi" with a user on your actual system...</span>
<span style="color:#4B0082">Replace the user name "pi" with a user on your actual system...</span>
===== <span style="color: rgb(224, 62, 45);">'''Another Note:'''</span> If you find that you've outgrown Node-Reds available environment space... =====
'''i.e.:''' it starts crashing with a message like: <code>'''FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory'''</code> in the logs...
You can try editing line 16 of <code>/lib/systemd/system/nodered.service</code> and change the number 256 to something larger. (within as-yet undetermined limits...)


==== make the two scripts executable ====
==== make the two scripts executable ====
Line 185: Line 196:
== Moving Node-Red to a new server ==
== Moving Node-Red to a new server ==
=== When creating a NEW server ===
=== When creating a NEW server ===
<span style="color: rgb(186, 55, 42);" >'''NOTE''': If you are updating to a much newer version of Node-Red, this is not likely the best technique...</span>
Pretty easy actually...
Pretty easy actually...


Line 192: Line 205:


=== 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...
 
<span style="text-decoration: line-through;" >(i.e.: Moving or cloning a VM...)</span>


Seems to lose the flows. :(
<span style="text-decoration: line-through;" >Seems to lose the flows. :(</span>


They're not actually lost. :)
<span style="text-decoration: line-through;" >They're not actually lost. :)</span>


The flow files are named with the server name.
<span style="text-decoration: line-through;" >The flow files are named with the server name.</span>


You can select a flow file in <code>~/.node-red/settings.js</code>
<span style="text-decoration: line-through;" >You can select a flow file in <code>~/.node-red/settings.js</code></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;" >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>


Then restart node-red
<span style="text-decoration: line-through;" >Then restart node-red</span>


<code>sudo systemctl restart nodered.service</code>
<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...

Latest revision as of 16:39, 7 March 2023

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)

FFFfffuuuuuu...

nodejs & npm versions are problematic.

For some reason, the versions available in all of the repositories tend to be out of date AND all attempts to update them fail in weird & wonderful ways.

I have found a procedure that seems to work

Use npm to install Node-RED

  • sudo npm install -g node-red
Latest WTF?

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

Another Note: If you find that you've outgrown Node-Reds available environment space...

i.e.: it starts crashing with a message like: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory in the logs...

You can try editing line 16 of /lib/systemd/system/nodered.service and change the number 256 to something larger. (within as-yet undetermined limits...)

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

NOTE: If you are updating to a much newer version of Node-Red, this is not likely the best technique...

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