Node-RED: connect devices and services without code


Node-RED is a popular system that allows you to link devices and services without entering any code. After all, programming is done graphically by connecting nodes in flows. There is a thriving community around Node-RED and in addition to automating things, you can also create dashboards. Recently version 1.0 of Node-RED was released. The ideal opportunity to get started with this article.

On the Node-RED website the program is described as “low-code programming for event-driven applications”. That’s quite a mouthful. Low-code programming simply means that you don’t have to type any code, unlike, say, a Python program. Instead, in a web interface, select components from a toolbox and link them together.

“Event-driven applications” means that you build applications that respond to events with Node-RED. For example, your program can respond when you press a button that connects you to a Raspberry Pi, or a message sent via MQTT over your home network, or a sensor value that changes in your Home Assistant installation.

01 System requirements for Node-RED

Node-RED can be installed on Windows, macOS, Linux, on a Raspberry Pi and even in the cloud. On the page Getting started you will find the installation instructions for the various options.

In this basic course we assume an installation via Docker on a Raspberry Pi. If you make another choice, follow the specific instructions for your system on the Node-RED website. After installation, almost everything works the same regardless of the system on which you run Node-RED.

01 You can install Node-RED in many different ways and various devices.

02 Raspbian with Docker

We assume you Raspbian Buster Lite installed on your Raspberry Pi, for example with balenaEtcher. You log in to your Raspberry Pi via the command ssh in Command Prompt or by connecting a keyboard and display to your Raspberry Pi.

Then install Docker on the Raspberry Pi, give the standard user pi access to Docker, install pip and Docker Compose, and finally reboot with these commands:

curl -sSL https://get.docker.com | sh

sudo usermod pi -aG docker

sudo apt install python3-pip

sudo pip3 install docker compose

sudo reboot

Once you are logged in again, you also create a directory where you store the Node-RED data files, such as your flows and the extensions you install:

mkdir / home / pi / node-red

03 Install Node-RED

We install Node-RED using Docker Compose. First create a Docker Compose file:

nano docker-compose-node-red.yml

Place the following lines in it:

version: ‘3.7’

services:

node-red:

image: nodered / node-red

container_name: node-red

restart: always

volumes:

– ./node-red:/data

ports:

– “1880: 1880”

environment:

– TZ = Europe / Amsterdam

Here we define that we have the Docker image nodered / node-red download and start, taking the directory node-red in the current directory in the Node-RED Docker container mount to the /data. We forwarded port 1880 from the container to port 1880 on the Raspberry Pi. Finally, we also set our time zone so that Node-RED uses the correct time.

Save the file with Ctrl + O and close nano with Ctrl + X. Then we simply start Node-RED with the following command:

docker-compose -f docker-compose-node-red.yml up -d

That will take a while, because Docker will now download the image of Node-RED first. But when you see Command Prompt again, Node-RED has started and can be reached in your web browser at http: // IPADRES: 1880. If it doesn’t work, check with docker logs node-red what goes wrong.

03 The logs of Node-RED show what the program starts in the background.

04 Your first flow

The Node-RED web interface consists of four parts. At the top you have the header with a button on the right Deploy and on the far right the menu that gives you access to all kinds of functionality.

On the left you have the palette, in which you will find all kinds of “nodes”. You can drag those nodes to the middle, the workspace. That workspace initially contains one tab with a “flow” called Flow 1. On the right you have the sidebar, which gives you more information about the selected item. At the moment that is the empty flow, so you will see the name of the flow there.

Let’s make a first flow. Drag a node inject and a node debug to the workspace. The text for the first one changes to timestamp and the second in msg.payload. Connect the right gray box of the first node to the left gray box of the second node. Then click on top right Deploy.

Then click the inverted triangle under the menu and choose Debug messages. If you then click on the large square to the left of the node timestamp the time (in seconds since January 1, 1970) appears in the sidebar.

04 Our first flow in Node-RED shows the time in the debug window when you click on the first node.

05 Install additional nodes

That time in the debug window is not very legible. Could we convert this to a legible date and time? Of course, and you can do that with a node, for example function where you can specify javascript code that will run the command. But we started this article with the message that you should not have to program in Node-RED (which is possible, see also the box “Javascript code in Node-RED”), so we are going to execute this graphically.

In the palette on the left you see a lot of nodes that Node-RED already installs by default, but there is none in between to show times in legible format. Fortunately, Node-RED has a thriving ecosystem of external nodes that are easy to install. Click on the menu and then on Manage palette.

In the tab Install you see that there are more than 2400 modules available for installation. Type in the search field date in. You will then see all possible nodes that do something with a date. Be sure to pay attention to the date of the last update, because you will also find many nodes that are no longer maintained. For each node you can click on the icon next to the name for more information.

05 More than 2,400 nodes are available to install in Node-RED.

06 Readable time

Press the button install at the node node-red-contrib moment and confirm with Install. Then close the window. Now if you scroll all the way down in the panel on the left, you will see two new nodes: moment and humanizer. If you click on one, you will see useful information about the usage in the sidebar.

Disconnect timestamp and msg.payload by selecting them and pressing the Delete key. Then drag the node moment to the workspace. She will then be given the name Date / Time Formatter. Connect the output of timestamp with the input of this node, and the input of msg.payload with the output.

Deploy the flow again and click the timestamp. The debug window should now have a time such as “2020-02-11T14: 15: 56.321Z” to show up. If the time is wrong, double click on the node and check if your time zone is correct. If necessary, add Adjustment up an hour. Don’t forget after every adjustment Deploy to click.

06 With the node “moment” you can easily convert the number of seconds since 1 January 1970 into a legible time.

07 Download every five minutes

Node-RED now does something when we click on the first node, but we can also have our flow run automatically, for example every five minutes. Click on the first node, choose at Repeat in front of interval and then fill every 5 minutes in. Also change the name of the node to Every 5 minutesso that the function is clearer. Save with Done and then click Deploy. You will see the current time every five minutes in the debug window.

Now that we can get Node-RED to do something every five minutes, we’re going to make it more useful: download weather data. Remove the node Date / Time Formatter and put a node instead http request. Double click on it and fill in the url http://xml.weerslag.nl/xmlfeed.xml in. Name the node Weerslag.NL.

If you now click on the square at the first node (or wait five minutes), the XML file from Weerslag.NL will be downloaded and the content will be shown in the debug window. But of course you are only interested in the weather with you. Open the feed in your web browser and search for a weather station near you. Write down the station code.

07 Download an XML feed in Node-RED every five minutes.

08 Filter the XML code

You can easily filter the XML code of the Weerslag.NL feed so that you only see the information from the weather station you are interested in. Therefore, place a node xml between your node http request and the node debug. This node sets the xml into a javascript object. First look at the result in the debug window.

Then add a node change between xml and debug, and double click on it. Now change behind to the type of output to expression (shown as a stylized capital letter J). Enter the following expression behind it:

payload. **. weather station[stationcode[0]= ‘11179’]

Replace in here 11179 by the station code from your neighborhood. If you want to know more about this type of expression, read the documentation of JSONata. After a deploy and a click on the first node, the debug window only shows the information from one weather station.

08 Filter one weather station from the XML code.

Javascript code in Node-RED

If there is no node that does what you need, you can still start programming. After all, Node-RED is written in javascript and supports via the node function also javascript code. So you would be the node moment can be replaced by a node function, in which you write javascript code that creates a Date object based on the payload, and replaces the payload with the string version of that object. This is a simple example, but you can write much more complex functions. See the online documentation page Writing functions for more information.

Node-RED also allows you to write javascript functions yourself.

09 Temperature and air pressure

Until now, we showed full javascript objects in the debug window, but we are really only interested in temperature and air pressure. We simply filter out the temperature with a node change after the previous node change. We change the type behind to to msg and fill in there:

payload.temperature GC[0]

Hang another one behind this node debug-node. Now do the same to filter out the humidity. In the debug window, study the contents of the full weather station javascript object to see what you have in the node change should type.

If you now run the flow, you will see the values ​​of temperature and humidity in the debug window.

09 Filter the temperature and humidity from the weather station data.

10 Dashboard

The debug window is useful if you are building a flow in Node-RED, but as soon as it works, you obviously want to display your data more nicely. Fortunately, Node-RED also has a dashboard. For that we need to install an extra node first.

Open again Manage palette in the menu and install the node node-red dashboard. That adds no less than sixteen new types of nodes to your palette.

Then hang a node gauge behind the node for the temperature. Double click on it and click next to Group on the pencil icon. Name the group (eg Temperature) and click next Tab on the pencil icon. Name the tab (eg Again), click on Add and again Add. Then you are back in the properties of the node gauge. Refill Label a name like Temperature in, at Units the unit ° C, and with Range a minimum and maximum temperature. click on Done to save the node.

After you click Deploy Click on the icon of the graph at the top right (left of the inverted triangle). Then click on the icon of the square with the arrow out. You will now see your first Node-RED dashboard. Obviously this is just the beginning. We leave it up to you as a reader to add the humidity and some other interface components such as a line plot.

10 In Node-RED, you can easily create a dashboard to plot data.

11 Check if it is freezing

Node-RED can do much more. For example, you can send out warnings when it freezes. To know when it’s freezing, add one switch behind the node for the temperature. Name that switch Test for frost and set up the test <= 0, where just before the 0 you enter the data type number choose.

The node at the output of the switch is now only executed when the temperature in the weather station is below 0. Test that by one inject– node to be linked to the input with the value –15 with data type string, and by one debug-node to connect to the output. If after a deploy on the inject click, you will see the temperature in the debug window –15 degrees. And if you do the –15 in the inject-node changes to 15 and on the inject-node clicks, nothing happens.

11 We test our control for frost.

12 Frost warning

Then we only need the latter debug-node to be replaced by a node that warns us. Here we give the example of a warning with an email. Install the node for that node-red-node-email.

Before you add that node, you add a node change behind the switch. Give it the name E-mail and put msg.topic on It is freezing. This will be the subject of our email. The message itself arrives msg.payload, and that still contains the temperature, so we keep that.

Then add the node e-mail behind the node change. Enter the correct information: the recipient’s email address, the smtp server, port, user account and password. If you use a mail server with STARTTLS, you serve Use TLS? check it, but Use secure connection not. And if you’ve set up two-factor authentication for Gmail, you’ll first need an app-password to create. If you then deploy your flow, you will receive an email notification when it freezes.

Of course this is only a first version. Currently, you would get an email every five minutes while it freezes. You should definitely limit that number of emails. But Node-RED offers enough components to further develop this basic flow. And if the default nodes are not enough, you can easily install additional nodes from the community or program your own nodes in javascript.

12 Our full flow that downloads the temperature and humidity every five minutes, shows it in a dashboard and sends an e-mail in case of frost.
.

Recent Articles

Related Stories