MQTT Sensor - filter specific JSON value

I’m new to templating and have been playing around with setting up a MQTT sensor to store when zigbee2mqtt is updating a device and show the messages output in lovelace.

This is a mqtt message example:

topic 'zigbee2mqtt/bridge/log', payload '{"type":"ota_update","message":"Update of 'Remote Kitchen' at 0.36%, +- 196 minutes remaining","meta":{"status":"update_progress","device":"Remote Kitchen","progress":0.36}}'

This is what I have for a sensor:

  - platform: mqtt
    name: Zigbee Bridge Update Log
    state_topic: "zigbee2mqtt/bridge/log"
    icon: mdi:message-alert-outline
    value_template: "{{ value_json.type }}: {{ value_json.message }}"

This just displays all messages from the topic with “type: message” I want to be able to filter out only “type”:“ota_update” but not sure how to do that with the sensor?

I need to do this to display only ota update related log messages and also I’m getting errors in the HA log due to a really long MQTT message on the same topic showing all devices and their status:

Exception in message_received when handling msg on 'zigbee2mqtt/bridge/log': '{"type":"devices","message":[LONG_MESSAGE]}' Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 35, in wrapper msg_callback(msg) File "/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py", line 165, in message_received self.async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 290, in async_write_ha_state self._async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 403, in _async_write_ha_state self.entity_id, state, attr, self.force_update, self._context File "/usr/src/homeassistant/homeassistant/core.py", line 1007, in async_set state = State(entity_id, new_state, attributes, last_changed, None, context) File "/usr/src/homeassistant/homeassistant/core.py", line 755, in __init__ f"Invalid state encountered for entity id: {entity_id}. " homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.zigbee_bridge_update_log. State max length is 255 characters.

Use Dev Tools/Templates to play with it.


Please describe in more detail how it should look.

Solve the second problem first. A sensor’s state cannot exceed 255 characters but its attributes can, so you’ll have to unpack the message into a sensor attribute and set the state to the type. Something like this (untested, see the docs, particularly the bit about synchronous updates which looks like it might be a problem):

  - platform: mqtt
    name: Zigbee Bridge Update Log
    state_topic: "zigbee2mqtt/bridge/log"
    icon: mdi:message-alert-outline
    value_template: "{{ value_json.type }}"
    json_attributes_topic: "zigbee2mqtt/bridge/log"
    json_attributes_template: "{{ value_json.message }}"

than solve the first problem with a template sensor that only updates if the type is ota_update (again, untested):

- platform: template
  name: Zigbee OTA update message
  entity_id: sensor.zigbee_bridge_update_log
  value_template: >
    {% if states('sensor.zigbee_bridge_update_log') == 'ota_update') %}
      {{ state_attr('sensor.zigbee_bridge_update_log', 'message') }}
    {% else %}
      {{ states('sensor.zigbee_ota_update_message') }}
    {% endif %}

This will break if the ota_update message exceeds 255 characters: you’ll then need to set it as an attribute of the second sensor.

1 Like