Xiaomi Wireless Switch long press trigger

Hello community, I have the Xiaomi Wireless Switch with 3 triggers (action is toggle light):

  trigger:
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001b1bbb1
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001b1bbb1
      click_type: double_click
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001b1bbb1
      click_type: long_click_press

Both click and double_click work perfect, instead, long_click_press doesn’t work as expected. I would like to long press the switch and toggle the light.

What happen is:
Long press: Light ON
Release: Light OFF

What’s wrong? Thank you,
Lucas

Which switch, the round or square one?

Round switch

Try “click_type”: “hold”

single, double, hold

Here is my config:

trigger:
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_xxxxx
      click_type: single
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_xxxxx
      click_type: double
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_xxxxx
      click_type: long_click_press

click_type for each event

“Hold” have the same behaviour.
I also have click_type for each event :slight_smile: Is it working for you? That’s odd, your configuration is quite the same.

Btw, Yevgeniy, which is your actions? Out of curiosity since my next step will be create two actions based on 2 or 3 triggers. I mean:

  • if one click then turn on/off light 1
  • if double click then turn on/off light 2
  • if long press then turn on/off light 2

Yes. Works all three events. Sorry, in your message first event have not click_type. I use:

  • Single click - light 1
  • Double click - light 2
  • Long press - skip alarm clock

Here is my action:

action:
  - service_template: >
      {% if trigger.event.data['click_type'] == 'long_click_press' %}
        xiaomi_aqara.stop_ringtone
      {% endif %}
    data:
      gw_mac: 'AA:BB:CC:DD:EE:FF'
  - service: light.toggle
    data_template:
      entity_id: >
        {% if trigger.event.data['click_type'] == 'single' %}
          light.xiaomi_philips_led_1
        {% elif trigger.event.data['click_type'] == 'double' %}
          light.gateway_light_xxxxx
        {% endif %}

Thank you, thank you, thank you it works perfect!!! You save ne a lot of headaches :slight_smile:
And what about if I wanto to toggle multiple lights like? :

{% elif trigger.event.data['click_type'] == 'double' %}
   light.gateway_light_1_xxxxx
   light.gateway_light_1_xxxxx
{% endif %}

Unfortunately the above code doesn’t work.

Is this part of the condition? If this is the whole condition it should look like this:

{% if trigger.event.data['click_type'] == 'double' %}
   light.gateway_light_1_xxxxx
   light.gateway_light_1_xxxxx
{% endif %}

Nope, the whole action is the following (I want to switch off 2 lights on long press):

- service: light.toggle
  data_template:
    entity_id: >
      {% if trigger.event.data['click_type'] == 'single' %}
        light.yeelight_ceiling4_mibt77360569_miio_7811dc5xxxxx
      {% elif trigger.event.data['click_type'] == 'double' %}
        light.yeelight_ceiling4_mibt77361672_miio_7811dcdxxxxx
      {% elif trigger.event.data['click_type'] == 'long_click_press' %}
        light.yeelight_ceiling4_mibt77361672_miio_7811dc5xxxxx
        light.yeelight_ceiling4_mibt77360569_miio_7811dcdxxxxxx
      {% endif %}

EDIT: This config simply do nothing on long press.
Obliviously with only one light it works perfect

Try it like this:

- service: light.toggle
  data_template:
    entity_id: >
      {% if trigger.event.data['click_type'] == 'single' %}
        light.yeelight_ceiling4_mibt77360569_miio_7811dc5xxxxx
      {% elif trigger.event.data['click_type'] == 'double' %}
        light.yeelight_ceiling4_mibt77361672_miio_7811dcdxxxxx
      {% endif %}
- service_template: >
    {% if trigger.event.data['click_type'] == 'long_click_press' %}
      light.turn_off
    {% endif %}
  entity_id:
    light.yeelight_ceiling4_mibt77361672_miio_7811dc5xxxxx, light.yeelight_ceiling4_mibt77360569_miio_7811dcdxxxxxx

No way :frowning:
It doesn’t work well and from log I can see:

2018-04-13 21:35:06 ERROR (MainThread) [homeassistant.core] Invalid service data for light.toggle: Entity ID is an invalid entity id for dictionary value @ data[‘entity_id’]. Got ‘’
2018-04-13 21:35:06 ERROR (MainThread) [homeassistant.helpers.service] Template rendered invalid service

Btw, I changed light.turn_off in light.toggle because I want Toggle both lights when long pressed. However light.turn_off works perfect and turn off both lights.

Hey. I was wondering why you put all the click types as triggers just to have to sort them in action!? Why not just write three simple automations instead of a complex one? I use mine like this:

- id: botao_sala1_single
  alias: (bs1s) liga ou desliga o spot da sala
  hide_entity: True
  trigger:
    platform: event
    event_type: click
    event_data:
        entity_id: binary_sensor.switch_xxxx
        click_type: single
  action:
      service: light.toggle
      entity_id: light.rc1

- id: botao_sala1_double
  alias: (bs1d) alterna profiles do spot da sala
  hide_entity: True
  trigger:
    platform: event
    event_type: click
    event_data:
        entity_id: binary_sensor.switch_xxxx
        click_type: double
  action:
      - service: input_select.select_next
        entity_id: input_select.cor_table
      - service: light.turn_on
        data_template:
          entity_id: light.rc1
          profile: >-
             {{ states('input_select.cor_table') }}

- id: botao_sala1_hold
  alias: (bs1h) hold sala
  hide_entity: True
  trigger:
    platform: event
    event_type: click
    event_data:
        entity_id: binary_sensor.switch_xxxx
        click_type: hold
  action:
        service: light.toggle
        entity_id: light.spot_sala

Yes sure, I can split the triggers in 3 IDs but for a clean GUI and for programming knowledge (I’m already a programmer of other languages) I prefer to find a solution all in one! :wink:

I just tested this automation, everything works fine

  - service: light.toggle
    data_template:
      entity_id: >
        {% if trigger.event.data['click_type'] == 'single' %}
          light.xiaomi_philips_led_1
        {% elif trigger.event.data['click_type'] == 'double' %}
          light.gateway_light_xxxxx
        {% elif trigger.event.data['click_type'] == 'long_click_press' %}
          light.xiaomi_philips_led_1, light.gateway_light_xxxxx
        {% endif %}

I did not notice this before, I also get errors in the log when automation been triggered. At the same time, everything works 
 :roll_eyes:

1 Like

Well, I adjusted my whole automation basing on your advices, and now it works fine, maybe I had somethings dirty on configuration. Many thanks for your help :wink:

this is awesome. I have just started to change all my xiaomi button automations to match your example
 3 automations become 1! I dont quite understand the syntax of the template system yet so hopefully this will help me learn. Thank you!

a question on the use of this as an action
 would it be possible to split this up into 2 services as per this:

action:
  - service: light.toggle
    data_template:
      entity_id: >
        {% if trigger.event.data['click_type'] == 'single' %}
          light.bedroom_lamp
        {% elif trigger.event.data['click_type'] == 'double' %}
          light.wardrobe_light
        {% endif %}
  - service: homeassistant.turn_on
    data_template:
      entity_id: >
        {% elif trigger.event.data['click_type'] == 'long_click_press' %}
          light.front_door_light
        {% endif %}

Its just that I have a couple of Xiaomi buttons that currently have light toggle functions for single and double clicks but a completely different action for long press. I’d love to be able to include the long press automation into the same one as the other two
 but maybe that’s not possible

Yes. You can use this automation

action:
  - service_template: >
      {% if trigger.event.data.click_type == 'single', 'double' %}
        light.toggle
      {% endif %}
    data_template:
      entity_id: >
        {% if trigger.event.data.click_type == 'single' %}
          light.bedroom_lamp
        {% elif trigger.event.data.click_type == 'double' %}
          light.wardrobe_light
        {% endif %}
  - service_template: >
      {% if trigger.event.data.click_type == 'long_click_press' %}
        homeassistant.turn_on
      {% endif %}
    data:
      entity_id: light.front_door_light

This works, but it can show errors in the log.

awesome! thanks. Now
 to complicate things my actual automation for the long_click_press is currently as per below. Is it possible to include additional actions for the long press like this?

action:
  - service: homeassistant.turn_on
    entity_id: light.front_door_light
  - delay: '00:01:00'
  - service: homeassistant.turn_off
    entity_id: light.front_door_light