Xiaomi single automation for wireless button g1

Hello guys!

I was trying to create just one automation for all functions of the Xiaomi Aqara Wireless Button generation 1.

This would simplify my automation managment and make it simple by just using scripts triggered by the click type event.

This did not work… Can someone help me?

- alias: Botão Xiaomi quarto
  trigger:
    - platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d000120ea75
        click_type:
          - single
          - double
          - long_click_press
  action:
    - service_template: >
        {% if trigger.event.click_type == 'single' %}
          script.botao_xiaomi_quarto_click_1
        {% elif trigger.event.click_type == 'double' %}
          script.botao_xiaomi_quarto_click_2
        {% elif trigger.event.click_type == 'long_click_press' %}
          script.botao_xiaomi_quarto_click_3
        {% endif %}

Any idea why?

That would help me a great deal since I have 10 buttons x 3 automation each hehe

Thanks!

1 Like

The last 2 in the action aren’t a service :), tho’ I’m not sure if it will work anyway.

   action:
     - service_template: >
         {% if trigger.event == "sunset" %}
         homeassistant.turn_on
         {% elif trigger.event == "sunrise" %}
         homeassistant.turn_off
         {% endif %}

       entity_id: switch.s4_cam_night_vision

You may need something like this…

 - service: homeassistant.turn_on
   data_template:
     entity_id: >- 
         {%- if trigger.payload == "bedroom lights"  or trigger.payload == "bedroom light" -%}
           script.bedroomlights
         {%- elif trigger.payload == "bedroom" -%}
           script.bedroomallswitches
         {%- endif %}
1 Like

Hello @keithh666.

You were right, I did forgot the script. before the service. I’ve changed and still not work.

Also, trigger.payload did not work for me…

I think the main problem is how to get te event type from the trigger data and then start scripts using service template…

Any ideas?

Thanks!!

I think the way you have the click event is basically correct but if you look at the way I’ve done it, you will see there is a call to a service (light,switch,scene,script,homeassistant, etc) followed by the entity id (script.botao_xiaomi_quarto_click_1). So your action cannot work as you don’t call a service first :slight_smile:

So for you I would do something like…

action:
  - service: homeassistant.turn_on
    data_template:
      entity_id: >- 
        {% if trigger.event.click_type == 'single' %}
           script.botao_xiaomi_quarto_click_1
        {% elif trigger.event.click_type == 'double' %}
           script.botao_xiaomi_quarto_click_2
        {% elif trigger.event.click_type == 'long_click_press' %}
           script.botao_xiaomi_quarto_click_3
        {% endif %}

Hope that helps :slight_smile:

Sorry my friend… It did no work.

I tough that scripts did not need entity ID to run with service_template.

I am not sure if this works also:

click_type:
  - single
  - double
  - long_click_press

If I can use multiple click_types like this. I’ve tried to separate the triggers without luck…

I cannot find a way to read the automation’s trigger data.

Anyways, thanks for you time!

Yep I think the same the trigger may have to be split into 3 automations, tho’ in theory it should work. Hmm thinking about it try it like this…

- alias: Botão Xiaomi quarto
  trigger:
    - platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d000120ea75
        click_type: single
    - platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d000120ea75
        click_type: double
    - platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d000120ea75
        click_type: long_click_press
  action:
    - service: homeassistant.turn_on
      data_template:
        entity_id: >- 
          {% if trigger.event.click_type == 'single' %}
             script.botao_xiaomi_quarto_click_1
          {% elif trigger.event.click_type == 'double' %}
             script.botao_xiaomi_quarto_click_2
          {% elif trigger.event.click_type == 'long_click_press' %}
             script.botao_xiaomi_quarto_click_3
          {% endif %}
1 Like

Ok, so this works for me:

- alias: Botão Xiaomi quarto
  trigger:
    - platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d000120ea75
        click_type: single
    - platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d000120ea75
        click_type: double
    - platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d000120ea75
        click_type: long_click_press
  action:
    - service_template: >
        {% if trigger.event.data.click_type == 'single' %}
          script.botao_xiaomi_quarto_click_1
        {% elif trigger.event.data.click_type == 'double' %}
          script.botao_xiaomi_quarto_click_2
        {% elif trigger.event.data.click_type == 'long_click_press' %}
          script.botao_xiaomi_quarto_click_3
        {% endif %}

It needed the “data” part here: trigger.event.data.click_type.

Also, I had to split the trigger click type.

Thanks!

2 Likes

Hi, this helped me a lot so thanks keithh666 and Schneider.:+1:

1 Like

Just out of curiousity: since the entity_id and the event_type are the same for all triggers, can’t you use a trigger like this?

- alias: Botão Xiaomi quarto
  trigger:
    - platform: event
      event_data:
        click_type: single
    - platform: event
      event_data:
        click_type: double
    - platform: event
      event_data:
        click_type: long_click_press
  action:
    - service_template: >
        {% if trigger.event.data.click_type == 'single' %}
          script.botao_xiaomi_quarto_click_1
        {% elif trigger.event.data.click_type == 'double' %}
          script.botao_xiaomi_quarto_click_2
        {% elif trigger.event.data.click_type == 'long_click_press' %}
          script.botao_xiaomi_quarto_click_3
        {% endif %}
1 Like

Hello!

I don’t think so, you need entity_id.

Actually I had changed my current script to fit better:

- alias: Botão Xiaomi quarto
  trigger:
    - platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d000120ea75
  action:
    - service_template: >
        {% if trigger.event.data.click_type == 'single' %}
          script.botao_xiaomi_quarto_click_1
        {% elif trigger.event.data.click_type == 'double' %}
          script.botao_xiaomi_quarto_click_2
        {% elif trigger.event.data.click_type == 'long_click_press' %}
          script.botao_xiaomi_quarto_click_3
        {% endif %}

This works like a charm.

Have a great day!

1 Like

True that!

1 Like

Lol, pocket typing

1 Like

I am trying to do something very similar but simpler (I think) but it’s not quite working. I have 3 switches and I would like each type of click to toggle one of the three switches so I have

  trigger: 
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch_158d0002c60d00
  action:
    service: switch.toggle
    data_template:
      entity_id: >
        {% if trigger.event.click_type == 'single' %}
          switch.10003514bd
        {% elif trigger.event.click_type == 'double' %}
          switch.100035fe1e
        {% elif trigger.event.click_type == 'long_click_press' %}
          switch.10005e0b4b
        {% endif %}

Unfortunately when I click the switch nothing happens and I get the following error in the log.

 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.hannahs_switch. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data['entity_id']

Any advice much appreciated!

I realised what the problem was :slight_smile:
The code in the earlier examples didn’t work any more because the HA implementation for the Xiaomi switches changed a few months ago.
I looked at the Xiaomi sensor docs and the click_type is no longer an event attribute. It’s event.data now. So I just added “.data” in as needed and now this works.

  trigger: 
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch_158d0002c60d00
  action:
    - service: switch.toggle
      data_template:
        entity_id: >
          {% if trigger.event.data.click_type == 'single' %}
            switch.10003514bd
          {% elif trigger.event.data.click_type == 'double' %}
            switch.100035fe1e
          {% elif trigger.event.data.click_type == 'long_click_press' %}
            switch.10005e0b4b
          {% endif %}
1 Like