Node Red for Beginners – 2021 EditionFebruary 22, 2021
Today on the hookup we’re going to take a beginners look at favorite program for home automation: Node Red. I’m going to show you how I use it in conjunction with home assistant to transform my connected home into a smart home.
It’s been a couple years since I’ve made a video about node red, but it’s as big a part of my smart home as it has ever been. In this video I’m going to make a case for why you should include node red in your smart home and show you how to get started with your first node red automations.
Thank you to Govee for sponsoring this video. Their latest product, the immersion LED strip combines a 1080p camera mounted above or below your TV with their colorsense controller to extend your TV’s colors onto the surrounding wall. The Govee immersion LEDs are specifically designed for TVs between 55 and 65 inches and come pre soldered with flexible corner sections so installing them is a breeze and you won’t need to do any cutting or bending of your LEDs. Because the system uses a camera instead of an HDMI connection you can use it with any TV input, including your TV’s built in smart apps. The immersion package is compatible with google home and amazon echo devices and also includes music reactivity and dynamic lighting modes. Check it out using the link in the description.
Did I throw anyone off by saying that node red, not home assistant is my favorite program for home automation? Well, lets start out with the most common question I get about node red. If I’m already running home assistant, why do I need node red, and conversely if I already have node-red, why do I need home assistant. On the surface it might look like node red and home assistant do the same things, but dig deeper and you’ll find that they excel in very different areas.
Home assistant is irreplaceable for bringing all your different devices under one unified platform and keeping track of the state of each of those devices. Home assistant’s automation and scripting user interface have come a long way in the last year, but I still find Node Red significantly more powerful AND user friendly due to its visual user interface and excellent real time output options, and setting up node red in your home assistant installation only takes a few minutes.
For the purpose of this video I’ll be using a fresh installation of home assistant running on the new Home Assistant Blue hardware, but the process is exactly the same for any full home assistant install whether you’re using a raspberry pi or virtual machine.
Assuming you’ve finished the initial onboarding process for home assistant you’ll click on the supervisor button on the left side and then select the addon store from the top bar. In the list of community addons go down to node-red and hit install.
Next head over to the configuration tab to add in a credentials secret like the documentation tells you to, and then go back and start the addon. You can always check the progress of the addon’s launch by looking at the log tab. In my case, node red failed to launch because I don’t have SSL enabled on this fresh home assistant install, and I told node red to use SSL in the configuration file. If you just installed home assistant you probably don’t have SSL enabled either, so set those two parameters in the configuration file to false, hit save, and then hit the start button one more time. This time everything loaded up properly, and it was time to hop into the Node Red web interface.
Inside node red there are a few different important areas. On the left side are all the preinstalled node types, each node is like a little skill that you give to node red, and most of the must have nodes are installed by default in the home assistant addon but there are thousands more specialized nodes made by node-red contributors that you can install if and when you need them, more on that later. The middle area in node red is where you’re going to make your automations, and you can create as many tabs as you like using the bar at the top to organize your automations. Technically each tab is called a flow, but I don’t love that terminology, and I often mistakenly call each automation or sequence a flow.
Before we go any further, me introduce you to the two most important node types and make our first sequence. If you’re unfamiliar with node red you should do this with me, I’ll wait. On the left side we’re going to find a node called inject, you could browse through the node list like a maniac, or you can just use the search bar to filter results in realtime. Pull that inject node into your main flow area. Inject is the second most important node for testing and learning in node red. It gives you a small button that you can press to immediately trigger an input for your automations.
If you double click on any node it brings up the editable properties of that node. For inject, you can send a timestamp, which is selected by default or you can choose between a bunch of other data types, we’re going to go with a string, which is the datatype for a bunch of letters in a row, like a word or sentence.
Under msg.payload we’ll type the string “I did it”, and under msg.topic we’ll put in “my first sequence”. Hit that done button, then hit deploy, which is essentially the “save” button in node red, and then click the little grey box on the side of the inject node. And just like that, nothing happens.
If we want to see the message we generated we need to utilize the most important node: the debug node. In your left column find the debug node and drag it into the main window. Then, you’re going to connect the two nodes by dragging the inject’s output which is always on the right side of a node to the debug’s input, which is always on the left side.
Hit deploy, then click your inject button. Still seems like nothing. But, if you open your debug window by clicking the little bug icon on in the right panel you’ll notice that says “I did it”, which was located in the msg.payload, and above that the msg.topic has the value “my first sequence”. As you get more advanced with node-red there are going to be a lot more message parts, so I always recommend changing your debug nodes to output the complete message object instead of just the msg.payload so you can see everything.
When viewing the complete message object our debug window now contains an exandable entry that contains each message part, which in this case is the message id, msg.payload and msg.topic. In programming we call this an object and each object can have many associated properties like topic and payload in this case. Just like I’m Rob, and Rob.eyes, Rob.hair, and Rob.shirt are properties with values specific to me but if you looked at someone else like DrZzs he’s got Justin.eyes, and Justin.shirt which have value specific to him, and he doesn’t even have the justin.hair property. Your objects can have as many properties as you want and each will be passed through your entire sequence as it goes from the output of one node to the input of the next.
The point of automation is to get your devices to behave a certain way without you needing to explicitly tell them what to do. Some people take this to the extreme by having lights and music turn on based on a specific person entering the room, but until my home can correctly identify my mood, my specific needs are not that predictable. So instead, I use automation to make physical controls more intuitive.
Lets look at my daughter’s room. She’s got RGBW LEDs under her bed, nanoleaf tiles on the wall, an overhead fan light, and a standing lamp. One switch on the wall is physically wired to the standing lamp and the other is wired to the fan light, but that doesn’t make much sense since these two lights should theoretically always get turned on together whenever you just want the room to be brighter.
Similarly the under bed lights and nanoleaf tiles seem like they should be turned on together for mood lighting, but neither of them are connected to the physical switches, so you’d normally need to use a voice command or a phone app to turn them on, which is not always convenient and definitely not intuitive.
Behind the switches I’ve got shelly smart relays which when set to detached switch mode enable me to separately monitor the state of the physical switch while independently controlling the lights for each circuit using their internal relay.
My end goal is to have the first switch control the fan light and floor lamp together and have the other switch control the nanoleaf tiles and underbed LEDs, and I want to do all this without messing with the wires in the wall.
The first thing I need to do is get all the devices into home assistant so they can talk to each other. The new shelly integration might be the most painless way to add a device to home assistant that I’ve ever used,
Unfortunately, I’m currently having some issues with it due to my VLAN setup, causing it to revert to local polling instead of local push. I’m in process of figuring that out, but for this video I’m going to set up my shelly devices using MQTT which requires editing of the configuration file.
For this reason, I’m going to set up my shelly devices using MQTT which required editing my configuration file, but if you use HACS you can also setup shelly for hass that utilizes CoAP for and much faster response time.
The only additional thing I’m going to do is add an MQTT binary sensor for each of the switches to monitor the actual switch position which is independent from the state of the relay when used in detached mode.
The Nanoleaf integration also requires adding a few lines of code to configuration.yaml file, but this isn’t a home assistant tutorial, so I’m going to assume you know how to get your devices added. Once you’ve added all the necessary entities you can check your home assistant configuration and then restart home assistant.
Once home assistant comes back up, which is amazingly quick these days, you’re going to take one more step before jumping into node red, which is to restart the node-red addon. If you skip this step your new entities may not auto-populate in node red, which will make your life much more difficult.
Okay, lets get automating.
Lets start easy, we’re going to get our switches configured the way we want them. Pull out the home assistant state node and select the binary sensor output for the switch that we added earlier. In my case I’m going to configure the first and third switch on this panel.
I happen to know that the two states that this node will output are “on” and “off”, but remember, if you ever need to see what is coming out of a node you can always use a debug node which will show you the exact output in the debug window. The next node we’ll go into is a switch node, which is like a fork in the road. We want one of the forks to correspond to an “on” payload and the other one should be for “off”. If the msg.payload is exactly “on” it will travel out of the top output, and if it’s exactly “off” it will travel out of the bottom output. The switch node has a ton of different options for analyzing the properties of your message object, but in this one we’re just going to leave it as equals equals, which means the values are exactly equal.
When the first switch turns on it will generate an “on” payload which will travel up through the switch node, and then we want it to turn on both the floor lamp and the ceiling light. To do this we’ll use a home assistant call services node. In this node set the domain to “switch”, and the service to “turn on”. Then, under entity ID you can just start typing the names of the home assistant entities that you want to turn on and the field will auto complete. If you want to turn on more than one entity you can just separate them with a comma and keep typing. In my case I want to specify the entity ID for the ceiling light and floor lamp.
When the switch turns off it will generate an “off” payload and travel out of the bottom output of the switch node. Since I want to control the exact same entities I can just copy and paste the call services node that I just set up, but change the “turn on” service to “turn off”.
For the third switch we can repeat the exact same process, but because the RGBW2 and nanoleaf are classified as lights in home assistant and not switches I’ll change the domain to light instead. I’ll enter the entity IDs for the nanoleaf and the shelly RGBW2 and configure the turn on and turn off services accordingly.
At this point the automation is complete and after hitting deplay the lights should work as expected, but we can do better, so lets kick it up a notch.
The nanoleaf has all kinds of effects, and the RGBW2 can of course be colors other than white. Again, normally to control those attributes you use a voice command or a phone app, but there’s a better way. Usually if you are going to put on colored lighting you’ll use it alone, but if you just want the room to be bright you’ll turn on all the lights to white.
So to intuitively control these colored devices we’ll check if the other lights in the room are on, and then decide what to do based on that data. To check the specific state of a home assistant entity you can use the “current state” node which will output either on or off for a switch, and a bunch more information for a light.
In this case we’ll split our output one more time based on the current state information. Using a switch node after the current state node we can add another fork in the road to send different turn on commands.
In addition to turning a light on and off the call services node can also specify tons of other attributes under in the data field. For the mood lighting I had my daughter pick out the colors, effects, and brightness that she wanted using the home assistant app and then I used the current state node discover their exact values. In the debug window you can click on the “Copy value” button next to attributes to get all the current values in JSON format.
If the current state of the ceiling light is off my daughter wants the nanoleaf to turn on using the ocean effect at 192 brightness, and the bed LEDs to be set to teal at 204 brightness.
However, if the room lights are already on, the nanoleaf and bed LEDs should turn on to warm white instead. As I said, any attribute that you want to specify should be given in json form in the “data” field. If you’ve copied information from the debug window you can just paste it into the data field and delete any unneeded attributes, which will ensure that your data is given in the right format.
After hitting deploy there are no errors, so we should be able to walk into her room and flip the first light switch to turn on the ceiling light and floor lamp together. If I then flip the third switch the nanoleaf and underbed LEDs come on in warm white.
If I instead flip the third switch then the nanoleaf tiles and underbed LEDs will come on to the mood lighting that my daughter requested. If she ever wanted to have the colored lights on with the white lights she can just flip the third switch switch and then the first switch and then to change the nanoleaf and bed lights to white, she just needs to flip the third switch on and off. Perfect.
This is a small example of the automations that can be accomplished with node-red, but really your imagination is the limit. Within the built in nodes you’ve got things like http, tcp and mqtt based messages, tons of home assistant specific controls, and even the ability to send and receive messages via things like twitter, amazon echo, and email. However, that only scratches the surface of possible nodes, and community developed nodes can be easily added via the palette manager allowing more control over your devices and the ability to add almost any functionality you can dream of.
If this video left you wanting more, don’t worry I’ve got a second node-red video planned in the very near future, but I want to hear your automation ideas. No matter how crazy or far fetched they seem, leave them down in the comments and I’ll do my best to make them happen and include them in my next node-red video. To sweeten the pot a little bit more, if I end up selecting your automation, not only will I credit you in the next video, but I’ll also send you a nice giveaway pack including shelly and tuya gear. All you need to do is comment with your most creative and useful automation idea.
A big thanks to all of my awesome patrons over at patreon for your continued support of my channel. If you’re interested in supporting my channel please check out the links in the description. If you enjoyed this video please hit that thumbs up button and consider subscribing. And always, thanks for watching the hookup.