Home-Assistant_Config
Home-Assistant_Config copied to clipboard
Low battery themplate
To accompany video To-Do lists, here is code used in automation:
For trigger, this is used:
platform: template
value_template: |2-
{% for state in states.sensor | selectattr('attributes.device_class', '==', 'battery') %}
{% if 0 <= state.state | int(-1) < 20 | int %}
{% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state ~ ' %)'] %}
{% endif %}
{% endfor %}
And in action, following code is used:
service: todo.add_item
target:
entity_id: todo.shopping_list_2
data:
item: >-
{% set result = namespace(sensors=[]) %} {% for state in states.sensor |
selectattr('attributes.device_class', '==', 'battery') %}
{% if 0 <= state.state | int(-1) < 10 | int %}
{% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state ~ ' %)'] %}
{% endif %}
{% endfor %} {% for state in states.binary_sensor |
selectattr('attributes.device_class', '==', 'battery') | selectattr('state',
'==', 'on') %}
{% set result.sensors = result.sensors + [state.name] %}
{% endfor %} {{result.sensors|join(', ')}}
This will push to To-Do list called todo.shopping_list_2
list of all entities (names) that have battery power between 0 and 20%