MQTT button state to binary_sensor

Hi,

I’m trying to create a binary sensor which would replicate zigbee button status (hue button connected via zigbee2mqtt).The sensor shall stay in ON state as long, as the button as pressed and shall go to OFF when the button is released. I need that to identify x1/x2/x3… clicking patterns afterwards.

I was able to read payload from the trigger, but have difficulties to find the right syntaxis to do the comparison… nothing works there. I would appreciate a lot if someone could give me a hand here. Thanks!

template:
  - trigger:
      - platform: mqtt
        topic: zigbee2mqtt/Button/action
    binary_sensor:
      - name: "bin_Button"
        state: >
          {% if states('trigger.payload') == 'press' %}
            true
          {% elif states('trigger.payload' == 'release' %}
            false
          {% endif %}

MQTT LOG from single button push:

Info <small>2022-09-30 13:51:32</small>`MQTT publish: topic 'zigbee2mqtt/Button_3/action', payload 'press'`
Info <small>2022-09-30 13:51:32</small>`MQTT publish: topic 'zigbee2mqtt/Button_3/action', payload 'release'`
Info <small>2022-09-30 13:51:32</small>`MQTT publish: topic 'zigbee2mqtt/Button_3/action', payload 'off'`

Just guessing, did you try using only trigger in the template? Payload might just be added in the logs.
And your elif line is missing a ) at the end of states.
The documentation is referring to a capitalized True.

template:
  - trigger:
      - platform: mqtt
        topic: zigbee2mqtt/Button/action
    binary_sensor:
      - name: "bin_Button"
        state: >
          {% if states('trigger') == 'press' %}
            True
          {% elif states('trigger') == 'release' %}
            False
          {% endif %}

thanks for your quick guess, @eXtatic

Updated accordingly, no changes… I have added one more sensor for troubleshooting.
bin_Button_1 - works as expected (showing the button action status, including “press” and “release”)
bin_Button_2 - no changes, if/else statements are never executed.

template:
  - trigger:
      - platform: mqtt
        topic: zigbee2mqtt/Button_3/action
    sensor:
      - name: "bin_Button_1"
        state: "{{ trigger.payload }}"
        
  - trigger:
      - platform: mqtt
        topic: zigbee2mqtt/Button_3/action
    sensor:
      - name: "bin_Button_2"
        state: >
          {% if states('trigger') == 'press' %}
            True
          {% elif states('trigger') == 'release' %}
            False
          {% endif %}
        

I have managed to move forward with the following code:

template:
  - trigger:
      - platform: mqtt
        topic: zigbee2mqtt/Button/action
    binary_sensor:
      - name: "bin_Button"
        state: >
          {% if trigger.payload == 'press' %}
            True
          {% elif trigger.payload == 'release' %}
            False
          {% endif %}

But now there is a problem: the sensor’s state is cleared on ‘off’ event.
is it possible to create sensor’s trigger condition which would trigger only payloads “press” and “release” ? This should be easy to do, just can’t find how… any hints?