While scanning for the next token found character '%' that cannot start any token in "/config/automations.yaml",

I’m getting the follow error

  • while scanning for the next token found character ‘%’ that cannot start any token in “/config/automations.yaml”, line 10, column 8 for the yaml below.
- alias: 'Smart button front door'
  id: 14115d72-6bea-41c1-8411-1eaf41b4ef2b
  trigger:
    - platform: state
      entity_id: input_boolean.front_door_tap
      to: 'on'
      from: 'off'
    - service: >
      {%- for light in states.light %}
      {%- if is_state(light.entity_id, 'on') %}
        {{light.entity_id}}.turn_off
      {%- endif %} 
     {%-endfor%}

what am i doing wrong?

your indentation is off:

- alias: 'Smart button front door'
  id: 14115d72-6bea-41c1-8411-1eaf41b4ef2b
  trigger:
    - platform: state
      entity_id: input_boolean.front_door_tap
      to: 'on'
      from: 'off'
    - service: >
        {%- for light in states.light %}
          {%- if is_state(light.entity_id, 'on') %}
            {{light.entity_id}}.turn_off
          {%- endif %} 
       {%-endfor%}

However the template won’t work the way you are trying to use it.

Ever action needs a service call. In your case you want to turn off lights that are on.

so you need to call the light.turn_off service. but the light.turn_off service needs an entity_id (or multiple)

so in it’s basic for it looks like this:

- service: light.turn_off
  entity_id: light.some_light

what you will end up with when the template is rendered on the first light that’s on is:

- service: some_light.turn_off

which won’t work.

hopefully you see the difference.