[SOLVED] Finding difficoult to "retain last known value" for a Sonoff SNZB-02 Sensor reporting via MQTT trough a SonOff ZBBridge on Tasmota 9.5.0.3

Newbie here, and I’m at a bit of a loss with a Sonoff ZigBee Bridge operating Tasmota 9.5.0.3 and a Sonoff SNZB-02 Sensor reporting, trough the bridge, via MQTT.

The sensor is correctly paired with the Bridge (acting as Coordinator) and the data is sent via MQTT in the form of:


 MQT: tele/tasmota_zb/SENSOR = {"ZbReceived":{"0xB585":{"Device":"0xB585","Temperature":30.65,"Humidity":79.69,"Endpoint":1,"LinkQuality":118}}}

On Home Assistant (running in ESXI thanks to the readly available image) the Mosquitto component is correctly receiving data over MQTT from the Bridge.

In Configuration I have the following:

 - platform: mqtt
    name: "ZB_Est_Temperature"
    qos: 1
    state_topic: "tele/tasmota_zb/SENSOR"
    value_template: "{{ value_json['ZbReceived']['0xB585']['Temperature'] }}"
    unit_of_measurement: "°C"
    availability_topic: "tele/tasmota_zb/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"
    device_class: temperature
  - platform: mqtt
    name: "ZB_Est_Humidity"
    qos: 1
    state_topic: "tele/tasmota_zb/SENSOR"
    value_template: "{{ value_json['ZbReceived']['0xB585']['Humidity'] }}"
    unit_of_measurement: "%"
    availability_topic: "tele/tasmota_zb/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"
    device_class: humidity

I can read the sensor latest data on my Lovevalce Card, but only when the Sensor is sending updated data. If, for example, temperature isn’t changed from last measurement and the MQTT message is just:


MQT: tele/tasmota_zb/SENSOR = {"ZbReceived":{"0xB585":{"Device":"0xB585","Humidity":64.36,"Endpoint":1,"LinkQuality":113}}}

Then I get the " 0°C " value instead of the last know value. I’m pretty sure the sulution is very simple and I’m dumb enough to have missed it, but after reading all the docs I could find I’m still missing it.

One of things I tried, without success, is using a if/then statement to try and filter out the value like this:

    value_template: > 
     {% if "Temperature" in "value_json['ZbReceived']['0xB585']" %} 
        {{ "value_json['ZbReceived']['0xB585']['Temperature']" }}
     {% else %}
       {{states('sensor.ZB_Est_Temperature') }}
    {% endif %}

I’ve also tried to use this statement, derived from another post here on the forum, but doing so I couldn’t show the temperature value while beeing able to see the latest value in the time chart; this convinced me even more that I’m just “one stupid mistake away” fom learning how to do it.

   value_template: > 
     {% if "Temperature" in value_json["0xB585"] %} 
      {{ value_json["0xB585"]["Temperature"] }}
     {% else %}
      {{states('sensor.Temperature') }}
     {% endif %}

I’m hoping someone could be kind enough to point me in the right direction to solve this. :pray:

In the end I managed to solve the issue.

Turns out Tasmota (ZigBee-to-Tasmota) has way more “customization options” than I tought: I was able to customize the values (re)published from the SonOff ZigBee Bridge in order to have two different MQTT Topics with “retain” function.

image

15:57:06.281 MQT: tele/tasmota_zb/B585/SENSOR = {"ZbReceived":{"0xB585":{"Device":"0xB585","Temperature":25.41,"Humidity":75.88,"Endpoint":1,"LinkQuality":149}}}
15:57:06.289 RUL: ZBRECEIVED#0XB585#HUMIDITY performs "publish2 home/zigbee/esterno/humidity 75.88"
15:57:06.292 MQT: home/zigbee/esterno/humidity = 75.88 (retained)
15:57:06.299 RUL: ZBRECEIVED#0XB585#TEMPERATURE performs "publish2 home/zigbee/esterno/temperature 25.41"
15:57:06.303 MQT: home/zigbee/esterno/temperature = 25.41 (retained)

Thus meaning the Configuration in Home Assistant is now as simple as that:

  - platform: mqtt
    name: "ZigBee_Esterno_Humidity"
    qos: 1
    state_topic: "home/zigbee/esterno/humidity"
    unit_of_measurement: "%"
    device_class: humidity
  - platform: mqtt
    name: "ZigBee_Esterno_Temperature"
    qos: 1
    state_topic: "home/zigbee/esterno/temperature"
    unit_of_measurement: "°C"
    device_class: temperature

SonOff SNZB-02 is now working as desired, plus reading the Tasmota Documentantion I was able to further customize the MQTT output so that one ZBBridge can support more sensors each with a dedicated MQTT Topic. No more “Unavailable” status for me. :slight_smile:

I hope this might help others, since I’ve found a lot of unresolved posts around the web dealing with the same situation.

2 Likes

you could also use:

  - platform: mqtt
    name: "DG-Bad Hum"
    state_topic: "tele/tasmota_9F7C52/E83F/SENSOR"
    value_template: > 
      {% if "Humidity" in value_json.ZbReceived["0xE83F"] %} 
          {{ value_json.ZbReceived["0xE83F"]["Humidity"] }}
       {% else %}
         {{states('sensor.dg_bad_hum') }}
       {% endif %}
    device_class: humidity
    unit_of_measurement: '%'
    qos: 1