Node-RED Basics

If you’re here, it likely means you are at least aware of both Home Assistant and Node-Red and even if you’re not, this is a good place to learn. I hope to provide enough info in this article to help someone who is new to Node-RED, understand the basics.

If you’d rather get your information directly from the source, nodered.org provides all the information (and more) that I am covering here. I plan to provide examples and other articles to show concepts along with the info, but nodered.org is a great place to start as well.

This info can be found here – nodered.org “Node-RED Concepts”


.

.

What is Node-RED?

Quoted directly from nodered.org

Node-RED is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single-click.

nodered.org – Working with messages

To further expound on this, Node-RED is a way to visually program and code. Nodes are wired together to form a flow. A message containing a payload is passed from node to node down the flow. These flows form an automation. The flow starts at the beginning, and as the payload travels down the flow, different things happen depending on the node used, this is the automation of the flow.

An example of a Node-RED flow, that turns on a light based on motion events.

.

.

What is a Node-RED node?

Quoted directly from nodered.org –

A Node is the basic building block of a flow.
Nodes are triggered by either receiving a message from the previous node in a flow, or by waiting for some external event, such as an incoming HTTP request, a timer or GPIO hardware change. They process that message, or event, and then may send a message to the next nodes in the flow.

A node can have at most one input port and as many output ports as it requires.

nodered.org – Node-RED Concepts

Nodes can be thought of as a representation of something happening in your smart home. For example, there are “stoptimer” nodes that will trigger a timer based on the incoming message, and stop that timer based on the incoming message. There are nodes that monitor events, be it from a Home Assistant service or a piece of hardware on your network like a motion sensor, that will trigger based on how it was configured. There are call service nodes, that will start, stop or change a service, such as light.turn_on, light.turn_off and light.toggle. These are just a few examples of almost countless types of nodes.


.

.

An overview of the Node-RED workspace.

This is the Node-RED workspace. Along the top, we have different tabs for different flows, or automations. On the left, we have the node palette, and the right we have the sidebar. The deploy button is also up at the very top right.

There are a few different way to access these areas of the workspace.

For the sidebar, it can be accessed by pressing “ctrl+space”, hovering your mouse curser over the edge of the screen, or by selecting it in the top right hamburger menu.

To access the node palette, it’s basically the same process.

There’s one more way to access the node palette and that’s by “ctrl+left” clicking a blank spot on the workspace.


.

.

Inject nodes, debug nodes and debug messages

I think that one of the first things someone should know, is how to use the Inject, and Debug nodes. These can be invaluable when it comes to troubleshooting why something isn’t working.

Start by ctrl+right clicking a blank spot on the workspace, and selecting “inject” –

This will drop an inject node on the workspace. Double click the inject node, which will open the edit screen for that node. Click the “timestamp” drop-down and change it to “string” –

Now, fill in the name, and type something in to the string field. Click done. These can be anything for this example. –

Now, grab a debug node, following the same process as before, but instead of selecting the inject node, select the debug node. Once it drops to the workspace, double click the debug node, and leaving everything else the same, give it a name. This can be anything for this example.

After clicking done, you will be brought back to the workspace. Now, connect the two nodes by clicking the output of the inject node, and dragging to the input of the debug node. You’ll notice the deploy button is now bright red. Click the down arrow on the deploy button, and for now, select “Modified Nodes”.  If the inject and debug nodes are connected, click deploy.

After this is done, click the tab that’s on the left of the inject node, open the sidebar and click the tab to open the debug messages. Now, you can see  the payload in the dubug message, which will be whatever it was you typed into the string field earlier.

Double click the inject node to open the editor, and type something into the Topic field. Then click the tab on the inject node, and you’ll notice the topic you just added does not show the message.

This is because the debug node is configured to only show the msg.payload, and we need to set it to show the entire message. Start by double clicking the debug node, to open the editor. Select the down arrow next to msg, and select “complete msg object”.

Once this is done, click the tab on the inject node, and you’ll now see the complete debug message. Click the arrow to expand the message, and you’ll see both the payload, and the topic.

This is a very basic breakdown of the Inject and Debug nodes, but you now know the basics on how they work.


.

.

A few basic examples of flows.

Persistent Notification Example

A persistent notification are the notifications that show up in the left hand side bar in your Home Assistant Lovelace frontend.

These ‘uns.

Here is the official documentation on creating these – Persistent Notification

Setting these up is pretty straight forward, and we’re going to do it the simplest way possible, using an Inject node, and a call service node. Grab both of these nodes, and connect them as we previously discussed in this article. They will look similar to the below screenshot.

Open the inject node, and basically remove everything from it, then click Done. In the call service node config, set Domain to persistent_notification, set Service to create, and set Data to {“message”:”This is a test notification”}, then click done.

Now when you click the tab on the inject node, and go take a look at your Home Assistant front end, you’ll see the notification bell with a badge, letting you know there’s a waiting notification.

When clicked, you’ll see the test message you put into the call service node.

Tada!

Restart Node-RED example

This is another straight forward example. Start by grabbing an inject node, and a call service node and connect them, like you did in the first example. Open the inject node, remove all the fields and name it Restart, click done. Open the call service node, and set Domain to hassio, set Service to addon_restart, and set Data to {“addon”:”a0d7b954_nodered”}. Click Done.

Now, when the tab on the Restart inject node is click, Node-RED will be restarted.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s