CPU temp via telegraf-mqtt: templating MQTT sensors

I posted this to r/homeassistant but haven’t had any ideas there so I hope its OK to repost here for a different audience perhaps?

I think I need help understanding templating/jinja2 and json… here goes, hope its clear enough:

I am using lm-sensors and telegraf on my server to send its CPU temps to my HA mqtt broker.

It’s ‘working’, however telegraf sends payloads for each of the CPU cores (4) and the package (1) = 5 total payloads every time it updates.

I have successfully added an MQTT sensor to my HA config, which sort of works but the 5 instant payloads plays silly changing the state of the sensor 5 times in an instant. Their temps are also usually a few degrees apart so its not that helpful for graphing and i wonder if it’ll mess up automations.

This is the server sensors output:

username@XXXX:~# sensors
coretemp-isa-0000
Adapter: ISA adapter
Package id 0:  +41.0°C  (high = +80.0°C, crit = +100.0°C)
Core 0:        +38.0°C  (high = +80.0°C, crit = +100.0°C)
Core 1:        +38.0°C  (high = +80.0°C, crit = +100.0°C)
Core 2:        +41.0°C  (high = +80.0°C, crit = +100.0°C)
Core 3:        +39.0°C  (high = +80.0°C, crit = +100.0°C)

This is a telegraf mqtt payload, for core_3:

{"fields":{
    "temp_crit":100,
    "temp_crit_alarm":0,
    "temp_input":39,
    "temp_max":80
    },
  "name": "sensors",
  "tags":{
    "chip":"coretemp-isa-0000",
    "feature":"core_3",
    "host":"XXXX"
    },
  "timestamp":1612384950
}

This is an mqtt sensor:

- platform: mqtt
  name: XXXX CPU Temperature
  state_topic: "my/telegraf/topic"
  value_template: "{{ value_json.fields.temp_input }}"
  device_class: temperature
  unit_of_measurement: '°C'

I would like to only use the temp_input value which has "feature":"package_id_0" in the tags line. I have then tried this value_template:

- platform: mqtt
  name: XXXX CPU Temperature
  state_topic: "my/telegraf/topic"
  value_template: >
        {% if  value_json.tags.feature == 'package_id_0' %}
        {{value_json.fields.temp_input}}
        {% endif %}
  device_class: temperature
  unit_of_measurement: '°C'

This again, ‘works’ but it reports the four cores as 0°C … which makes the graphs even worse and I think will make automations much harder to set up… Perhaps I’m going about it the wrong way altogether? Is there anything in templating where it uses the last non-zero value or something like that in an {% else %} argument?

Thanks so much for any ideas, I’m a bit lost!

Here’s a graph of the output of the second mqtt sensor above, for what its worth (its really hard to see the red dots of the package temp in amongst the 0 readings):
Screenshot 2021-04-27 at 22.17.30

1 Like

Thanks to @123 on another thread, I solved this by adding an {% else %} to use the previous state if the mqtt payload doesn’t contain any values for package_id_0. It seems so simple now I know, I wasnt aware of the states('sensor.XXXX_cpu_temperature') despite spending far too long trying to understand the states templating!!

This is the sensor:

- platform: mqtt
  name: XXXX CPU Temperature
  state_topic: "my/telegraf/topic"
  value_template: >
        {% if  value_json.tags.feature == 'package_id_0' %}
        {{value_json.fields.temp_input}}
        {% else %}
        {{ states('sensor.XXXX_cpu_temperature') }}
        {% endif %}
  device_class: temperature
  unit_of_measurement: '°C'
1 Like