Low battery level detection & notification for all battery sensors

Try this instead;

service: notify.notify
data:
  message: >-
    A battery in the sensor named {{sensors}} is reporting a low state.
  title: Low Battery Warning

Robert Blackwell

@Sbyx:

Is it possible to have an include option instead of excluding battery level sensors?

Maybe as new blueprint if too complicated to combine both (include & exclude) in one blueprint?

I think this is a very valid use-case. E. g. to monitor a specific set/group of devices.

1 Like

Is there any way to have multiple thresholds in one automation instead of creating multiple automations?

Do this blueprint only check low battery at a certain time? Because I cant get it to work

It triggers at the time and on the day(s) you set in the blueprints configuration. A battery needs to be below the threshold before it sends out a notification.

Hey, I really like this Blueprint so first of all thak you!
Nevertheless I sometimes struggle to identify the device by its name. Would it be possible to expose the area of the device for the notification as well?

1 Like

Hey guys,
I like the idea presented here. However for certain reasons I would rather implement and adapt this functionality in code directly.

Does anyone have this blueprint translated into entities and automation rules? Thanks!

exposing Area of the Sensor would be great addition to this Blueprint

Great Blueprint! Can you tell me what defines the baseline for each battery sensor? As I understand it, it alerts when battery is 20% below the baseline. Thanks!

I am getting constantly the following log n+errors


Blueprint Low battery level detection & notification for all battery sensors generated invalid automation with inputs OrderedDict([('actions', [OrderedDict([('service', 'notify.mobile_app_iphonekhw'), ('data', OrderedDict([('message', '{{s'), ('title', 'Homeassistant ')]))])])]): template value should be a string for dictionary value @ data['action'][0]['default'][0]['data']. Got None

Anyone any idea?


alias: Batterieladung von allen Sensoren etc. prĂŒfen und Pushwarnung ausgeben
description: ""
use_blueprint:
  path: sbyx/low-battery-level-detection-notification-for-all-battery-sensors.yaml
  input:
    exclude:
      entity_id:
        - sensor.pv_batterie_status
    threshold: 20
    day: 1
    actions:
      - service: notify.mobile_app_ipad_khneu
        data:
          message: Batterie prĂŒfen {{sensors}}
          title: "HomeAssistant "
    time: "10:00:00"

@mituns
You have to escape data.message → Batterie prĂŒfen {{sensors}} → "Batterie prĂŒfen {{sensors}}"

This should mask your variable "{{sensors}}"

I have an issue with this blueprint recently. Somehow (i think) one of my sensors doesnt have device class. This conclusion i get after pasting the template in dev tools/templates:
\UndefinedError: ‘homeassistant.util.read_only_dict.ReadOnlyDict object’ has no attribute ‘device_class’

How do i find the culprit here (as as far as i know, this should not happen on entities)?

also noticed suddenly some weird entities having device_class ; battery while they are not battery at all but battery related (eg charging state entity has class battery, which will never have a numerical value)

1 Like

Same issue for me using the Blueprint.

I set the % to 95% and run a test and it worked.

Anyone know which part to delete the space between the level and percentage e.g Test iphone 95% instead of Test iphone 95 %

Thanks

I had this problem with some helper entities that I had added. I had to add them to the exclusion list.

Thank you. Imported the Blueprint, configured it, and it works.

I modified @Sbyx’s code to automatically exclude all mobile phones, using: not in integration_entities(“mobile_app”), so there should be no need to exclude them manually.

blueprint:
  name: Low battery level detection & notification for all battery sensors
  description: Regularly test all sensors with 'battery' device-class for crossing
    a certain battery level threshold and if so execute an action.
  domain: automation
  input:
    threshold:
      name: Battery warning level threshold
      description: Battery sensors below threshold are assumed to be low-battery (as
        well as binary battery sensors with value 'on').
      default: 20
      selector:
        number:
          min: 5.0
          max: 100.0
          unit_of_measurement: '%'
          mode: slider
          step: 5.0
    time:
      name: Time to test on
      description: Test is run at configured time
      default: '10:00:00'
      selector:
        time: {}
    day:
      name: Weekday to test on
      description: 'Test is run at configured time either everyday (0) or on a given
        weekday (1: Monday ... 7: Sunday)'
      default: 0
      selector:
        number:
          min: 0.0
          max: 7.0
          mode: slider
          step: 1.0
    exclude:
      name: Excluded Sensors
      description: Battery sensors (e.g. smartphone) to exclude from detection. Only
        entities are supported, devices must be expanded!
      default:
        entity_id: []
      selector:
        target:
          entity:
            device_class: battery
    actions:
      name: Actions
      description: Notifications or similar to be run. {{sensors}} is replaced with
        the names of sensors being low on battery.
      selector:
        action: {}
  source_url: https://gist.github.com/sbyx/1f6f434f0903b872b84c4302637d0890
variables:
  day: !input day
  threshold: !input threshold
  exclude: !input exclude
  sensors: >
    {% set result = namespace(sensors=[]) %}
    {% for state in states.sensor
        | rejectattr('attributes.device_class', 'undefined')
        | selectattr('attributes.device_class', '==', 'battery') %}
      {% if 0 <= state.state | int(-1) < threshold | int
            and not state.entity_id in exclude.entity_id
            and state.entity_id not in integration_entities("mobile_app") %}
        {% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state ~ '%)'] %}
      {% endif %}
    {% endfor %}
    {% for state in states.binary_sensor
        | rejectattr('attributes.device_class', 'undefined')
        | 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') }}
trigger:
- platform: time
  at: !input time
condition:
- '{{ sensors != '''' and (day | int == 0 or day | int == now().isoweekday()) }}'
action:
- choose: []
  default: !input actions
mode: single

To verify what sensors the blueprint is monitoring visit /developer-tools/template and enter the following code in the template editor:

    {% set threshold = 101 %}
    {% set exclude = namespace(entity_id=[]) %}
    {% set result = namespace(sensors=[]) %}
    {% for state in states.sensor
        | rejectattr('attributes.device_class', 'undefined')
        | selectattr('attributes.device_class', '==', 'battery') %}
      {% if 0 <= state.state | int(-1) < threshold | int
            and not state.entity_id in exclude.entity_id
            and state.entity_id not in integration_entities("mobile_app") %}
        {% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state ~ '%)'] %}
      {% endif %}
    {% endfor %}
    {% for state in states.binary_sensor
        | rejectattr('attributes.device_class', 'undefined')
        | 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') }}
2 Likes

Hello,

I modified @Sbyx code to handled battery sensors “not seen since x days”. To give some context, it works with my sensors created by zigbee2mqtt which creates an attributes “last_seen” on batteries sensor with and epoch time format. I am not sure it is standard in Home assistant, anyways it can be a good start for people who want to modify it.

Configuration in zigbee2mqtt :
image

I added an input last_seen to define a number of days which should trigger an alert (if your sensor as disappeared since more than x day you will be alerted) .

Here is my version :

Enjoy!

3 Likes

Is it me or does this blueprint not work with negative values?

I’ve got an template battery sensor which calculates the percentage left in the battery.
Apperently not the nicest calculation because it shows a negative value.

Its class is defined as battery, and it has worked before when it reaches below threshold and is a postive value

The blueprint works great. Any way you an include battery powered buttons?

The blueprint works very well. Thank you for your help!
How to be informed of a new version?