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.

automation:

- id: '1600849795061'
  alias: TranslateEvents
  description: ''
  trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_ieee: 00:15:8d:00:02:93:04:11
      command: attribute_updated
      args:
        attribute_id: 0
        attribute_name: on_off
        value: true
  condition: []
  action:
  - wait_for_trigger:
    - platform: event
      event_type: zha_event
      event_data:
        device_ieee: 00:15:8d:00:02:93:04:11
        command: attribute_updated
        args:
          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 }}'
      sequence:
      - event: Longpress
        event_data: {}
    default:
    - event: Shortpress
      event_data: {}
  mode: single
7 Likes

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

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

2 Likes

Champion, thanks for that!