Home-Assistant_Config icon indicating copy to clipboard operation
Home-Assistant_Config copied to clipboard

Low battery themplate

Open BeardedTinker opened this issue 6 months ago • 1 comments

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%

BeardedTinker avatar Dec 12 '23 15:12 BeardedTinker