Longpress and Shortpress script/Automation for Xiaomi Round button with ZHA

For more then 2 years now i am using a hardware modified xiaomi round buttons behind my Gira switches. I designed a special holder for this (see:https://www.thingiverse.com/thing:3508223 )

Currently i am using the xiaomi mija gateway and the older integration. That integration has support for a longpress event, which i am using in several automations.

I am now investigating moving my zigbee devices (Ikea lights and xiaomi stuff) to the new Sonoff Zigbee Gateway (modified so it can be used in homeassistant) with ZHA.
It appears that the ZHA integration only expose the raw events and doesn’t have a longpress event implemented for the xiaomi round switch. (Lumi.sensor_switch in ZHA).

With the new release of core 0.115.x there is now a “wait_for_trigger” available in the actions, that makes it much easier and nicer to implement this without “helpers”.

I therefore share my solution for those who are facing the same problems.
Replace the <device_ieee> with your id.


- id: '1600849795061'
  alias: TranslateEvents
  description: ''
  - platform: event
    event_type: zha_event
      device_ieee: 00:15:8d:00:02:93:04:11
      command: attribute_updated
        attribute_id: 0
        attribute_name: on_off
        value: true
  condition: []
  - wait_for_trigger:
    - platform: event
      event_type: zha_event
        device_ieee: 00:15:8d:00:02:93:04:11
        command: attribute_updated
          attribute_id: 0
          attribute_name: on_off
          value: false
    continue_on_timeout: true
    timeout: 00:00:01
  - choose:
    - conditions:
      - condition: template
        value_template: '{{ not wait.trigger }}'
      - event: Longpress
        event_data: {}
    - event: Shortpress
      event_data: {}
  mode: single

Hi @jodur,
This sounds great, thanks for sharing - can you also show how you use long press in an automation with this?

Is is very easy. The above script generates 2 events(Shortpress and Longpress). You have to create an automation, with a trigger who listens to the event.

1 Like

Got it, very helpful, thanks!

@jodur, any ideas on how to modify the automation to accept multiple buttons? I was about to create a new automation for each button I have but feel like there would be a way to template this, just cant work out how!

You could use the new ‘Blueprints’ feature for this. There was already somebody who made a blueprint for the normale events for the Miija round button, so i suggested him to add the longpress from my example.
(see: https://community.home-assistant.io/t/zha-xiaomi-mijia-round-wireless-switch-wxkg01lm-lumi-sensor-switch/258472 )
I didn’t receive any feeback on it. Maybe if i have some time left over this weekend, i will do it my self to implement this. Coudn’t be that difficult, only have to do some more detail reading on blueprints.

1 Like

@djj and others who are intrested. I have created a Blueprint for this button.
see: https://community.home-assistant.io/t/zha-xiaomi-mijia-round-wireless-switch-wxkg01lm-with-hold-lumi-sensor-switch/263075


Champion, thanks for that!

Hi @jodur ,

Many thanks for your contribution which indeed is really helpful for those with the 1st gen Xiaomi buttons.

I tried to make an automation based on this topic for long-press and release function of the button to dim or brighten a light using an input_boolean. You can see my almost working yaml here.

The thing is that the dim/brighten automation was initially based on the Xiaomi integration which exposed the long-press and release functions but after reading your post I understood why it is not working with my config (I am using the buttons through the ZHA integration which obviously does not support the long-press event).

It would be super helpful if you had the time and could help with this dim and brighten automation using the way you discovered above. I tried to do it myself but unfortunately I am not getting any results.

Thank you!