Template config error

OK, I;ve poured over this and can’t find my error. Does it jump out at anyone?

Invalid config for ‘template’ at templates.yaml, line 6: invalid template (TemplateSyntaxError: expected token ‘end of statement block’, got ‘state’)

sensor:
    ##### Battery Levels | Smoke Detectors #####
    - name: "Smoke Detector Batteries"
      icon: >
       mdi:battery-low
      state: >
        {% set threshold = states('input_number.battery_threshold') | int %}
        {%- set ns = namespace(sensors=[]) -%}
        {%- set for state in states.sensor
          | selectattr('attributes.device_class', 'defined')
          | selectattr('attributes.state_class', 'defined')
          | selectattr('attributes.device_class', '==', 'battery')
          | selectattr('attributes.state_class', '==', 'measurement')
          | selectattr('state', 'is_number') -%}
          {%- if state.state | int <= threshold -%}
            {% set ns.sensors = ns.sensors + [dict(name = state.name | replace(' battery', '') | replace(' Battery', ''), state = state.state | int)] %}
          {%- endif -%}
        {%- endfor -%}
        {%- set batt = ns.sensors | sort(attribute='state') %}
        {%- set ns = namespace(batt='') -%}
        {%- for state in batt -%}
            {%- set ns.batt= ns.batt + (state.name ~ ' (' ~ state.state ~'%)' ~"\n") %})
        {%- endfor -%}
        
        {% if ns.batt | count > 0 %}
          {{ ns.batt | truncate(255, true, '...') }}
        {% else %}
          {{ 'unavailable' }}
        {% endif %}

The third line of your template should not begin with set. Also you should not directly return unavailable as a state, you should use an availability template.

1 Like

Bingo, you rock. Thanks for taking the time to help.