Get value from MQTT message

After flashing and installing a Sonoff Zigbee bridge with Tasmota it handles messages over MQTT from a paired Zigbee temperature/humidity sensor. The messages are shown in the Tasmota console, so far so good:

11:55:07 MQT: tele/ZBBridge/SENSOR = {"ZbReceived":{"0xEDC2":{"Device":"0xEDC2","Temperature":22.16,"Humidity":99.99,"Endpoint":1,"LinkQuality":18}}}

Now I’m trying to show the temperature within HA but I am struggling with MQTT. This is what I tried:

sensor Kantoor-Temperatuur:
    platform: mqtt
    name: "Kantoor Temperatuur"
    state_topic: "tele/ZBBridge/SENSOR/0xEDC2/Temperature"
    qos: 0
    unit_of_measurement: "ºC"

No error messages but also no temperature is shown when using entity sensor.kantoor_temperatuur. Can someone guide me by finding the right settings?

sensor:
  - platform: mqtt
    name: "Kantoor Temperatuur"
    state_topic: "tele/ZBBridge/SENSOR/"
    value_template: "{{ value_json.ZbReceived.0xEDC2.Temperature }}"
    qos: 0
    device_class: temperature

Thanks for helping Tom! I have changed configuration.yaml with your code:

sensor Kantoor-Temperatuur:
  - platform: mqtt
    name: "Kantoor Temperatuur"
    state_topic: "tele/ZBBridge/SENSOR/"
    value_template: "{{ value_json.ZbReceived.0xEDC2.Temperature }}"
    qos: 0
    device_class: temperature

Unfortunattely an error is shown after saving and tried to restart HASS:

Invalid config for [sensor.mqtt]: invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'xEDC2') for dictionary value @ data['value_template']. Got '{{ value_json.ZbReceived.0xEDC2.Temperature }}'. (See ?, line ?).

Any suggestions?

Oh, that’s a special problem when ids begin with numbers. Try this instead:

sensor Kantoor-Temperatuur:
  - platform: mqtt
    name: "Kantoor Temperatuur"
    state_topic: "tele/ZBBridge/SENSOR/"
    value_template: '{{ value_json["ZbReceived']["0xEDC2"]["Temperature"] }}'
    qos: 0
    device_class: temperature

Sorry Tom, another error is now shown:

Invalid config for [sensor.mqtt]: invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'xEDC2') for dictionary value @ data['value_template']. Got '{{ value_json.ZbReceived.0xEDC2.Temperature }}'. (See ?, line ?).

Argh. Look at the stupid mistake I made:

value_template: '{{ value_json["ZbReceived']["0xEDC2"]["Temperature"] }}'
                                          ^
                                        This should be a double quote. Sorry

Try:

value_template: '{{ value_json["ZbReceived"]["0xEDC2"]["Temperature"] }}'
1 Like

Thanks again for your time, very appreciated!

No error messages with your new code and the entity sensor.kantoor_temperatuur is available within HA. Unfortunattely no temperature value is shown :frowning:

(I have double-checked there were some MQTT messages)

Another try:

sensor Kantoor-Temperatuur:
  - platform: mqtt
    name: "Kantoor Temperatuur"
    state_topic: "tele/ZBBridge/SENSOR/"
    value_template: "{{ value_json.ZbReceived['0xEDC2'].Temperature }}"
    unit_of_measurement: "°C" 
    device_class: temperature

No error messages are generated but also no sensor value in HA.

Any help or suggestions still welcome!

  1. Go to Configuration > Integrations > MQTT > Configure > Listen to a topic
  2. Enter tele/ZBBridge/SENSOR in “Topic to subscribe to
  3. Click Start Listening and wait to receive a payload.

Screenshot from 2020-10-27 12-24-24
If you never receive a payload, it means Home Assistant isn’t subscribed to that topic. Either Home Assistant isn’t connected to the MQTT Broker or the Sonoff Zigbee Bridge isn’t connected to the MQTT Broker (or both).

Thank you Taras and Tom for your advice. Given the complexity, I decided to use my CC2531 Zigbee USB dongle. After adding the Zigbee integration this works fine. I don’t need MQTT for only measuring temperatures and humidity, so that can be done much easier.