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
2 Likes