What's wrong with this sensor

Hello there,

I just found out one of battery on motion sensor pretty low and was thinking about a sensor which could give it to me. Luckily I found a video about it so I just copied the code from there as it is doing what I actually need.

Here is the code in templates.yaml

- sensor:
    - name: "Vyměnit baterku"
      unique_id: xxxxxxx...
      icon: mdi:battery-low
      state: >
        {% set threshold = states('input_number.battery_threshold') | int %}
        {%- set ns = namespace(sensors=[]) -%}
        {%- for state in states.sensor
          | selectattr('attributes.device_class', 'defined')
          | selectattr('attributes.state_class', 'defined')
          | selectattr('attributes.unit_of_measurement', 'defined')
          | selectattr('attributes.device_class', '==', 'battery')
          | selectattr('attributes.state_class', '==', 'measurement')
          | selectattr('attributes.unit_of_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 %}

There’s no error in editor, yaml validator says it’s ok, still when I checking it in developer there is a error notification which says:

Logger: homeassistant.config
Source: config.py:357
First occurred: 19:47:18 (8 occurrences)
Last logged: 19:59:54

Invalid config for ‘template’ at templates.yaml, line 45: invalid template (TemplateSyntaxError: expected token ‘end of statement block’, got ‘ns’) for dictionary value ‘sensor->0->state’, got '{% set threshold = states('input_number.battery_threshold') | int %} {%- set ns = namespace (sensors=) -%} {% 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') -%}\n {% if state.state | int <= threshold -%}\n {% set ns.sensors ns.senso…
Invalid config for ‘template’ at templates.yaml, line 45: invalid template (TemplateSyntaxError: expected token ‘end of statement block’, got ‘ns’) for dictionary value ‘sensor->0->state’, got '{% set threshold = states('input_number.battery_threshold') | int %} {%- set ns = namespace (sensors=) -%} {%- for state in states.sensor\n | selectattr('attributes.device_class', 'defined')\n | selectattr('attributes.state_class', 'defined')\n | selectattr('attributes.device_class', '==' 'battery')\n | selectattr('attributes.state_class', '==', 'measurement')\n | selectattr('state', 'is_number') -%}\n {%- if state.state | int <= threshold -%}\n {% set ns…
Invalid config for ‘template’ at templates.yaml, line 45: invalid template (TemplateSyntaxError: expected token ‘end of statement block’, got ‘ns’) for dictionary value ‘sensor->0->state’, got "{% set threshold = states(‘input_number.battery_threshold’) | int %} {%- set ns = namespace (sensors=) -%} {%- for state in states.sensor\n | selectattr(‘attributes.device_class’, ‘defined’)\n | selectattr(‘attributes.state_class’, ‘defined’)\n | selectattr(‘attributes.device_class’, ‘==’ ‘battery’)\n | selectattr(‘attributes.state_class’, ‘==’, ‘measurement’)\n | selectattr(‘state’, ‘is_number’) -%}\n {%- if state.state | int <= threshold -%}\n {% set ns.sensors ns.sensors + [dic…

Any idea what could be wrong?

Missing an equal sign.

Do a find on ‘ns.sensors ns.sensors’

Oops, that is a typo after I was checking it with image to text feature. But not really the solution, the result is still the same :confused:

I suspect you have another syntax error.

Is it really the same result? Or is the error different?

Something other than

expected token ‘end of statement block’, got ‘ns’

You are missing a comma on one of your select_attributes and on the same line where you missed an equal sign, there is something wrong with the dict.

ETA: the dict is also missing an equal sign.

You either copied something that did not work or copied it wrong.

First, I typed it from a screen later after let’s say quite a few checks I went for some image to text solution and that did made horrible things obviously. Anyway, the original error said something different than the one posted so there was something I typed wrong for sure but did not see. With your hint I made it correct finally. Thank you for your time and help, @jeffcrum

For others - if needed - corrected and functional is the edited original code in a topic. And of course my source: https://www.youtube.com/watch?v=VgV_ExVeSfE

1 Like