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:54Invalid 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?