Hi,
I updated to 2022.07 today and have noticed that one of my MQTT sensors has stopped working with the following errors:
2022-07-11 19:45:23 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{ value_json.cpu_load }}'
2022-07-11 19:45:23 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{ value_json.cpu_temp }}'
2022-07-11 19:45:23 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{ value_json.used_space }}'
...
The sensor
config is (i know i need to move to the new format before 2022.09):
- platform: mqtt
name: "OSMC CPU Load"
state_topic: "systems/osmc/system_data"
value_template: "{{ value_json.cpu_load }}"
unit_of_measurement: "%"
icon: mdi:chip
- platform: mqtt
name: "OSMC CPU Temperature 2"
state_topic: "systems/osmc/system_data"
value_template: "{{ value_json.cpu_temp }}"
unit_of_measurement: °C
device_class: temperature
- platform: mqtt
name: "OSMC Used Space"
state_topic: "systems/osmc/system_data"
value_template: "{{ value_json.used_space }}"
unit_of_measurement: "%"
icon: mdi:chart-pie
...
And the captured input message is:
Message 0 received on systems/osmc/system_data at 21:20:
{"used_space": 81, "sys_clock_speed": 1200, "cpu_load": 21.0, "memory": 48.5, "uptime_days": 10, "cpu_temp": 59.6, "swap": NaN, "voltage": 1.3062}
I appreciate that this is all on one line so perhaps not valid JSON but this was working prior to updating to 2022.07 and nothing has changed on the Pi sending the message. I have other Pis running the same script that work, the difference is that the JSON is multi line e.g.:
Message 1 received on systems/home-assistant/system_data at 21:23:
{
"used_space": 70,
"sys_clock_speed": 1500,
"cpu_load": 71.3,
"memory": 21.1,
"uptime_days": 37,
"cpu_temp": 65.7,
"swap": 75,
"voltage": 0.8438
}
Is this a known change in the way that value_json works?
Weirdly both systems are running the same Python version (2.7.16) for the same script, one has line breaks the other doesn’t so i am not quite sure how to fix yet.
The Python code is below if anyone does have any ideas:
values_json = {
"cpu_load":cpu_load,
"cpu_temp":float(cpu_temp),
"used_space":used_space,
"voltage":float(voltage),
"sys_clock_speed":int(sys_clock_speed),
"swap":swap,
"memory":memory,
"uptime_days":uptime_days,
}
values_string = json.dumps(values_json)
# connect to mqtt server
client = paho.Client()
client.username_pw_set(config.mqtt_user, config.mqtt_password)
client.connect(config.mqtt_host, int(config.mqtt_port))
# publish monitored values to MQTT
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/system_data", str(values_string), qos=1)