How do I publish sensor data to mqtt upon change?

I want to publish data from multiple identical Zigbee sensors to mqtt (which in turn is stored in influxdb) when they change value. I’d like to tweak the topic depending on the metric that changed (which reflects my influxdb architecture) [this works], and include the zone and zigbee address [doesnt work]. I have parts working but need some help to put this together :slight_smile:

I have this working for one sensor, an now I’d like to expand it to 3 others:

alias: Publish Aqara 5f543d to MQTT
trigger:
  - platform: state
    entity_id:
      - sensor.lumi_lumi_weather_temperature
      - sensor.lumi_lumi_weather_pressure
      - sensor.lumi_lumi_weather_humidity
condition: []
action:
  - service: mqtt.publish
    data:
      topic: |-
        {% if trigger.from_state.attributes.device_class == 'pressure' %}
          influx/environv3/quantity/P/source/aqara/board/aqara_5f543d/location/home/room/office2/value/state
        {% elif trigger.from_state.attributes.device_class == 'temperature' %}
          influx/temperaturev3/location/home/quantity/actual/room/office2/source/aqara/value/state
        {% elif trigger.from_state.attributes.device_class == 'humidity' %}
          influx/environv3/quantity/RH/source/aqara/board/aqara_5f543d/location/home/room/office2/value/state
        {% else %}
          invalid/environv3/quantity/other/source/aqara/board/aqara_5f543d/location/home/room/office2/value/state
        {% endif %}
      payload: "{{trigger.to_state.state}}"
mode: parallel

I’m stuck at getting the zone and zigbee IEEE in dynamically. Combining this and this topic I found this to work for 1 sensor:

{{ (device_attr("sensor.lumi_lumi_weather_temperature", "identifiers")| selectattr(0,'eq','zha') | map(attribute=1) | first)[-8:] }}

and I found

area_name({{trigger.to_state.object_id}})

should give the zone name, however I don’t know how to put this in an automation. I now have the following:

alias: Publish Aqara to MQTT - WIP
trigger:
  - platform: state
    entity_id:
      - sensor.lumi_lumi_weather_temperature
      - sensor.lumi_lumi_weather_pressure
      - sensor.lumi_lumi_weather_humidity
      - sensor.lumi_lumi_weather_temperature_2
      - sensor.lumi_lumi_weather_pressure_2
      - sensor.lumi_lumi_weather_humidity_2
      - sensor.lumi_lumi_weather_temperature_3
      - sensor.lumi_lumi_weather_pressure_3
      - sensor.lumi_lumi_weather_humidity_3
      - sensor.lumi_lumi_weather_temperature_4
      - sensor.lumi_lumi_weather_pressure_4
      - sensor.lumi_lumi_weather_humidity_4
condition: []
action:
  - service: mqtt.publish
    data:
      topic: >-
        hatest/device_class/{{trigger.from_state.attributes.device_class}}/name/{{trigger.to_state.attributes.friendly_name}}/room/{{ area_name({{trigger.to_state.object_id}}) }}/unique_id/{{(device_attr(trigger.to_state.object_id, "identifiers")}}
      payload: "{{trigger.to_state.state}}"
mode: parallel

which gives output

hatest/device_class/pressure/name/Bathroom Pressure/room/None/unique_id/None/ 1014

Any clues how to fix this? Also: how can I debug this more easily? The sensors only change every ~10 min which makes for a slow development cycle… I tried using Developer Tools → Template, but I can’t use things like trigger.to_state.object_id there. I also can’t trigger the automation because it lacks a trigger variable.

As backup I can hard code all of the options and manually update every 10min, but I’d prefer a dynamic solution + the learning that comes with it :slight_smile: Thanks!

OK I managed to fix it myself (with slow turn-around time), the following seems to work. I’d still be happy to hear how to more effectively debug/develop automations also when the sensor updates slowly (is there a replay option for sensor updates?).

alias: Publish Aqara to MQTT - WIP
description: Push 0 5f543d office2 / 2 1dd65a bedroom / 3 living 1c41c6 / 4 bathroom 1dd8f1
trigger:
  - platform: state
    entity_id:
      - sensor.lumi_lumi_weather_temperature
      - sensor.lumi_lumi_weather_pressure
      - sensor.lumi_lumi_weather_humidity
      - sensor.lumi_lumi_weather_temperature_2
      - sensor.lumi_lumi_weather_pressure_2
      - sensor.lumi_lumi_weather_humidity_2
      - sensor.lumi_lumi_weather_temperature_3
      - sensor.lumi_lumi_weather_pressure_3
      - sensor.lumi_lumi_weather_humidity_3
      - sensor.lumi_lumi_weather_temperature_4
      - sensor.lumi_lumi_weather_pressure_4
      - sensor.lumi_lumi_weather_humidity_4
condition: []
action:
  - service: mqtt.publish
    data:
      topic: |-
        {% if trigger.from_state.attributes.device_class == 'pressure' %}
          hatest/environv3/quantity/P/source/aqara/board/{{var_board}}/location/home/room/{{var_room}}/value/state
        {% elif trigger.from_state.attributes.device_class == 'temperature' %}
          hatest/temperaturev3/location/home/quantity/actual/room/{{var_room}}/source/aqara/board/{{var_board}}/value/state
        {% elif trigger.from_state.attributes.device_class == 'humidity' %}
          hatest/environv3/quantity/RH/source/aqara/board/{{var_board}}/location/home/room/{{var_room}}/value/state
        {% else %}
          invalid/environv3/quantity/other/source/aqara/board/{{var_board}}/location/home/room/{{var_room}}/value/state
        {% endif %}
      payload: "{{trigger.to_state.state}}"
variables:
  var_board: >-
    aqara_{{(device_attr(trigger.entity_id, "identifiers")|
    selectattr(0,'eq','zha')| map(attribute=1) | first)[-8:]| replace(":", "")
    }}
  var_room: "{{area_name(trigger.entity_id) | lower}}"
mode: parallel