Using "Choose" option in automation for zigbee2mqtt

Hi,
I tried to use the “Choose” option in the Automation to control xiaomi zigbee remote control that have 6 buttons , every button can have 4 different presses (single, double, triple,hold), so i tried to make an automation that listing to the mqtt topic of the zigbee2mqtt and start the Automation when the topic changed , there is a unique topic that called “action”.
The automation was triggered when i pressed one of the buttons, but the “choose” didn’t worked because the value of the “action” topic was cleaned until he reach the “choose” part.
Is there anything i can do to make it work, instead of making 24 (4x6= 24) automations?
I hope that i was clear in my question

Thanks

Please show the non-working code you have.

This is the log from this topic, you can see that the action value is changing after less than 1 second

Please read the community guidelines and post your code formatted properly, no screenshots.

1 Like

As @Burningstone said, it’s MUCH easier to help when you post code in a more readable fashion.

That being said, having experience with these devices, I know what the issue is. The way zigbee2mqtt works with these devices is that it sets the state to “button_1_single” and then IMMEDIATELY sets it to an empty string. In the split second it takes to get from the trigger of your automation to the condition in your choose, the state is no longer what you expect it to be.

The work-around is to create a separate automation with the state you expect listed in the trigger instead of using choose. Alternately, you can use the trigger values in the choose conditions, instead of the entity_id, but I don’t know the syntax for that off-hand because I don’t use it. Choose actions are difficult to read so I avoid them whenever possible.

Zigbee2MQTT also has a debounce option which can help here

it doesn’t help in this problem , but thanks

sorry about the pictures , I didn’t have any access to my private computer .
I has edit the code with mqtt trigger but it still doesn’t work , only without the choose it can work.
i think that it can be a bug of the choose option , because it need to transfer the data from the payload to the choose option ,it doesn’t need to read again the payload , i am going to open an issue on GitHub on this and see if there is an option for fixing it



alias: Zigbee 6 Buttons Remote (Duplicate)
description: ''
trigger:
  - platform: mqtt
    topic: zigbee2mqtt/6 Button Switch/action
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '''{{ "button_1_single" == trigger.payload}}'''
        sequence:
          - service: notify.mobile_app_s10_phone
            data:
              message: choose 1 Duplicate
              topic: choose 1 Duplicate
            entity_id: light.door_light
    default:
      - service: notify.mobile_app_s10_phone
        data:
          message: default Duplicate
          title: default Duplicate
mode: single

after a lot of trying , i manage to make it work, this is the code:

alias: Zigbee 6 Buttons Remote (Duplicate)
description: ''
trigger:
  - platform: mqtt
    topic: zigbee2mqtt/6 Button Switch/action
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ "button_1_single" == trigger.payload}}'
        sequence:
          - service: light.toggle
            data: {}
            entity_id: light.guest_room_light
    default:
      - service: notify.mobile_app_s10_phone
        data:
          message: default
          title: default
mode: single