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.

TV Ambient Lighting, with Node-RED

I have a Hue LED strip mounted to the back of my 65″ TV, and I like to have it change color, according to input and activity on screen. It’s a pretty straight forward and easy setup, but one that people really enjoy.

Listed below is my setup and config, but I will try to show how to do this in the simplest way possible, in the hopes that it will work with whatever your setup is.

Hardware, and related integrations and addons:

(Amazon links are affiliate links, where I get a small commission of purchased item)

Software

  • Home Assistant
    • Node-RED addon
      • Built in nodes – (Nodes that are included with Node-RED)
        1. function node
        2. event: state (server-state-changed) node
        3. switch node
        4. call service (api-call-service) node

Don’t worry if you do not have this exact configuration, since we’ll be using all generic nodes, this should work with virtually all configurations.


To start, we’re going to need to get the status of the TV. To do that, we’re going to use the Activities from the Harmony Hub and remote. The Harmony Hub integration will automatically pull all the activities you created when you first setup your Harmony Hub. The names of those activities are located in the harmony config file located at the root of the Home Assistant config directory. Using the file manager of your choice (I use the Visual Studio Code addon) navigate to this directory and take note of the activity names. Case is important, so make sure to note them exactly as they are.

Now that we know the activity names, we need to figure out how to filter them and use them to kick off flows. To do that, we’re going to grab an “events: state” node, and configure it like so:

Then we’re going to grab a debug node, and configure like so:

And then we are going to connect the two, open the debug window and then start an activity on your TV remote. You’ll see the debug output on the right. This shows all of the information passed on by the state node

If you drill down to “data.new_state.attributes.current_activity”, you’ll see the name of the current Activity, in this example it is “Google TV”. We now need to take this path, plug it into a switch node, and filter the activities. Start by copying the path by clicking the little > emblem.

This will copy the path to your clipboard.

Next, grab a switch node, and paste the path that you copied into the Property field. Then, in the fields below make sure == is selected and then enter the Activity names. This will filter everything that comes in through “data.new_state.attributes.current_activity” to the corresponding output. Nothing else will get passed through. Now, connect the event state node you created earlier to the switch node, and connect properly labeled debug nodes to each output on the switch node. Don’t forget the PowerOff activity!

Switch node configuration

With them all connected, when you start an activity from your remote, you can hover over the debug output and it will highlight the debug node that it came from. Doing this, you can verify that the switch node is routing the payload correctly.

Now that we know when we press Google TV on the remote, it will route through the Google TV output of the switch (along with all the other activities routing through the correct output), we can start programming the lighting per output.

Grab a “call service” node node, and we’re going to program it to turn on the light when PS5 is selected. I like to Google the RGB code of whatever service it is that I’m working with. For example, I would Google “Netflix logo RGB color” and it will give me the RGB code for Netflix Red. Same with Playstation Blue, etc. With that info, the call service node to turn the light Playstation Blue, would look like this:

Domain is light, service is “turn_on. Click the three dots to open the JSON editor to program the settings for the light. “brightness_pct”: 55 will set the light to 55% brightness. “transition”:1.5 means that the light will reach 55% brightness when turned on in one and a half seconds. “rgb_color”:102, 92, 190 is the RGB color for PlayStation Blue. “entity_id” is the name of the light you want to control. You will need to repeat this until you have all the outputs done. One for TV, one for Nintendo Switch, etc.

Lastly, don’t forget about the PowerOff activity, to turn the lights off when you turn off the TV. It’s the same setup, different service. Grab another “call service node”, and program similar to before, except the only thing you need to worry about is the service, which is now “turn_off, and the entity_id of course. It will look like this:

The completed flow will look like this:

Here is a video showing how it can look. Keep in mind, in the video, I’m using a more advanced version of the flow to change colors based on the currently selected app in my Google TV, but the effect is the same. I may do a part two later to show how to do that. Also, don’t mind the creepy whispering at the end of the video, I was lightly bullying my buddy cause his TV isn’t this awesome.

Finally, here is the code.