Node-RED is a programming tool for creating automations more visually. It provides a browser-based Editor where you can create flows, aka, automations using a wide range of nodes. Node-RED is the perfect companion for Home Assistant. So, instead of writing automations in Yaml, you can simply wire together nodes to create a flow.
Table of Contents
This guide will show you how to set up and integrate Node-RED with Home Assistant. We’re going to look at the browser-based Editor, go over some of the available nodes, and also we’re going to create an automation so you can get started with Node-RED..
Install the Node-RED add-on
The first thing you need to do is install the Node-RED add-on so open Home Assistant go to Hass.io, Add-on Store, select Node-RED and then click on Install.
When the installation finish, go to Config and under credential_secret
set up a password that would be used for encryption.The Node-RED editor also has a dark theme available. To enable it, you can change the dark_mode
value from false
to true
.
Click on Save, and before you start the add-on, toggle the switch under Sidebar. So an option to open Node-RED becomes available on the Home Assistant sidebar. After that, click on Start and give it a couple of minutes for Node-RED to initiate.
After Node-RED is up and running, you can refresh Home Assistant, and the Node-RED option would now be available on the sidebar.
NOTE
If you have remote access set up with Home Assistant, there is no need to set up a port forwarding rule for Node-RED. The Node-RED add-on supports Ingress. This means that you can access Node-RED securely from Home Assistant anywhere you are.The Node-RED Editor
So, the browser-based Editor is separated in several sections. At the top, you have the Header, which has the Deploy button and the Menu. On the left, you have the Palette, containing all the available Nodes that you have installed. In the middle, you have the Workspace where you can drag and drop nodes and wire them together to create a flow. On the right, you have the Sidebar which contains panels that provides useful tools. For example, the information panel which shows details about a selected node. There is also the Debug panel which displays messages that are passed to a Debug node connected to a flow. We’ll look at the Debug node shortly, and go over the benefits of using it when creating flows.
The Home Assistant Nodes
There are a total of 12 Home Assistant nodes that you can use. The Node-RED add-on in Home Assistant already comes with the nodes pre-installed. So, you don’t need to manually install them. There is also no other configuration required for Node-RED to communicate with Home Assistant. To get started, we’re only going to look at the Events: state, Call service and Current state nodes.
The Events: state checks for a state change on a specific entity, and then it outputs that change to trigger a particular command in a flow. The Call service is used to send a request to Home Assistant to trigger a service. For example, to toggle a light on and off. The Current state gets the last known state of an entity and can be used to trigger different commands depending on that state. Kind of like using an IF statement.
How to set up a flow - Part 1
As an example, let’s create a simple automation that will turn a desk light on and off depending on the state of a computer. So, if the computer is turned on, it would turn the light on, and if it’s turned off, it will turn the light off.
Event state node
To listen for a change in the state of an entity, you can use the Events: state node. So, drop the node in the Workspace and then double click it to open the configuration panel. Set up a Name for the node. The Server is already configured to connect to Home Assistant so no need to configure that. Then, enter the Entity ID to check for a state change. In this case, it would be the computer switch. To save the changes, click on Done.
NOTE
The Entity ID, Domain, Service and Data fields, support autocomplete. Making it easy to select the right information.Switch node
Next, use a Switch node. Which allows setting 2 outputs. One for when the computer is on and the other one for when the computer is off. Drag and drop the Switch node in the Workspace and connect the Event State node to the switch node. Open the switch configuration panel. Set up a Name and then set up 2 rules, one for on
and the other one for off
. Click on Done, and the switch now has 2 outputs. If you hover over each output, it will show you a label with its value.
Call service node
To send the command to Home Assistant to turn the desk light on or off, use the Call service node. So drop it in the workspace and connect the on
output from the switch to the Call Service node. Set up a Name for the node. The Domain, set it to light
, the Service to turn_on
and then enter the Entity ID for the desk light. The Data field, leave it empty because you only want the light to turn on. However, if you have a light that you want to turn on with a specific brightness or color, you could set up the json information in the Data field. The Data field can be expanded and open as a text editor by clicking on the 3 dots on the right side. This allows typing the json information in a more comfortable way.
Now, do the same for the off
output. To make it easy, you can copy the Call service node to turn on the light by pressing Ctrl+C
and then paste it below with Ctrl+V
. Then connect the off
output from the switch to the Call service node. After that, open the node and edit the Name and change the Service from turn_on
to turn_off
.
Debug node
Now, before deploying the flow, connect a couple of Debug nodes at the end of the flow. Then using the Debug panel, you can see the message that is sent. Now, click on Deploy. Then, on the sidebar, change to the Debug panel, and you’d noticed that when the automation is triggered, it will show the message that was sent through the flow including the date, time and the node ID. You can click the node ID, and the Debug node that received the message would have a flashing dashed outline around it.
How to set up a flow - Part 2
Now, let’s say that you only want the automation to turn the desk light on when it’s dark outside. Also, to check the state of the light before sending the message to turn the light on. You can do these by using the Time range node and the Home Assistant Current state node.
Time range node
Between the Switch and the Call service node to turn on the light, drag the Time range node and drop it on top of the wire to insert it into the flow. Then, open the node and set up a Name for it. The Latitude and the Longitude are set up automatically for your location. However, you can still manually edit it if you need to. After that, set the Start time to sunset
and the End time set it to sunrise
.
Current state node
Next, drag the Current state node and drop it between the Time range node and the Call service node. Open the node, set up a Name for it, and enter the Entity ID for the light. Then, for the If state, change the condition to is not
and set the value to on
. This would create 2 outputs on the node. The first one would pass the message to turn the light on if the current state of the light is off, and the second one would just halt if the light is already on.
Now, add another Current state node, or copy the one above and drop it between the Switch and the Call service node to turn off the light. Then, open the node and edit the Name and set the If state to off
. After that, click on Deploy to save the changes.
If you look at the entire flow, you can easily understand what the automation is doing. When the computer is turned on, the on
message is passed to the switch. Which then goes through the on
output and if it’s night time, it passes the message to turn on the desk light. Then when the computer is turned off, the off
message is passed to the switch. Which then goes through the off
output and check if the light is on and if it is, it sends the message to turn off the light. Otherwise, it just halts.
All right, so I hope that this guide has helped you understand more about Node-RED and get you started. On future tutorials, I want to cover other nodes and create a few more automations that you guys can use. If you have any suggestions on what you guys would like me to cover on Node-RED, definitely let me know. You can always reach me via social media.