In the first part of this tutorial, we set up Telegraf and MQTT to transmit our computer temperature data from our host machine. If you have not yet read that tutorial, you can check it out here!
In this second part, we are going to integrate the MQTT data into Home Assistant. I’m going to use this as an opportunity to discuss some features of Node-RED that I find very useful.
Installing “hass-node-red”
If you’ve install Node-RED using supervisor Add-ons page, then you should be familiar with Home Assistant palette in Node-RED:
However, you might notice that my Home Assistant palette has some extra nodes, specifically webhook and entity. These nodes were added through the addition of "hass-node-red"
. It can be installed in the HACS (Home Assistant Community Store), under the name Node-RED:
I’d recommend installing "hass-node-red"
through HACS. However, the Github Repo can be found here if you wish to install manually.
This integration allows us to create and update sensors from Node-RED. While it is possible to create MQTT sensors directly in Home Assistant, I’ve experimented with this integration and appreciate how easy it is to quickly create sensors.
After installing hass-node-red
you will need to restart your Home Assistant server.
Node-RED Setup
Next, we need to set up MQTT in Node-RED. If you have not used the MQTT node in Node-RED, I’ve discussed it in the “Node-RED Automation Setup” section in my tutorial on Home Assistant Presence Detection.
Add a MQTT node and connect it to a Debug node. If you followed my previous tutorial, then you will want to change these settings on your MQTT node:
- Subscribe to the Topic:
telegraf/#
. In the previous tutorial, the MQTT topic in Telegraf was set to ‘telegraf’. As such, Telegraf will send our sensor data in the format:telegraf/[IP address]/[sensor type]
. For us, based on the previous tutorial, the[sensor type]
will either be:sensors
orhddtemp
. By using the hash symbol (#), we are instructing the MQTT node to listen to all topics that begin withtelegraf
. (More info can be found on the HiveMQ page) - Since, in the previous tutorial, we set the Telegraf output data to be in JSON format, we can change our MQTT input node to automatically output, ‘a parsed JSON object’.
Next, on the Debug node, change the Output to: ‘complete msg object’. Then, deploy and open up the debug message panel. We will then wait for our messages to appear from Telegraf:
Set Debug node Output to a ‘complete msg object’
Telegraf MQTT data arriving in Node-RED
Filtering the MQTT Data
When the Telegraf MQTT data comes in, each sensor is sent as an individual payload. I am going to use the HDD temperature sensor data as an example, however it should be noted that this same procedure can be used for all sensor types. Expanding the hddtemp
MQTT data reveals an output that looks like this:
Here we can see all components of the payload. We need to 1) Filter this data for each HDD we have, and 2) Only pull the temperature data from the temperature:
field. We will use a switch
node to filter our drives based on the model name. As shown in the above image, the button >_
allows us to copy the path to the model:
field. Copy the path, and then add a switch node:
In the switch node’s Property
field, we will paste the path output we just copied. In this case, the path is: payload.tags.model
. Then, we need to filter based on our drives name. As shown, I have two drives, one named: WDC WD10JPVT-00A1YT0
and another named KINGSTON SA400S37240G
. The button beside the copy path >_
button, lets copy the values. Add your drive names here. We will then have a switch node that will have two outputs, each filtering the MQTT messages depending on the drive model name:
Creating the Entities
Finally, we will create our Entities using the entity
node from the Hass-node-red integration we installed earlier. Continuing from the switch node in the previous section, we will connect an entity node to each output. We want these entity
nodes to only receive the temperature data. Looking back at our MQTT data from the debug node, we can see that the temperature data is located at payload.fields.temperature
. We can use the same copy path >_
button as before to copy this path.
Then, in our entity node, the State
will be set by this path we just copied:
As for the other settings, here we can set our sensors name, and unit of measurement. These are optional, however, are handy to set here.
Finally, deploy our new nodes and wait for Telegraf data to come in. If we look in the Developer States tab in Home Assistant, and search for the name set in our entity
node, we should see the newly created sensor, with the correct temperature data:
Full Node-RED Code
We now have Node-RED listening for MQTT data from Telegraf, which converts it into a JSON object, filters/sorts it depending on the payload type, and outputs the temperature data into entity
nodes which creates sensors in our Home Assistant Server. While the HDD temps were shown, this can also be done for the CPU core temperatures. I’ve included the full code for Node-RED below:
Hopefully this is good starting point to get you started! Feel free to comment below or message me in Discord if you want to share your implementation or need any further help!
Good info. Lucky me I discovered your blog by accident (stumbleupon).
I have bookmarked it for later!
Feel free to visit my web site; delta 8 CBD