Schneider
(Antonio Schneider)
November 24, 2017, 7:58pm
1
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
keithh666
(Keith Hull)
November 25, 2017, 5:19am
2
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
Schneider
(Antonio Schneider)
November 25, 2017, 12:21pm
3
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!!
keithh666
(Keith Hull)
November 25, 2017, 12:54pm
4
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
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
Schneider
(Antonio Schneider)
November 25, 2017, 1:19pm
5
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!
keithh666
(Keith Hull)
November 25, 2017, 1:31pm
6
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
Schneider
(Antonio Schneider)
November 25, 2017, 1:37pm
7
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
umerfraz
(Umer Fraz)
March 24, 2018, 8:07pm
8
Hi, this helped me a lot so thanks keithh666 and Schneider.
1 Like
Bob_NL
(Bob Visser)
March 24, 2018, 8:16pm
9
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
Schneider
(Antonio Schneider)
March 24, 2018, 10:21pm
10
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
Michaelrch
(Michael R)
January 14, 2019, 8:12pm
14
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!
Michaelrch
(Michael R)
January 15, 2019, 7:08am
15
I realised what the problem was
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