Skip to content

Instantly share code, notes, and snippets.

@zubir2k
Last active February 2, 2024 03:46
Show Gist options
  • Save zubir2k/f7d16801d534ef4e772b57485316692f to your computer and use it in GitHub Desktop.
Save zubir2k/f7d16801d534ef4e772b57485316692f to your computer and use it in GitHub Desktop.
Battery Monitoring - Mobile/Tablets based on HA Companion App

Battery Monitoring

What is this? A simple jinja template that will show battery level for mobile devices only.

There are 2 options:

  1. Alert first, then list of device
  2. List of device first, then alert

image

type: vertical-stack
cards:
- type: markdown
content: >-
{% set ns = namespace(low_count=0) %}
{% for state in states.sensor | selectattr('entity_id', 'search',
'battery_level') %}
{% if state.state | int < 50 %}{% set ns.low_count = ns.low_count + 1 %}{%
endif %}{% endfor %}
{% if ns.low_count > 0 %}<ha-alert alert-type="warning">The device(s)
below requires attention.<br />Battery is running low:</ha-alert>
{% else %}<ha-alert alert-type="success">All devices are well
charged.</ha-alert>{% endif %}
{% for state in states.sensor | selectattr('entity_id', 'search',
'battery_level') %}
{% if state.state | int < 50 %}
- {{ state.name.split(' Battery')[0] | title }} ({{ state.state }}%)
Last Updated: {{ relative_time(state.last_updated) }} ago
{% endif %}{% endfor %}
title: 🔋 Low Battery
- type: markdown
content: >-
{% set ns = namespace(low_count=0) %}
{% for state in states.sensor | selectattr('entity_id', 'search',
'battery_level') -%}
{% if state.state | int < 50 %}
{% set ns.low_count = ns.low_count + 1 %}
- {{ state.name.split(' Battery')[0] | title }} ({{ state.state }}%)
Last Updated: {{ relative_time(state.last_updated) }} ago{% endif %}
{% endfor -%}
{% if ns.low_count > 0 %}<ha-alert alert-type="warning">The device(s)
above requires attention.<br />Battery is running low!</ha-alert>
{% else %}<ha-alert alert-type="success">All devices are well
charged.</ha-alert>{% endif %}
title: 🔋 Low Battery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment