Template sensors work in Developer Tools --> Template, but preventing HA restart

I’m trying to create a script for daily weather announcements. I have two sensors that contain strings that I intend to feed through TTS in Node-RED. Both of these sensors output the desired strings in Template, but they don’t pass muster when checking configuration:

  - sensor:
    - unique_id: tts_weather_current
      name: TTS Weather Current
      state: >
        It's currently {{states.sensor.dark_sky_temperature.state|round}} degrees, and feels like {{states.sensor.dark_sky_apparent_temperature.state|round}}.
  - sensor:
    - unique_id: tts_weather_forecast
      name: TTS Weather Forecast
      state: >
      {% set current = states('sensor.dark_sky_current_text') %}
      {% set later = states('sensor.dark_sky_daily_summary') %}
      {% if (current == later) %}
        It will be {{current}} outside today.
      {% else %}
        It is {{current}} outside, and it will be {{later}} later today. The apparent high temperature will be #{states('sensor.dark_sky_daytime_high_apparent_temperature_0d')|round}}.
      {% endif %}
  - sensor:
    - unique_id: tts_weather_wind
      name: TTS Weather Wind
      state: >
      {% set current = (states.sensor.dark_sky_wind_speed.state|round) %}
      {% set later = (states.sensor.dark_sky_wind_speed_0d.state|round) %}
      {% if (current == later) %}
        The wind is blowing {{states.sensor.dark_sky_alt_wind.state}} with gusts up to {states.sensor.dark_sky_wind_gust_0d.state|round}}.
      {% elif (current > later) %}
        The wind is blowing {{ states.sensor.dark_sky_alt_wind.state }}. Later today, the wind speed will decrease to {{ later }} miles per hour #with gusts up to {{states.sensor.dark_sky_wind_gust_0d.state|round}}.
      {% else %}
        The wind is blowing {{ states.sensor.dark_sky_alt_wind.state }}. Later today, the wind speed will increase to {{ later }} miles per hour #with gusts up to {{states.sensor.dark_sky_wind_gust_0d.state|round}}.    
      {% endif %}

The error I’m getting is this:

Error loading /config/configuration.yaml: while scanning for the next token
found character '%' that cannot start any token
in "/config/templates.yaml", line 390, column 8

Line 390, column 8 is the first opening bracket ‘{’ in my template sensor tts_weather_forecast. I’ve included the previous template sensor in case that’s of relevance.

I have no idea what’s happening here. I’ve deleted and retyped, I’ve checked spacing, I’ve changed around the syntax of the variables and state references, and while all of these sensors work in template, they are keeping HA from restarting due to the error. The one clue I have is that Notepad++ bolds the result section of each template sensor, but does not bold from this sensor on:

Apparently, Notepad++ recognizes an issue that I don’t. If I comment out these two sensors, all is well. Any guidance?

Because they’re not indented correctly.

Indent the two sections, that your screenshot identifies as “NOT BOLD”, by an additional two spaces. The section marked “BOLD” is indented correctly.

The Template Editor evaluates Jinja2 templates. It doesn’t evaluate YAML syntax. Therefore it confirmed your template works but can’t confirm you will use it correctly within a YAML document.


NOTE

Your screenshot shows you have defined at least four Template Sensors. It’s not necessary to repeat the key word sensor: before each sensor’s configuration. You can specify it once and then list all four sensor configurations under it.

Spacing is in fact the issue. I just found that this works:

  - sensor:
    - unique_id: tts_weather_forecast
      name: TTS Weather Forecast
      state: >
        {% set current = states('sensor.dark_sky_current_text') %}
        {% set later = states('sensor.dark_sky_daily_summary') %}
        {% if (current == later) %} It will be {{current}} outside today.
        {% else %} It is {{current}} outside, and it will be {{later}} later today. The apparent high temperature will be {{states('sensor.dark_sky_daytime_high_apparent_temperature_0d')|round}}.
        {% endif %}
  - sensor:
    - unique_id: tts_weather_wind
      name: TTS Weather Wind
      state: >
        {% set current = (states.sensor.dark_sky_wind_speed.state|round) %}
        {% set later = (states.sensor.dark_sky_wind_speed_0d.state|round) %}
        {% if (current == later) %} The wind is blowing {{states.sensor.dark_sky_alt_wind.state}} with gusts up to {{states.sensor.dark_sky_wind_gust_0d.state|round}}.
        {% elif (current > later) %} The wind is blowing {{ states.sensor.dark_sky_alt_wind.state }}. Later today, the wind speed will decrease to {{ later }} miles per hour with gusts up to {{states.sensor.dark_sky_wind_gust_0d.state|round}}.
        {% else %} The wind is blowing {{ states.sensor.dark_sky_alt_wind.state }}. Later today, the wind speed will increase to {{ later }} miles per hour with gusts up to {{states.sensor.dark_sky_wind_gust_0d.state|round}}.    
        {% endif %}

I found it when trying to standardize the spacing of some of my other sensors.

I have a lot of template sensors defined here… it’s in my templates.yaml file, referenced by configuration.yaml. It stems from the fact that I have that document split into multiple sections with binary sensors mixed it. It’s been working this way for a few years… maybe a year in its present format. I’ll revisit this after I get my script working, thank you.