Hello everyone, it’s Frenoy here and today, we are going to cover a very important that
is going to allow us to do a lot of different types of automations. If you have been following
along with the home automation series, then you will know that we have already done a
few things: First, we used home assistant as the base platform to integrate everything,
we then used ESPhome to add different sensors and nodes, we used influxDB to store the sensor
data and finally we used Grafana to visualize this data.
In last week’s video, we took a very quick look at automation to get an idea of how it
works and in today’s video, we are going to use Node-Red which will allow us to take
our automations to a whole new level. Node RED is a graphical tool that allows you to
visualize your automations. This means that you can simply look at the different nodes
to get an idea of what’s going on and how they all communicate with each other. This
is far better than having to change configuration files and having to write endless code. At
a very basic level, an automation consists of 3 things: input, processing and output.
Think of the input as a trigger, the thing
that actually gets the ball rolling for the automation. This input could be a change in
a sensor value, a switch being toggled, or even the simple act of a phone connecting
to the WiFi network when someone comes home for instance. All these triggers are then
processed to give us the output. The actual processing will depend on what we are trying
to do and we will take a look at an example later on in the video. Finally, we have the
output which could be as simple as switching ON a light, playing some music, sending an
email or something else. Think of it as the end result or final action. All of this will
make sense once we start using node-RED so let’s quickly set it up first and we will
then create some basic automations.
The first thing we need to is head over to the add-on store, then search for node red
and start installing it. It will take a minute to install and once that is done, scroll down
to the config section. We need to add a string that will be used for encryption and this
string could be anything like this. We then need to disable SSL as once again, home assistant
is running on our local network and that means we are protected behind our router. Once that
is done, simply enable the show in sidebar option so that we can easily access node-RED
and finally hit the start button. It will take a couple of minutes to start the first
time and you can refresh the logs to wait for it to start the nginx server. Once that
is done, you can simply open up the WEB UI to get something like this.
Remember I mentioned that an automation involves inputs, processing and some outputs? In node
red, we do all of that using these blocks which are also called nodes. These nodes can
be dragged into this workspace which is called a flow and they can be connected using links
like these. Finally, this section allows you to change different settings and view more
information about the nodes which can help you debug any issues.
Before we start creating an automation, we need an input or trigger. You could configure
a node in ESPHome to contain a toggle switch like we did in a previous video and that could
be used as a trigger. But an easier way to test things is by creating a dummy Boolean
input.
This page gives you an example configuration that can be used and all you need to do is
copy it and then add it to your configuration file. We have done this in a previous video
and we simply need to open configurator, navigate to the yaml file, paste it and then save it.
You could change the name, icon and so on but I’ll leave that up to you. Once this
is done, you need to restart home assistant and that can be done by navigating to hassio,
system and clicking the reboot option. Give the board a minute or two to reboot. Once
that is done, we simply need to add it to the dashboard so that we can toggle it. This
can be done by selecting the configure option, adding a new entity and then selecting the
name that we gave the Boolean input. We can then save this; exit the configuration view
and we can then use this toggle input for our automation. Right now, toggling it does
nothing so let’s fix that by opening node red.
The very first thing we need is a trigger and, in our case, it will be the state of
this Boolean input that we just created. If you hover over the events state node, you
will see that it can be used to obtain the state of an entity – this is perfect for
our needs so start by dragging it over to the flow. Keep a note that the side window
provides you with a lot of information that can be used to learn how to use the node.
First, we need to configure the node so that we can obtain the state of the Boolean input.
To do this, we can simply double click the node which will bring up this window.
Start by giving it a name. We then need to provide the entity ID, you can press the down
arrow to view all the available entities or you can start typing which will bring up the
prompt. Select the input Boolean entity, change the state type to Boolean and hit the DONE
button. And that’s all you need to do to configure the input. This node will give you
an output which is either true or false, depending on the state of the toggle input. We want
to switch ON the RGB light when the output is true, and we want to switch it OFF when
the output is false. In order to do this, we need to use the switch node to separate
the output, so drag that in, connect it to the input by clicking and then dragging and
finally, double click it so that we can configure it.
Start by giving it a name and then change the property to the isTrue option like so.
This means that we will receive a signal at the first output, when the input to this node
is true. We also need an output for the false signal so add a property, configure it to
isFalse and then click the DONE button to save it. The node will now have two outputs,
one corresponds to true and the other to false. We can now connect more nodes to these outputs
to define our flow or automation.
We need to switch ON the light when the output is true. This means that we need to call a
service so drag that node into the flow and double click it so that we can configure it.
Start by giving it a suitable name. We need to define a few things now the first being
the domain. If you click the domain box, then you will see a couple of options, select light
as we are going to control a light. Click the service box and select turn_on, enter
the entity id as shown here and finally, click DONE to save it all. Connect the node to the
true output as seen here. Next, we need to call a service to switch OFF the light and
that can be done by selecting the node and then using CTRL+C to copy and CTRL+V to paste
the node. Double click the new node to configure it. All we need to do is update the name,
update the service, and save it. Finally, we can connect this node to the false output
and we are ready to test this flow.
Before we test it out, I am going to enable debug messages so that you can get an idea
of what is going on. This can be done by double clicking the nodes and selecting the show
debug information option. Some nodes do not have this option, in which case you can use
the debug node and wire it to the point of interest as seen here. You can open up the
debug window to view all the messages.
If you look at the flow, it is quite easy to understand what is happening. We check
to see if someone has arrived, which is nothing but the state of this Boolean input. Ideally,
you would set it up such that it detects when you are home by either checking your phone
or by some other means. The answer to this question is either going to be a True or False.
If it is true, then we switch ON the light. If it is false, we switch OFF the light.
In order for this to work, you need to click the deploy button which will activate this
flow. I am going to open up another browser window so that we can view the dashboard along
with the debug messages that will be shown in node-RED. The light is currently OFF, but
it will switch ON the moment I toggle the input, its icon will change and you will also
be able to view the debug messages. If you hover over the message date-time information
then it will highlight the node which generated the message, which is pretty handy. You will
also be able to view when the event was last triggered below the nodes. Similarly, if we
toggle the input, then the light will be switched OFF as expected.
This was a very basic automation that can be used to do a lot of different things. We
are now going to update this such that the light will only switch ON during a certain
time interval and we will check the state of the light before we send a new message
to switch it ON or OFF.
In order to do that, I am going to start by dragging these nodes away as we need to add
a few more. Since we are interested in time, simply type in the word time to view the relevant
nodes. You can hover over them to view that they can be used for and you will see that
the time check node can be used to compare the current time to a predefined time. However,
I will use the time range node as I would like to switch ON the light when it is dark.
So drag it in, double click it to configure it and start by giving it a name. I will define
the start time as 7PM or 19:00 hours and the end time as 11PM or 23:00. The node has two
outputs, one tells you when you are within the time range that you’ve specified, and
the other tells you when you are out of this predefined range.
I am then going to delete the previous connection and connect it to the time range node. Next,
we need to check the state of the light before sending a new command. This can be done by
using the current state node. I am going to give it a name, define the entity and the
state that we are interested in detecting, which is OFF in this case. We can then connect
it all together and clean it up a bit like so.
I am then going to copy the current state node and drag it over the connection which
will automatically connect the node for us. I will change its name and also set the state
to reflect the ON state. I am then going to delete the debug node as we do not need it
any more. This gives us a beautiful flow like this. Before we test it, let’s quickly see
what’s happening here. We monitor the Boolean input which tells us if someone has arrived.
If they have arrived then this output will be true. We then check to see if it is night
time, and if it is, then a signal is sent to the next node which will check the state.
If the light is OFF, then there will be a signal at this output which will then switch
ON the light for us. However, if the light was already ON, then we will not send a new
command. Similarly, if no one has arrived, then this output will be false and that will
be sent to the next node which will check the light status. If it is ON, then we will
send a command to switch it OFF, otherwise we will not do anything further.
I am going to resize the browser, collapse these side bars so that we can view the entire
flow. The light is off and when I toggle the switch, it will still not switch ON. This
is because it is only 5:16PM which is outside the time range that we have set. This means
that there will not be a valid signal from this node. I’m going to manually switch
ON the light so that we can test the OFF section. Rightly so, when I switch OFF the input, the
light switches OFF as well and you can see the time stamp of the last command along with
the debug messages. For the last test, I am quickly going to extend the time range so
that the light switches ON from 5PM. I will then deploy this updated flow so that we can
test it. Now, when I toggle the input now, the light will switch ON instantly – which
is exactly what we wanted.
And there you have it – an extremely easy way of getting started with node-RED. You
can do a lot of stuff with node-RED and this is what we will be using for our automations
as it is very easy to visualize everything. You can integrate node-RED with a lot of devices
or entities to really customize your home. We will continue using it in future videos
so please do subscribe to stay updated. Thank you for watching and I will see you in the
next one.