Low battery alert in Lovelace UI

I have several z-wave devices/sensors that are battery-powered. I want a gentle reminder when those batteries get low, without cluttering my UI. Here’s how I used an entity filter card to solve that.

When there are no devices with low batteries, the card is hidden. This is what the low battery card looks like when a device has low battery:

image

(I don’t actually set the battery level threshold as high as 40%, but I needed to do so to get a screenshot.)

The battery level is displayed with both a numeric % and an icon by setting the device_class to battery.

Here’s an example configuration.yaml:

sensor:
  - platform: template
    sensors:
      kitchen_door_lock_battery:
        friendly_name: 'Kitchen Door Lock Battery'
        value_template: '{{ states.zwave.kwikset_unknown_type0003_id0446.attributes.battery_level }}'
        unit_of_measurement: '%'
        entity_id: zwave.kwikset_unknown_type0003_id0446
        device_class: battery

Here’s the ui-lovelace.yaml entry for the entity filter card. Note that you can define multiple state filters. These report battery_level in increments of 10, so I don’t have to define every value in between. Set show_empty to false to hide the card when no entities have a low battery.

    cards:
      - type: entity-filter
        entities:
          - sensor.front_door_lock_battery
          - sensor.kitchen_door_lock_battery
          - sensor.garage_tilt_sensor_battery
        state_filter:
          - "20"
          - "10"
          - "0"
        show_empty: false
        card:
          type: glance
          title: Low batteries
          column_width: 50%

Let me know if you have questions or suggestions for doing this better. I’m pretty new to this.

11 Likes

I would use the custom monster card with wildcard sensor.battery* as entity to not have to define all sensors manually.

1 Like

I use this but perhaps not usable in LoveLace

https://community.home-assistant.io/t/howto-create-battery-alert-without-creating-a-template-for-every-device/30576/371

I think he’s just showing people that he’s using the entity-filter to only display batteries below a specific threshold. He doesn’t want an actual alert, which is what you are referencing.

Thanks for sharing, I also used this in my own lovelace script :slight_smile:

1 Like

For some reason for me it doesn’t report the battery in 10% increments. If I filter for exact values, the entities show up, otherwise not. Any ideas why?

but does it allow to filter entities based on states?

Well by now the monster card is deprecated. Use auto-entities instead, and yes it can.

Just wanted to show what I have come up with from a variety of other ideas. This is a dynamically generated list I have on my maintenance page. Simple but I find effective:

      - type: vertical-stack
        title: Low Battery
        cards:
          - type: entities
            entities:
              - entity:  input_number.low_battery_alert_threshold
          - type: markdown
            content: |
              {% set result = namespace(sensors=[]) %}
              {% set ignore_entities = ['sensor.mid_1090ips_battery_level'] %}
              {% for state in states.sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') | rejectattr('entity_id', 'in', ignore_entities)%}
                {% if 0 <= state.state | int(-1) < (states.input_number.low_battery_alert_threshold.state | float) | int  %}
                  {% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state ~ ' %)'] %}
                {% endif %}
              {% endfor %}
              {% for state in states.binary_sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') | selectattr('state', '==', 'on') %}
                {% if not state.entity_id in exclude.entity_id %}
                  {% set result.sensors = result.sensors + [state.name] %}
                {% endif %}
              {% endfor %}
              {{result.sensors|join('\n')}}

Untitled

edit: added filter to prevent errors if the device_class is not defined

4 Likes

Hello
How do you create your entity input_number.low_battery_alert_threshold?

Can’t get this to work :face_with_raised_eyebrow:

Any ideas?

image

You haven’t shown your yaml but that is likely the issue looking at the partial error message

Just saw my mistace, you are correct.

I’d like to use the alert. But getting an error including it. Seems to be some yaml code thing but not sure where to look for.

I really like your Idea of this.
In case someone want to get this to work… because there are many hoops to jump throuh that many people might not know how to im going to summarize what to do with the most GUI-able way:

  1. Create input helper in https://your.home.assistant:8123/config/helpers

click on “number” and name it low_battery_alert_threshold.
Be sure that it has the “correct entity ID”, otherwise you need to edit later (which is pesky):

copy this configuration (which is ready for copy+paste)

type: vertical-stack
title: Low Battery
cards:
  - type: entities
    entities:
      - entity: input_number.low_battery_alert_threshold
  - type: markdown
    content: |
      {% set result = namespace(sensors=[]) %}
      {% set ignore_entities = ['sensor.mid_1090ips_battery_level'] %}
      {% for state in states.sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') | rejectattr('entity_id', 'in', ignore_entities)%}
        {% if 0 <= state.state | int(-1) < (states.input_number.low_battery_alert_threshold.state | float) | int  %}
          {% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state ~ ' %)'] %}
        {% endif %}
      {% endfor %}
      {% for state in states.binary_sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') | selectattr('state', '==', 'on') %}
        {% if not state.entity_id in exclude.entity_id %}
          {% set result.sensors = result.sensors + [state.name] %}
        {% endif %}
      {% endfor %}
      {{result.sensors|join('\n')}}

go to your lovelace and select “add card” and select vertical cart

on the bottom left you will see “show code editor”

grafik

paste the code from above now on the right side you should see the slider. if not, the helper-entity has another name and you didnt read the instruction good enough.

grafik