What is the proper way to support multiple event, entity_id, etc. that perform the same action?

In this particular case, I’m using an Xiamoi button and I’d like the single and double click to perform the same action without duplicating the code block.

This works for a single click:

##################################################################
- alias: lamp by button
##################################################################
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_lamp
      click_type: single
  action:
    service: switch.toggle
    entity_id: switch.lamp

Would this work for a double-click too?

click_type: single, double

Or, what about something like this, which I think I’ve seen in some automations but can’t seem to find it now:

click_type: [ 'single', 'double' ]

1 Like

Not exactly the same, but might help to give you some ideas.

- alias: 'Minimote Button Pressed or Held'
  trigger:
  - platform: event
    event_type: zwave.scene_activated
    event_data:
      entity_id: zwave.aeotec_minimote_1
  action:
  - service: homeassistant.toggle
    data_template:
      entity_id: >
        {% set map = {1: 'light.bedroom_lamp_one',
                      2: 'light.bedroom_lamp_one',
                      3: 'light.bedroom_lamp_two',
                      4: 'light.bedroom_lamp_two',
                      5: 'light.bedroom_ceiling_light',
                      6: 'light.bedroom_ceiling_light',
                      7: 'switch.bedroom_fan',
                      9: 'switch.bedroom_fan'} %}
        {{ map[trigger.event.data.scene_id|int] }}

this is working for my minimote. I know its not the same, but something like this might work for the action

  action:
  - service: homeassistant.toggle
    data_template:
      entity_id: >
        {% set map = {single: 'switch.lamp',
                      double: 'switch.lamp'} %}
        {{ map[trigger.event.data.click_id] }}

Im not familiar with the click event, so I dont know if it would be “click_id”, or just click, or something else. I like this setup (once you get it figured out, because it’s easy to change in the future

For a click event w/ entity_id of binary_sensor.switch_lamp, what are the possible values for click_type? Specifically, are there more possibilities than single and double? If not, then just remove the click_type line from the trigger. Then it will trigger no matter what click_type is.

If there are other possible values, and you only want the automation action to run for single and double, then you’ll still need to remove click_type from the trigger, but you’ll also need to add a condition that checks that trigger.event.data.click_type is only single or double. E.g.:

  condition:
    condition: template
    value_template: >
      {{ trigger.event.data.click_type in ['single', 'double'] }}
1 Like

Thank you, that definitely does give me some ideas!

I like the simplicity of this and it works great… thanks!

1 Like