ZHA Help, please. How to use a Zigbee button in an automation

I installed the ZHA integration and it immediately started finding all of my Zigbee devices. Four of those devices are Zigbee buttons. I know the button is seen because when I push the button, I see the button press in the logbook:

When I click on the “+” in Automations, I don’t get anything like a button state or event to use as a trigger:

zha2

So, how do I use the button in an automation?

The docs suggest using the changing timestamp as a state trigger:

I use the ZHA event like this.

platform: event
event_type: zha_event
event_data:
  device_ieee: 94:34:69:ff:fe:57:d5:58
  unique_id: 94:34:69:ff:fe:57:d5:58:1:0x0006
  device_id: e676acf4e9302472d8d7d81896f9ed6b
  endpoint_id: 1
  cluster_id: 6
  command: "on"

I copied all the event data that I found when I pressed the button and saw these items revealed using the Developer tools... Events... Listen to events...
I then subscribe to the zha_event
I hope that gets you in the right direction.

When I subscribe to “zha_event” then click on “listen”, I get thousands of lines of data with no way to capture it or to stop the scrolling. Have I missed something?

Interesting, this does not happen to me, I only get data when my devices send it with a button press. I am not sure if there is a way to filter this data. I also don’t know what devices would send data continuously. The only zha devices I have are lights, a router, and different buttons

Apparently I did something wrong last night. It is working now. (I may have accidentally subscribed to all?)

Anyway, I’ll give it a try now.

BTW- Do you have any insight to why one would prefer ZHA or Zigbee2MQTT?

I can only give you what I have experienced.
There are conversations that z2m supports more devices than zha. I don’t have many devices, and all apparently are supported by zha as I have not had issues pairing the devices I do have. zha is built into HA so I went with that as my decision.

There are a couple of blueprints floating around for ZHA:

https://community.home-assistant.io/t/zha-sonoff-snzb-01/317850

https://community.home-assistant.io/t/zha-sonoff-button-press-double-press-hold-actions/300080

They should just require updating the model to SNZB-01P to match your devices.

  • ZHA compat versus Z2M compat; Z2M wins (~2x) except for a few specific devices better supported by ZHA.
  • ZHA requires fewer steps to setup and is part of HA “out-of-the-box”.
  • Other “details” like running Z2M outside of HA (not as an addon) means the Zigbee network remains operational if you update/reboot HA.

With a quick search you should be able to find more information on the two.

Regarding your device with Z2M, you would be able to use something like this:

mode: queued
max: 10
trigger:
  - platform: state
    entity_id:
      - sensor.button_office_light_action
    to: single
    id: SINGLE
  - platform: state
    entity_id:
      - sensor.button_office_light_action
    to: double
    id: DOUBLE
  - platform: state
    entity_id:
      - sensor.button_office_light_action
    to: long
    id: LONG
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - SINGLE
        sequence: []
      - conditions:
          - condition: trigger
            id:
              - DOUBLE
        sequence: []
      - conditions:
          - condition: trigger
            id:
              - LONG
        sequence: []
1 Like

I’ve been using Z2M for years, but the past week has been a frustrating exercise with getting the Add-On to start. Since ZHA found all of my Zigbee devices in just a few minutes, I decided to stop troubleshooting Z2M and adopt ZHA.

But unlike your Z2M example, ZHA does not make sensor entities. In order to use a button or switch in an automation, for example, I am forced to capture the ZHA event. (Thanks pcwii)

Thanks. I got some of my Zigbee buttons and switches working as automation triggers. But there’s a couple that I would like to work on: My Ikea Tradfri Zigbee remotes.

I have the toggle working on this remote, but I want to use some of the other buttons. When I capture the event for the “Bright” button, I get this:

event_type: zha_event
data:
  device_ieee: 00:0b:57:ff:fe:9b:cb:46
  unique_id: 00:0b:57:ff:fe:9b:cb:46:1:0x0008
  device_id: bb2e292f76d182ffc8c4a2c363793562
  endpoint_id: 1
  cluster_id: 8
  command: step_with_on_off
  args:
    - 0
    - 43
    - 5
  params:
    step_mode: 0
    step_size: 43
    transition_time: 5
origin: LOCAL
time_fired: "2023-12-27T03:06:14.245384+00:00"
context:
  id: 01HJMKSAB593KDKD8KY9XVK65D
  parent_id: null
  user_id: null

I used the contents of the data: as in the toggle automations, but the trigger never fires.
Do you see the problem?
Is this method documented anywhere?

Thanks

UPDATE
Fixed it by removing the params: section.

1 Like

Check out the 2 blueprints for ZHA I linked, it should make this a lot easier; they support the 3 types of “press” too. Although you already did most of the work manually.

Experimenting with my Zigbee button, I determined that all that the automation trigger needs is the device_ieee:

alias: Office Light on with button press
description: ""
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_ieee: 84:ba:20:ff:fe:d2:33:5e
condition: []
action:
  - service: switch.toggle
    target:
      entity_id: switch.officelight
    data: {}
mode: single

In fact… I did more experimenting and determined that the trigger can be any one of:

      device_ieee: 
      unique_id:
      device_id:

Interesting, I have not checked, but I assumed the device ID would not change when I pressed different buttons on my device. And therefore I couldn’t create unique automations based on different functions. I will have to test this. Thanks.

I recall that the triggers are AND and the conditions are OR. So by using all of the data from the event:

      device_ieee: 
      unique_id:
      device_id:

means that it would take all three to match. So in the case of a multifunction button, like my Ikea Tradfri button, the command: is necessary also.

Actually I am pretty sure that in automations the triggers are OR’d and the conditions are AND’ed. Any individual trigger will start the automation. If an individual entity has multiple property items I would expect these to be only redundant and not affect the trigger. But again, I will test this. I expected that the property items for a particular switch entity would not change.

The blueprints work and everything - but why is it so obtuse? Surely the ZHA button should automatically create a button device?

You would think so. But what if your Zigbee device has five buttons (like the Ikea Button)?

I don’t have any expertise with zigbee, so I’m brainstorming a little. But either it should register 5 button devices (in the same way that a device with 5 controllable light bulbs might register).

Failing that, maybe a helper could be used to expose the events as a button?

The current user experience is horrible - took me ages to figure out how to even see my device’s ID and I’d consider myself a reasonably technical user. Anything is better than this!

1 Like