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!