I’ve been helping a fair few people online recently with some introductory operations in node-red. I thought I would write a kind of primer to help the beginner get up to speed with some functional flows. I see a lot of information online about how to install, and how to send commands using various protocols, but I thought i’d try and wrap everything up as simply as I could. I’m going to make two assumptions here.
1) You have node-red installed and connected to Home-Assistant. That is, you have the home-assistant nodes already installed in your palette.
2) You have an mqtt broker configured and working on your network.
I don’t want to spend time going through the above, so if you’re not quite at that stage yet, then maybe better to come back here once you have that figured out. If you’re using Hassio / home-assistant supervised, then MQTT and Node-red are both add-ons which are simple enough to install anyway.
I also want to make this point up front. There are many different ways of achieving the same outcome with Node-red. You need to find the solution that works for you. I’m not much of a coder, so most of my logic will involve switch/gate nodes. Others would prefer to write logic statements in the function node. That’s fine, both are equally as relevant. I prefer to opt for simplicity. It makes it easier to duplicate, amend and more importantly debug. One of the most important functions of Node-red is the debug window, it’s found on the right hand side if the program. It’s a tab identified by a small bug.
This output will be your best friend in node-red. We will be referring to it time and time again. Just underneath it to the right, there’s the trash icon. We will use this to clear the debug window so as to start a fresh as needed. This clears only the debug window, it does not delete anything else. With that in mind, let’s get started.
MQTT send and receive
There are two MQTT nodes. MQTT send and MQTT receive. Drag out the MQTT receive node and double click on it. You will need to configure the connection tab and the security tab with the relevant connection details. For me I filled in the following:
Server, port, username and password. Once that’s done, connect the mqtt to a debug node and hit deploy. On the right hand side of the node-red window, if you click on the debug tab you’ll see the relevant feedback. Mine is saying “missing broker configuration.” This is because we didn’t select a topic for the MQTT node to subscribe to. Double click the MQTT node again and now enter the topic. You might have something already set up that you would like to listen to (maybe the output of a sensor or another device that’s constantly publishing), so you can enter that here. For now I am going to enter the following in the topic field: +/+/+
In MQTT speak, I have basically just told the MQTT node to receive everything. Hit deploy again and see what the results are. If you have a lot of devices on your system already you’ll probably be overwhelmed with the output, i.e. pages of strings and text. Let’s cut that down again. I have a variety of tasmota devices on my network. They all report using the following syntax: stat/(name of device)/POWER.. therefore I am going to change my topic +/+/+ to stat/+/+
I hit deploy and now I am looking at the power state of just my tasmota devices. I am going to drill down further. I have a tasmota flashed plug in the bedroom called bedroom (unsurprisingly). I’ll change the topic now to stat/bedroom/+
I am now recieving just the state of that one plug. If I toggle that plug, the debug message shows the new state.
This debug shows us the following. The payload for when the switch is turned on and when it’s off is ON and OFF respectively (case-sensitive). It also shows us that I am dealing with POWER1. (this particular plug is bliztwolf SHP5 and so has POWER1 for the main socket, but also POWER2 which controls a pair of USBs also – more on that here). We can use this information in a number of ways. Firstly, let’s see if we can control the lamp via MQTT from node-red.
We will use a simple inject node and the second of the MQTT nodes: MQTT publish (out).
The inject node basically kick starts the flow to send a message along the chain. By default it sends a simple text string, but in this instance I want to send a payload of On to the MQTT node. You can see below how I’ve configured the inject node.
I double click the MQTT out node and configure that so it’s using the same MQTT broker as the MQTT in node. I also need to change the MQTT topic. As per tasmota documentation stat/+/+ reports the state, but cmnd/+/+ is the topic you need to use to actually command (control) something. We already know that the topic should contain bedroom/POWER1 so i configure it as below:
Deploy the flow and hit the injet button. You should now have turned on your plug. I can verify this in the debug window as stat/bedroom/POWER1 shows me it’s On. I can also see this in my home-assistant UI. Let’s add a second inject node to the flow, only this time msg.payload = Off. You should now be able to toggle your MQTT enabled device on and off at the press of a button.
Summary
So from a home automation point of view, you can monitor a plug’s status, and you can control remotely when a plug is turned on or off. This is a good start. Remember, no matter what you have on your network which is MQTT enabled, you just need to see it reporting in the debug window by subscribing to either its individual topic if you know it, or to everything and then by deduction working out what the correct topic is. Once you have the topic, you can also work out what payloads do you need to send or receive to have the desired effect. You can either make it do something in Home-Assistant and monitor the NR debug from there, or physically push the switch, or trip the sensor and monitor the resultant output in debug. Remember to check out part 2 where we deal with switching and conditional statements and part 3 where we cover timers and schedules.
As a side point, if you enjoyed this type of content and would be interested in sharing your own solutions, tips and tricks with like minded people perhaps you’d consider joining our facebook group. The aim of this group will hopefully be more show and tell rather than support, but that’s not to say we can’t lend a helping hand 🙂