Notification on Low Battery Levels

This works for me, put it in your scripts and call it daily or whatever interval you like.

battery_check:
  alias: Battery Check

  sequence:
    - condition: template
      value_template: >-
        {% set battery_alert_threshold = 50|int %}
        {{ 
          states
          | selectattr('attributes.battery_level', 'defined') 
          | selectattr('attributes.battery_level','<', battery_alert_threshold ) 
          | list 
          | length >= 1 
        }}
        
    - service: script.turn_on
      entity_id: script.notify_all_engines
      data:
        variables:
          title: "Low Battery"
          who: "john"
          message: >-
            {% set battery_alert_threshold = 50|int %}
            {%- 
              set low_batteries = states 
              | selectattr('attributes.battery_level', 'defined') 
              | selectattr('attributes.battery_level','<', battery_alert_threshold ) 
              | map(attribute='entity_id') 
              | join(', ') 
            -%}
            Low batteries in the following devices: {{ low_batteries }}

(Edited to remove the expand as not needed when checking all states.)

2 Likes

ok my phone is at 5% now, no warning
The sensor low batteries has a state still 0
The automation didnt trigger either

Any ideas?

Did you try @jwelter example?

Think you need to clear up first of all what devices you’re trying to track here, those with a battery entity (such as a motion sensor) or those with a battery attribute (such as a mobile phone).

Have a look here at the screen shots of the two different types:

There’s method’s on here for both (trust me, took me a while to get my head around it all but got there!).

Have another crack at it and if it doesn’t work post your config back as that would help understand the problem…(and your use case that you’r e trying to answer).

It works to catch all our iOS devices using the mobile app to report low batteries so not sure what your problem is?

Can you put it into the template editor and see what it spits out?

No cause i tried your example…
I asked where to start and you give me a proposition so i tried that. without luck:)
Willy try the one from @jwelter now

I did clear up what devices to track and they are in a group now.
I do now the difference betwween battery entity and one with a battery attribute

edit: i can’t try the one from jwelter cause thats a script pointing to attributes, but for me the ones i want to track is an entity not an attribute, so it wont work…

Im stuck i think

Here’s my batteries.yaml file

sensor:
  - platform: template
    sensors:
      low_batteries:
        friendly_name: Low Battery Devices
        unit_of_measurement: Entities
        icon_template: "{{ 'mdi:checkbox-marked-circle-outline' if states('sensor.low_batteries')|int == 0 else 'mdi:alert-circle-outline' }}"
        value_template: >
            {%- set ns = namespace(counter=0) -%} 
            {%- set threshold = 10 -%}
            {%- for item in expand('group.battery_levels') -%}
            {%- if item.state | int <= threshold -%}
            {%- set ns.counter = ns.counter + 1 -%}
            {%- endif -%}
            {%- endfor -%}
            {{ ns.counter }}
        attribute_templates:
          matched_devices: >
            {%- set threshold = 10 -%}
            {%- for item in expand('group.battery_levels') -%}
            {%- if item.state | int <= threshold -%}
             - {{ item.name }} - {{ item.state }}|
            {%- endif -%}
            {%- endfor -%}
            
# When a battery in the group drops below a threshold count them. 
# It also drops their name and current level in an attribute we use in the notify messages.    

and my automation

- id: Lage batterij waarschuwing
  alias: System - Low Battery Warning
  trigger:
  - entity_id: sensor.low_batteries
    platform: state
    for: 00:01:00
  condition:
  - condition: not
    conditions:
    - condition: state
      entity_id: sensor.low_batteries
      state: '0'
  - condition: template
    value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
  action:
  - data:
      message: '{%- set ns = namespace(message = ''The battery level of the following
        device(s) are low:\n'') -%}  {%- set ns.message = ns.message +  state_attr(''sensor.low_batteries'',''matched_devices'').split(''|'')
        | join(''% \n'')  -%} {{ ns.message }}'
      title: '** Battery Level Warning **'
    service: notify.mobile_app_nokia_7_plus
  mode: single
2 Likes

ah i think i pointed to the wrong group,heres the group

battery_level_monitor:
  name: Battery Level Monitor
  entities:
    - sensor.battery_level
    - sensor.iphone_kim_battery_level
    - sensor.openclose_4_battery_level
    - sensor.openclose_5_battery_level
    - sensor.openclose_6_battery_level
    - sensor.openclose_7_battery_level

Hi, your group is called battery_level_monitor but the sensor references battery_levels (I’ve tidied up an earlier post to make that clearer). These need to be the same.

If you’re only wanting entity batteries what you have there then should work. If you’re wanting to include attribute type ones at the same time as entity ones (and I’m sure they’re better ways to do it!) I would change your sensor configuration to be this one (click on the link to get the whole lot):

Hope that all works for you now

Yeah found that too, the group wasnt right.
WIll see if i get warning now once my battery is drain again… (have to wait :frowning: )

Crap, battery is now 6% still no warning

Did you update the sensor to include the attribute method?

No cause i dont have enitities with the battery % as attribute

Strangle when i came home, and plugged in my phone to power up, i got a messages …

Something is definately wrond lol , battery now is 30% will wait for it to drop again

Shall i post my code?

sorry, I assumed the phone battery was an attribute of the phone entity :frowning:

Yes, you can do (not sure what I can do but will try :slight_smile: ) - send a screen shot of the phone entity as well with its battery attribute to check there’s nothing funky on that!

I got this working, thank you so much. One question though. How would you suppress the message if no devices are below 20%?

It should be caught by the condition, if there are zero devices under your threshold then the automation will stop:

I see. I have some issue with the trigger not firing, apparently. Running the condition and action in the Developer Tools gets “True” and several results. However, it is never firing.

Here is what I have so far, in the automation.yaml

- id: '1601512052928'
  alias: Alert - Low Batteries
  description: ''
  trigger:
  - at: '13:50:00'
    platform: time
  condition:
  - condition: template
    value_template: '"{%- set ns = namespace(message = false) -%} {%- set threshold
      = 80 -%}{%- for item in states.sensor -%}{%- if "battery" in item.name | lower
      and item.state | int < threshold and item.state|int != 0 -%}{%- set ns.message
      = (true) -%}{%- endif -%} {%- endfor -%} {{ ns.message}}"'
  action:
  - data:
      message: '"{%- set ns = namespace(message = "The battery level of the following
        device(s) is low:\n") -%} {%- set threshold = 80 -%} {%- for item in states.sensor
        -%} {%- if "battery" in item.name | lower and item.state | int < threshold
        and item.state|int != 0 -%} {%- set ns.message = ns.message + " - " + item.name
        + " (" + item.state + ")\n" -%} {%- endif -%} {%- endfor -%} {{ ns.message
        }}"'
      title: Low Battery Alert
    service: notify.mobile_app_moto_g_7
  mode: single

I wrote a test automation that turns on a single light at 1:50pm and it worked fine, so I’m not sure what is going on this with this one. Ideas?

1 Like

Try removing the double quotes around the value_template condition:

  condition:
  - condition: template
    value_template: '{%- set ns = namespace(message = false) -%} {%- set threshold
      = 80 -%}{%- for item in states.sensor -%}{%- if "battery" in item.name | lower
      and item.state | int < threshold and item.state|int != 0 -%}{%- set ns.message
      = (true) -%}{%- endif -%} {%- endfor -%} {{ ns.message}}'
2 Likes

OMG, I’m a complete idiot. I’ll never get used to how sensitive HA is to formatting. Thank you, worked immediatly!

Worked for me, Great! Thanks

1 Like

(Post edited…)
I’ve realised that this thread is about Notification on Low Battery Levels.
I was looking for a method to identify low battery levels and (as I should have realised) there are plenty of methods.

In case another newcomer stumbles upon this, here is a very simple solution that even I could implement:

  1. install the Auto-Entities plugin: 🔹 Auto-entities - Automatically fill cards with entities
  2. create a card with this code:
type: custom:auto-entities
card:
  show_header_toggle: false
  title: Battery low
  type: entities
filter:
  include:
    - attributes:
        device_class: battery
      state: <= 60
  exclude:
    - name: /[Ll]ow/
    - name: /[Ss]tate/
sort:
  method: state
  numeric: true

and voila:
HA battery level card

(My original post was:
Why on earth isn’t there an integration available that will monitor battery levels?
Doesn’t everyone need this etc etc
just another naive newbie post :frowning_face: )

1 Like