Automation Service template question

I am not understanding something about how templating is working in an automation. I have a Xiaomi switch with three click types. Single, Double and long press. That’s working fine. I have tested how to code for these three events in another automation which works well.

I would like single click to toggle a light, I would like a double click to turn on the light to a specific brightness and temperature setting and long click to turn on to a different brightness and temp setting. Here’s my code

- alias: Hannahs Switch 2
  trigger: 
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch_158d0002caxxxx
  action:
    service_template: >
        {% if trigger.event.data.click_type == 'single' %}
          light.toggle
        {% elif trigger.event.data.click_type == 'double' %}
          light.turn_on
        {% elif trigger.event.data.click_type == 'long_click_press' %}
          light.turn_on
        {% endif %}
    entity_id: light.hannahs_light
    data_template:
      brightness_pct: >
        {% if trigger.event.data.click_type == 'single' %}
        {% elif trigger.event.data.click_type == 'double' %}
           100
        {% elif trigger.event.data.click_type == 'long_click_press' %}
           40
        {% endif %}
      kelvin: >
        {% if trigger.event.data.click_type == 'single' %}
        {% elif trigger.event.data.click_type == 'double' %}
           4000
        {% elif trigger.event.data.click_type == 'long_click_press' %}
           2700
        {% endif %} 

I have left the settings for brightness and temp blank for single click (toggle) because I don’t want them specified when the light is toggled on.

The result of this is that the turn_on commands work perfectly, but the toggle doesn’t. Nor does it work if I change toggle to turn_off, nor does it work if I take out the IF statement for single click…

What am I missing please? Thanks in advance.

You can’t do this. This is what your template evaluates to on single click:

service: light.toggle
entity_id: light.hannahs_light
data:
  brightness:
  kelvin:

This is not a valid call of the light.toggle service

Neither kelvin nor brightness are service data attributes for light.toggle (even if the values are left blank).

2 Likes

Thanks.
So I guess the only solution is to have 2 automations right? One to handle the single click event and the other to handle the other types of click?
Unless there is syntax that lets me template out the brightness_pct and kelvin lines altogether but I don’t think I can right?

Yes, 2 automations. You’d want to constrain them so they both don’t fire

- alias: Hannahs Switch 2 (single click)
  trigger: 
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch_158d0002caxxxx
  condition:
    condition: template
    value_template: "{{ trigger.event.data.click_type == 'single' }}"
  action:
    service: light.toggle
    entity_id: light.hannahs_light
- alias: Hannahs Switch 2 (double click and long press)
  trigger: 
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch_158d0002caxxxx
  condition:
    condition: template
    value_template: "{{ trigger.event.data.click_type in ['double', 'long_click_press'] }}"
  action:
    service: light.turn_on
    data_template:
      entity_id: light.hannahs_light
      brightness_pct: >
        {% if trigger.event.data.click_type == 'double' %}
          100
        {% else %}
          40
        {% endif %}
      kelvin: >
        {% if trigger.event.data.click_type == 'double' %}
          4000
        {% else %}
          2700
        {% endif %}

And if you want to shorten the second automation templates (warning contains higher level code):

- alias: Hannahs Switch 2 (double click and long press)
  trigger: 
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch_158d0002caxxxx
  condition:
    condition: template
    value_template: "{{ trigger.event.data.click_type in ['double', 'long_click_press'] }}"
  action:
    service: light.turn_on
    data_template:
      entity_id: light.hannahs_light
      brightness_pct: "{{ 100 if trigger.event.data.click_type == 'double' else 40 }}"
      kelvin: "{{ 4000 if trigger.event.data.click_type == 'double' else 2700 }}"

Great. Many thanks.
And thanks for the improved code. I knew if I posted here, someone would be kind enough to give me the neatest code rather than my version which fills my whole screen!

And I learned some cool coding tricks.

Thanks! :+1: :grinning: