Trigger template sensor to copy Z2M device entity state to attribute via mqtt

I have a Zigbee device (Sonoff TRVZB) which in Z2M has an entity (“weekly_schedule”) that returns a state (json) of more than 255 characters. I can edit this state via Z2M but the entity in HomeAssistant is always cut off after 255 characters, resulting in invalid json so it’s unusable.

My idea was to create a template binary sensor that listens to the MQTT topic for this state and populates a “weekly_schedule” attribute of the binary sensor with the content of the topic, since the attribute size is not limited. Will this approach work? If so, any pointers on the code? I’ve tried something along the lines of:

- trigger:
  - platform: mqtt
    topic: "homeassistant/zigbee2mqtt/TRV master bedroom"
  binary_sensor:
  - name: "TRV Master Bedroom Schedule"
    unique_id: "bs_TRV_Master_Bedroom_Schedule"
    state: "{{ is_state('climate.trv_master_bedroom','auto') }}"
    attributes:
      full_payload: "{{ value }}"

But while I can see the sensor its state doesn’t update when I switch the underlying climate entity to ‘auto’ and there’s no attributes.

Because that is not the trigger. Triggered template sensors only update when the trigger occurs, not when an entity in the template changes (as for template sensors without triggers).

Also there is no {{ value }} variable for that template. You could use {{ trigger.payload }} but you would be better off just using an MQTT sensor instead. It supports attributes.

The only way to do this is to use an MQTT sensor’s attributes and completely avoid using template entities.

mqtt:
  sensor:
    - name: TRV Master Bedroom Schedule
      state_topic: homeassistant/zigbee2mqtt/TRV master bedroom
      device_class: timestamp
      state: "{{ now() }}"
      json_attributes_topic: homeassistant/zigbee2mqtt/TRV master bedroom
      json_attributes_template: "{{ {'full_payload': value} }}"

This will create a sensor that has the last time it was updated as the main state. And it will have an attribute named full_payload with the topic information.

Thank you, these replies set me off in the right direction.

My mqtt sensor code is:

mqtt:
  sensor:
    - name: TRV Master Bedroom Schedule
      unique_id: trv_mb_sched_attr
      state_topic: zigbee2mqtt/TRV master bedroom/availability
      json_attributes_topic: zigbee2mqtt/TRV master bedroom
      json_attributes_template: '{{ value_json|to_json }}'

A couple of things to note:

  1. Manual mqtt sensors don’t like device_class or state. Also, since the state was getting set by the content of state_topic, and this was longer than 255 characters, it was throwing a warning in the logs. I resolved this by changing the state_topic to the device’s availability topic, which returns {“state”: “:online”}.
  2. I had to fiddle with the correct formatting for the json_attributes_template as I kept getting “Erroneous JSON” errors. The result works fine. I am contemplating changing it to {{ value_json.weekly_schedule|to_json }} to reduce the amount of data in the sensor attributes.

state_class works, state should be value or value_template

it does, you just need a proper state that matches. You could have used what I wrote above for the value_template and the device_class and it would have worked.

and this wouldn’t have happened if you used what I wrote above as well.

The result just needs to be JSON. So you could have just used {{ value }} if you intended to just output the topic contents as the attributes.