Need help with an automation for a wireless button press

I’m using Zigbee2MQTT, and just paired a wireless Xiaomi dual button. It automatically created a sensor. The state of the sensor changes to Left, right, left_double, right_double, etc, based on which button I press/double press. How can I make it so that a light toggles when a button is pressed. I don’t think the ‘state_changed’ event will work for the times where I press the left button, and then press it again a minute later, since I’m guessing that wouldn’t actually change the state.

Here’s what I have tried, but it’s not working:

- id: MasterButtonLeftClick
  alias: Master Button Left Click
  initial_state: true
  hide_entity: true
  trigger:
    - platform: event
      event_type: click
      event_data:
        entity_id: sensor.0x00158d0002f07928_click
        click_type: left
  action: 
    service: light.toggle
    entity_id: light.0xb0ce181400012763

Please read the blue banner at the top of the page.

1 Like

I had done that, but it did not work right either. I edited my comment. And it just left aligned everything. I used the BBCode and it is working now.

Here my config:

automation:
  - alias: sz_schalter_eingang
    hide_entity: true
    trigger:
    - platform: mqtt
      topic: 'zigbee2mqtt/sz_schalter_eingang'
#    - platform: mqtt
#      topic: zigbee2mqtt/0x00137b0002f07120 #old device name - I changed the friendly name
    condition:
      condition: or
      conditions:
        - condition: template
          value_template: "{{ 'left' == trigger.payload_json.click }}"
        - condition: template
          value_template: "{{ 'right' == trigger.payload_json.click }}"
        - condition: template
          value_template: "{{ 'both' == trigger.payload_json.click }}"
    action:
    - service: scene.turn_on
      data_template:
        entity_id: >-
          {%- set scene_map = {'left':'scene.sz_max',
                         'right':'scene.sz_lesen',
                              'both':'scene.sz_aus'} -%}
          {{ scene_map[trigger.payload_json.click] }}

or here the cube with a single thing to trigger and listen to:

  - alias: pc_cube
    hide_entity: true
    initial_state: 'on'
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/sz_cube_pc'
    condition:
      - condition: template
        value_template: "{{ 'rotate_right' == trigger.payload_json.action }}"
    action:
    - service: script.turn_on
      entity_id: script.dash_pc_hochfahren

It’s always the same afterwards - look at the payload and define the trigger & In condition comes the part when you realy want it to trigger. so for example
like 'zigbee2mqtt/sz_cube_pc', payload '{"battery":37,"voltage":2975,"linkquality":89,"action":"rotate_right","angle":23.12}'

For the sake of the argument it sould only trigger when I put in a supernew battery.
the condition would be then
value_template: "{{ '100' == trigger.payload_json.battery}}"
and change the automation to the things you want
same with right click, left click and so on

Ah. I didn’t even think about looking at MQTT. I was just looking at the sensor. I got something working using State_change on the sensor, but it didn’t work right, and then our lights came on randomly in the middle of the night last night for no reason, and I had to wake up to shut them off. So I’m going to try out what you said. Thanks for your help with this.

Just in case anyone else needs it, what @Underknowledge posted worked perfectly.