Help Needed with Automation using ZHA Aqara Zigbee Button and Chooser Action

I am trying to modify an existing automation so that my doorbell plays a different sound depending on whether the Aqara Zigbee button I am using is pressed once, twice, three times etc. The Zigbee events corresponding to each of these can be selected as a trigger in the UI, however if I add a Choose Action then these are not available, only the battery level appears as shown below:

My yaml for the existing automation is …

- id: Hall_001
  alias: Doorbell Automation
  trigger:
  - device_id: f12a042cd06945cf8de93be563ccfc12
    domain: zha
    platform: device
    subtype: remote_button_short_press
    type: remote_button_short_press
  - device_id: f12a042cd06945cf8de93be563ccfc12
    domain: zha
    platform: device
    subtype: remote_button_double_press
    type: remote_button_double_press
  - device_id: f12a042cd06945cf8de93be563ccfc12
    domain: zha
    platform: device
    subtype: remote_button_triple_press
    type: remote_button_triple_press
  action:
  - data:
      delay: 00:00:04
      mp3_file: http://192.168.1.17:8123/local/doorbell-2.mp3
      volume: 0.5
    service: script.sonos_broadcast_mp3
  - delay: 00:00:04
  initial_state: 'on'

I would like to replicate the above action so that a different mp3 file is played depending on the number of button clicks. I relaise that I can do this in 3 automations but I has hoped this could be done in a single automation using the new Choose Action.

I think this may be due to the fact that ZHA treats these button presses as Events whereas in Zigbee2MQTT a Sensor can be created that changes to a different state based on the number of clicks.

Cheers!

I have no ZigBee devices so probably shouldn’t even be commenting, but…
Does the doorbell button not show up as a separate binary_sensor under entities rather than devices ?

Hi @jivesinger, that’s the way it works in Zigbee2MQTT but in ZHA you only get the battery status as an entity for for these kind of buttons and the presses are detecable as events in automation triggers. This is how it looks in the Integration:

I thought there may have been some similarities between ZigBee and zwave.

I was wrong :rofl:

I finally have this figured out by using the HA Developer Tools to listen to “zha_events” in order to determine the syntax that I needed to for the choose condition template (for my Aqara remote button this is trigger.event.data.args.click_type).

image

The resulting automation that allows multiple button presses to be handled by a single automation is:

- id: '1597668281558'
  alias: Doorbell Automation 2
  description: ''
  trigger:
  - device_id: f12a042cd06945cf8de93be563ccfc12
    domain: zha
    platform: device
    subtype: remote_button_short_press
    type: remote_button_short_press
  - device_id: f12a042cd06945cf8de93be563ccfc12
    domain: zha
    platform: device
    subtype: remote_button_double_press
    type: remote_button_double_press
  condition: []
  action:
  - data:
      entity_id: switch.doorbell
    service: switch.turn_on
  - choose:
    - conditions:
      - condition: template
        value_template: '{{ trigger.event.data.args.click_type == ''single'' }}'
      sequence:
      - data:
          delay: 00:00:04
          mp3_file: http://192.168.1.17:8123/local/doorbell-2.mp3
          volume: 0.5
        service: script.sonos_broadcast_mp3
    - conditions:
      - condition: template
        value_template: '{{ trigger.event.data.args.click_type == ''double'' }}'
      sequence:
      - data:
          delay: 00:00:02
          mp3_file: http://192.168.1.17:8123/local/woop-woop.mp3
          volume: 0.5
        service: script.sonos_broadcast_mp3
    default: []
  mode: single
2 Likes

Can you do a ‘hold’ action I wonder?
When I listen for holding events on TRADFRI switches, I get a ‘move’ event with numerical values, and then a ‘stop’.

"cluster_id": 8,
   "command": "move",
   "args": [
            1,
            83

It does not look like the Mija remote button WXKG01LM (https://zigbee.blakadder.com/Xiaomi_WXKG01LM.html) supports a hold action as when a press & hold this gets interpreted as a single press. Looking at the available options for the device as a trigger in an automation it appears that there are 5 click options as shown below:

image

Thanks for this solution! It was a bit harder with a double switch. But with some extra tinkering I made it work!

Until recently I was using the DeConz integration with these XiaoMi Aqara remotes. With that I could use more trigger modes that the 5 mentioned by Jonah1970. A reduced list including these 5 (all identified by an event number) is:
“Turn on” button pressed 1000 remote_button_short_press
“Turn on” button released 1002 remote_button_short_release
“Turn on” button double clicked 1004 remote_button_double_press
“Turn on” button triple clicked 1005 remote_button_triple_press
“Turn on” button quadruple clicked 1006 remote_button_quadruple_press
“Turn on” button quintuple clicked 1010 remote_button_quintuple_press
“Turn on” button continuously pressed 1001 remote_button_long_press
“Turn on” button released after long press 1003 remote_button_long_release
It seems the remotes allow more options than ZHA has implemented.
With DeConz the trigger events are passed on to the HA in a different form: for my automations I could choose between the triggers using e.g. trigger.event.data.event = 1003, which worked well.

I inspected the trigger events in /Developer Tools/Events and found that the format of DeConz events is very different from the (supposedly raw) events in ZHA as shown in Jonah1970’s post. The DeConz integration apparently does some pre-processing on the raw triggers; ZHA apparently works with the raw event data.

I have changed to the ZHA integration because I prefer the direct communication between the Zigbee devices and HA. In the process of rewriting the automations with Zigbee devices I encountered the same problems as the others in this post. I also ended up with the solution of Jonah1970 which appears to work as a good alternative to my earlier DeConz automations.

It is a pity that the ZHA triggers are based on the device_id instead of the names given. These id’s are meaningless and require some bookkeeping tables to connect them with these names. But this would probably require some pre-processing.

I am trying to use the Aqara button with ZHA as well to toggle a switch that is across the room.

With my code however, when I try and save the automation it returns “Message malformed:”

Any thoughts to what I’m doing wrong?

alias: Guest Apartment Virtual Switch
description: ''
trigger:
  - device_id: 3dc323b4c56ba20a3aef9c6b7b32ca1b
    domain: zha
    platform: device
    subtype: remote_button_short_press
    type: remote_button_short_press
condition: []
action:
  - type: toggle
    device_id: 4e2fe640a0fe567fb75e12140beff85a
    entity_id: light.lumi_lumi_switch_b1laus01_on_off
    domain: light
mode: single

I’m not really sure but I believe that these buttons work by triggering events so you could try something like this as the trigger:

  trigger:
  - event_data:
      device_ieee: 00:15:8d:00:03:e7:ff:40
    event_type: zha_event
    platform: event

However make sure you use your Zigbee IEEE address which can be found on the Integrations page:

A trigger makes a lot of sense, HA now considers the automation valid, but it’s not performing the action. Is there something else I should include in the trigger?

alias: Guest Apartment Virtual Switch
description: ''
trigger:
  - event_data:
      device_ieee: 00:15:8d:00:02:8f:44:0e
    event_type: zha_event
    platform: event
condition: []
action:
  - type: toggle
    device_id: 4e2fe640a0fe567fb75e12140beff85a
    entity_id: light.lumi_lumi_switch_b1laus01_on_off
    domain: light
mode: single
lumi.remote.b1acn01
by unk_manufacturer
Zigbee info
IEEE: 00:15:8d:00:02:8f:44:0e
Nwk: 0x4ec2
Device Type: EndDevice
LQI: 132
RSSI: -67
Last Seen: 2021-08-22T18:03:34
Power Source: Battery or Unknown

Hi, you could go into Developer Tools, type “zha_event”, then click “Start Listening”. You can then observe the log that is produced to see what (if any) events are being captured for your device as in the example below:

I don’t recognise the sensor name “unk_manufacturer” (for mine I see “Lumi” here) so you might want to check the link below to see if you can find it and confirm that it is supported:

The other thing you could do is to go to your list of automations and click “RUN ACTIONS” for the relevant automation. If your light toggles then that proves this part of the automation is working as expected.

Hope that helps a bit!

Thanks everyone. Just putting that (and a little more) altogether for the big square Aqara Smart Light Control Wall Wireless Switch (single or double):

  • Pair the switch in ZHA by holding it down for ten seconds to enter pairing mode, then repeatedly, quickly pressing the button until ZHA sees the device.
  • Open Developer Tools / Events and under “Listen to event” enter zha_event as “Event to subscribe to” and click “Start listening”
  • Press the button!
  • Create an automation with trigger platform: event, event_type: zha_event and event_data contains device_ieee, unique_id and device_id that were shown in the logs once you pressed the button

Thus giving a complete automation like:

- id: '1635869385618'
  alias: Toggle Bedroom light
  description: ''
  trigger:
  - event_data:
      device_ieee: 00:15:8d:00:01:57:61:42
      unique_id: 00:15:8d:00:01:57:61:42:1:0x0006
      device_id: 2731333dd03ee19144f52d7e2672e509
    event_type: zha_event
    platform: event
  condition: []
  action:
  - type: toggle
    device_id: 90d916d096fce1cdb616abce95766de6
    entity_id: light.bedroom_level_on_off
    domain: light
  mode: single

1 Like

in my case the automation always performs the single click option even on double and triple press.
Does anyone know why?
when listening to zha_events “double” and “triple” are sent correctly from the switch to HA, so it seems to be an issue with the automation itself.

alias: Aqara Smart Switch 1
description: ""
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_ieee: 00:15:8d:00:04:02:59:ea
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.event.data.args.click_type == single }}"
        sequence:
          - service: light.turn_on
            data:
              rgb_color:
                - 249
                - 240
                - 107
              brightness: 129
            target:
              entity_id: light.arbeitszimmer
      - conditions:
          - condition: template
            value_template: "{{ trigger.event.data.args.click_type == double }}"
        sequence:
          - service: light.turn_on
            data:
              rgb_color:
                - 204
                - 0
                - 0
              brightness: 141
            target:
              entity_id: light.arbeitszimmer
      - conditions:
          - condition: template
            value_template: |
              {{ trigger.event.data.args.click_type == triple }}
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.arbeitszimmer
mode: restart

i.e.:

event_type: zha_event
data:
  device_ieee: 00:15:8d:00:04:02:59:ea
  unique_id: 00:15:8d:00:04:02:59:ea:1:0x0006
  device_id: d00b7f46bc05b0726a52df67dfb4fc9e
  endpoint_id: 1
  cluster_id: 6
  command: click
  args:
    click_type: double
  params: {}
origin: LOCAL
time_fired: "2023-06-06T11:12:39.345978+00:00"
context:
  id: 01H286HBDHZP2R926F8GC1NDFF
  parent_id: null
  user_id: null