How to Convert Unicode Value in MQTT JSON for a Custom Sensor

Hi, I am using a Tasmota zigbee coordinator and send values to HA over MQTT.

This is the full message that the coordinator is sending over:

{"ZbReceived":{"0x3999":{"Device":"0x3999","Temperature":19,"Humidity":63,"Endpoint":1,"LinkQuality":113}}}

I can see that message in the MQTT integration when I listen to the topic. However, I cannot correctly parse this message into custom Temperature and Humidity sensors. I have set up a separate mqtt.yaml referenced in config.yaml, and added the following sensor to the mqtt.yaml:

 sensor:
     name: "Office Temperature"
     state_topic: "tele/tasmota/3999/SENSOR"
     value_template: "{{ value_json.ZbReceived.0x3999.Temperature }}"

This did not work, unfortunately. So I checked the template output with Dev Tools:

{% set value_json = {"ZbReceived":{"0x3999":{"Device":"0x3999","Temperature":19,"Humidity":63,"Endpoint":1,"LinkQuality":113}}} %}
{{ value_json.ZbReceived.0x3999.Temperature}}

The error I got back is:
UndefinedError: dict object has no element 14745

So it seems the device ID “0x3999” is read by HA as unicode number 14745 which breaks the reference. How do I convert this into a string (?) within the value_json template? I cannot change the devide ID, it is not supported by Tasmota it seems.

Thanks!

Try bracket notation instead of dot notation.

sensor:
     name: "Office Temperature"
     state_topic: "tele/tasmota/3999/SENSOR"
     value_template: "{{ value_json['ZbReceived']['0x3999']['Temperature'] }}"

Thanks a lot!

1 Like