Part 2: Integrate Telegraf with Home Assistant Using Node-RED and MQTT

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.

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.

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:

  1. 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 or hddtemp. By using the hash symbol (#), we are instructing the MQTT node to listen to all topics that begin with telegraf. (More info can be found on the HiveMQ page)
  2. 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

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:

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:

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:

[{“id”:”343a25de.4b4d8a”,”type”:”mqtt in”,”z”:”57218d90.254cb4″,”name”:””,”topic”:”telegraf/#”,”qos”:”2″,”datatype”:”json”,”broker”:””,”x”:180,”y”:4360,”wires”:[[“5fc159ea.dcd33″,”40180a72.804514”]]},{“id”:”5fc159ea.dcd33″,”type”:”switch”,”z”:”57218d90.254cb4″,”name”:””,”property”:”payload.tags.chip”,”propertyType”:”msg”,”rules”:[{“t”:”eq”,”v”:”coretemp-isa-0000″,”vt”:”str”}],”checkall”:”true”,”repair”:false,”outputs”:1,”x”:330,”y”:4300,”wires”:[[“eaa42cad.291c3”]]},{“id”:”40180a72.804514″,”type”:”switch”,”z”:”57218d90.254cb4″,”name”:””,”property”:”payload.tags.model”,”propertyType”:”msg”,”rules”:[{“t”:”eq”,”v”:”KINGSTON SA400S37240G”,”vt”:”str”},{“t”:”eq”,”v”:”WDC WD10JPVT-00A1YT0″,”vt”:”str”}],”checkall”:”true”,”repair”:false,”outputs”:2,”x”:330,”y”:4420,”wires”:[[“53752733.497d5”],[“de3a0bb6.01f94”]]},{“id”:”eaa42cad.291c3″,”type”:”ha-entity”,”z”:”57218d90.254cb4″,”name”:”CPU Temp”,”server”:”e7f76522.c1a328″,”version”:1,”debugenabled”:false,”outputs”:1,”entityType”:”sensor”,”config”:[{“property”:”name”,”value”:”CPU Temp”},{“property”:”device_class”,”value”:””},{“property”:”icon”,”value”:””},{“property”:”unit_of_measurement”,”value”:”°C”}],”state”:”payload.fields.temp_input”,”stateType”:”msg”,”attributes”:[],”resend”:true,”outputLocation”:””,”outputLocationType”:”none”,”inputOverride”:”allow”,”x”:470,”y”:4300,”wires”:[[]]},{“id”:”53752733.497d5″,”type”:”ha-entity”,”z”:”57218d90.254cb4″,”name”:”Kingston Temp”,”server”:”e7f76522.c1a328″,”version”:1,”debugenabled”:false,”outputs”:1,”entityType”:”sensor”,”config”:[{“property”:”name”,”value”:”Kingston HDD Temp”},{“property”:”device_class”,”value”:””},{“property”:”icon”,”value”:””},{“property”:”unit_of_measurement”,”value”:”°C”}],”state”:”payload.fields.temperature”,”stateType”:”msg”,”attributes”:[],”resend”:true,”outputLocation”:””,”outputLocationType”:”none”,”inputOverride”:”allow”,”x”:500,”y”:4380,”wires”:[[]]},{“id”:”de3a0bb6.01f94″,”type”:”ha-entity”,”z”:”57218d90.254cb4″,”name”:”WD Temp”,”server”:”e7f76522.c1a328″,”version”:1,”debugenabled”:false,”outputs”:1,”entityType”:”sensor”,”config”:[{“property”:”name”,”value”:”WD HDD Temp”},{“property”:”device_class”,”value”:””},{“property”:”icon”,”value”:””},{“property”:”unit_of_measurement”,”value”:”°C”}],”state”:”payload.fields.temperature”,”stateType”:”msg”,”attributes”:[],”resend”:true,”outputLocation”:””,”outputLocationType”:”none”,”inputOverride”:”allow”,”x”:480,”y”:4440,”wires”:[[]]}]

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!

2 thoughts on “Part 2: Integrate Telegraf with Home Assistant Using Node-RED and MQTT”

Leave a Comment