BeardedTinker / Home-Assistant_Config

BeardedTinker Home Assistant configuration. Feel free to browse, edit, tweak, improve, criticize and suggest.
MIT License
445 stars 50 forks source link

Low battery themplate #111

Open BeardedTinker opened 11 months ago

BeardedTinker commented 11 months ago

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%

stale[bot] commented 8 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.