Trap for players: Templates don't recognise comments (in the wrong place)

The following action:

action:
  service: script.turn_on
  data_template:
    entity_id: >-
      {% if states('sensor.aeotec_lounge_light_level') | float > 70 %} # Lights off
        script.lounge_rd_lights_off
      {% elif 41 > states('sensor.aeotec_lounge_light_level') | float > 30 %} # Lights 25%
        script.lounge_rd_lights_25
      {% elif 31 > states('sensor.aeotec_lounge_light_level') | float > 20 %} # Lights 50%
        script.lounge_rd_lights_50
      {% elif 20 > states('sensor.aeotec_lounge_light_level') | float %} # Lights 75%
        script.lounge_rd_lights_75
      {% else %}
        script.noop # no change required
      {% endif %}

Kept throwing this error:

Invalid service data for script.turn_on: Entity ID # lights 75%
  script.lounge_rd_lights_75 is an invalid entity id for dictionary value @ data['entity_id']. Got '# Lights 75%\n  script.lounge_rd_lights_75'

However as soon as I deleted the comments all was well. Kind of obvious now I look at it (the template will and should render all text if true).

This would be the correct way to comment in a template.

action:
  service: script.turn_on
  data_template:
    entity_id: >-
      {% if states('sensor.aeotec_lounge_light_level') | float > 70 %} 
        script.lounge_rd_lights_off # Lights off
      {% elif 41 > states('sensor.aeotec_lounge_light_level') | float > 30 %} 
        script.lounge_rd_lights_25 # Lights 25%
      {% elif 31 > states('sensor.aeotec_lounge_light_level') | float > 20 %}
        script.lounge_rd_lights_50 # Lights 50%
      {% elif 20 > states('sensor.aeotec_lounge_light_level') | float %} 
        script.lounge_rd_lights_75 # Lights 75%
      {% else %}
        script.noop # no change required
      {% endif %}
1 Like