Multilines on automation template- flash lights

Can somebody please help me with the correct format/syntax for this?
Goal: sensor state changes, check light status, flash light if it’s on, turn on if it was off.
I have most of it working, individually. I can’t find the correct format to put them together.
So to confirm flashing works, this snippet in an automation.yaml works fine:

- alias: Front Door Opened
  description: ''
  trigger:
    entity_id: binary_sensor.frontdoor_status
    platform: state
    from: 'off'
    to: 'on'
  action:
  - service: light.turn_on  
    data:
      entity_id:       
        - light.dining_room_lamp 
      flash: long  

To confirm I formatted the template/logic correctly, this works:

  action:   
    entity_id: light.dining_room_lamp  
    service_template: > 
      {% if is_state('light.dining_room_lamp', 'on') %}             
        light.turn_off         
      {% else %}
        light.turn_on
      {% endif %}

But putting the two together does not. I suspect it has something to do with mulit-lines in the data part, but I don’t know what to do.
I have tried about 2 hours of variations. The one below is just the one closet to what I would think it should be.

  action:
    entity_id: light.dining_room_lamp
    service_template: >   
      {% if is_state('light.dining_room_lamp', 'off') %}             
        light.turn_on         
      {% else %}
        light.turn_on
        data:
	  flash: long
      {% endif %}	

have a look at light.toggle service!

Thanks, but toggling the light is not actually the goal. In fact, that second test snippet above does that. My problem is figuring out which part of yaml syntax HA uses.
ie this > symbol is folding, this | symbol is literal. And I’ve tried block chomping too…
But combinations of my efforts has not found the correct one.

Your yaml spacing looks a little off in the last snippet - have a look at making the data: and flash: blocks inline with each other.

Understand your desire to understand the HASS formatting, but the need for testing the state for a single light goes away if you use the light.toggle service, a delay, and then a second light.toggle (all in the same automation actions) - the light will return to its original state with the “flash” you seem to be looking for.

You have to remember that templates are rendered/evaluated at run time, and the YAML files are processed only at startup. Hence templates must be constrained to values within YAML. I.e., they cannot be used to dynamically control key/value pairs. E.g., you can’t selectively include a call parameter using templates. You can only use templates to dynamically calculate a parameter’s value.

BTW, you’re not alone in making this mistake. It seems everyone falls into this trap at some point in their early HA learning curve.

Anyway, you can use a script to do what you want. E.g., this would be the automation:

- alias: Front Door Opened
  description: ''
  trigger:
  - entity_id: binary_sensor.frontdoor_status
    platform: state
    from: 'off'
    to: 'on'
  action:
  - service_template: >
      {% if is_state('light.dining_room_lamp', 'on') %}
        script.flash_light
      {% else %}
        light.turn_on
      {% endif %}
    data:
      entity_id: light.dining_room_lamp

and this would be the script:

flash_light:
  sequence:
  - service: light.turn_on
    data_template:
      entity_id: "{{ entity_id }}"
    data:
      flash: long

This assumes, of course, that a light that is on will flash as you want when you call the light.turn_on service with flash: long. I can’t test that part because I don’t have any lights that support the flash parameter.

Thanks! Your code works out-of-the-box with one exception. I had to hard-code the entity_id: light.dining_room_lamp in the script. I have not successfully passed the entity_id as a variable. I’ve attempted a few things based on the pages below, but no luck yet.


Oops, my bad. I fixed it. (Needed to put entity_id under data:.)

Ah. I was trying to put it at the same level as the script call. After seeing it this way, it makes sense. Awesome. Thanks!
BTW: Your bit about “templates are rendered/evaluated at run time, and the YAML files are processed only at startup” is actually spot-on. Thinking of it that way does help clear up some confusion I had.

1 Like