Howto create battery alert without creating a template for every device

Aha…got it! Thanks!

I watch this thread and it amazed me how difficult people make battery management out to be. The thought of pumping this all via MQTT just feels so inefficient and dirty.

I have the below script; which I use time_pattern to run every couple of hours and a 2nd trigger if the threshold slider is changed that handles it all. All you need is the input_slider to set the threshold; or you can hard code it. No pre-defined groups and such.

########################################
battery_check:
  alias: Battery battery_check

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

I agree that monitoring batteries with Home Assistant is way more difficult and complex than it should be. The issue is that each component has their own unique way of reporting battery levels. There are no standards and no consistency. Your example only handles zwave, while this package handles all variations that I’ve encountered and have been reported in this thread by others.

True, but if you change the states.zwave to states.sensor this will parse all sensors; I just limited it to save a couple CPU cycles.

Yes, that’ll handle a different format, but there a many more variations that it would miss, including zwave devices.

If you use the custom card “group-card”

You only need this code for lovelace.

title: Battery
icon: mdi:battery
cards:
  - type: custom:group-card
    card:
      type: entities
      title: Battery Status
      show_header_toggle: false
    group: group.battery_status
  - type: custom:group-card
    card:
      type: entities
      title: Battery Alert
      show_header_toggle: false
    group: group.battery_alert

And then you dont have to edit the code if you have more entities.

5 Likes

That looks like a really nice simple solution.

Just perfect!

I’m hoping someone can point me in the right direction on the following. For some reason I cannot get the notifier working for my iOS group that works for all other automations tied to it.

The notify group is set correctly under the Notifier name input field but I only receive updates for low battery alerts via the native web notifications.

Perhaps you’re using a notifier component that needs a special format. Which notifier component are you using? Do you have an example of the format needed?

The notifier is setup to my Apple iPhone devices which do not require any special format for the alert content.

Which notifier component are you using? Do you see any errors in the log when the automation fires?

It looks like it is working now. I adjusted the max alert threshold and it now is sending notifications to my iphones.

I’m not sure why changing the threshold fixed the issue.

Anyway, I appreciate the battery alert piece and will definitely be using it going forward.

Regards

1 Like

Edit: never mind the below statement. After upgrading, everything is working as before. The Check my Configuration add-on must have triggered on something else. Sorry for causing alarm without proper cause.

Could it be that the package is incompatible with HA 0.87 ? I ran Check My Configuration before updating from 0.86.4 to 0.87, but it says something is wrong here. Maybe this has something to do with this breaking change (but I might be jumping to conclusions :-)):

“MQTT platforms will now flag mistyped configs from configuration.yaml correctly as invalid, instead of just ignoring them. (@emontnemery - #20562) (mqtt docs ) (breaking change)”

alias: battery_notification_slack
      hide_entity: False
      initial_state: True
      trigger: ?
        - platform: time
          at: 10:00:00
        - platform: time
          at: 18:00:00
        - platform: state
          entity_id: ?
            - input_number.battery_alert_threshold_min
            - input_number.battery_alert_threshold_max
    - action: ?
        - data_template: ?
            payload: <homeassistant.helpers.template.Template object at 0x7fbeb52b4668>
            retain: <homeassistant.helpers.template.Template object at 0x7fbeb52b44a8>
            topic: <homeassistant.helpers.template.Template object at 0x7fbeb52b4ac8>
          service: mqtt.publish
        - data_template: ?
            payload: <homeassistant.helpers.template.Template object at 0x7fbeb52acc50>
            retain: <homeassistant.helpers.template.Template object at 0x7fbeb52ac5f8>
            topic: <homeassistant.helpers.template.Template object at 0x7fbeb52ac160>
          service: mqtt.publish
        - data_template: ?
            payload: <homeassistant.helpers.template.Template object at 0x7fbeb529b7f0>
            retain: <homeassistant.helpers.template.Template object at 0x7fbeb529b470>
            topic: <homeassistant.helpers.template.Template object at 0x7fbeb529bd68>
          service: mqtt.publish
      alias: battery_sensor_from_attributes
      condition: ?
        - condition: template
          value_template: <homeassistant.helpers.template.Template object at 0x7fbeb52ac860>
        - condition: template
          value_template: <homeassistant.helpers.template.Template object at 0x7fbeb52acc88>
        - condition: template
          value_template: <homeassistant.helpers.template.Template object at 0x7fbeb52ac4a8>
        - condition: template
          value_template: <homeassistant.helpers.template.Template object at 0x7fbeb5302da0>
        - condition: template
          value_template: <homeassistant.helpers.template.Template object at 0x7fbeb52f0f98>
        - condition: template
          value_template: <homeassistant.helpers.template.Template object at 0x7fbeb529b358>
        - condition: template
          value_template: <homeassistant.helpers.template.Template object at 0x7fbeb529b828>
        - condition: template
          value_template: <homeassistant.helpers.template.Template object at 0x7fbeb528d860>
        - condition: or
          conditions: ?
            - condition: template
              value_template: <homeassistant.helpers.template.Template object at 0x7fbeb529b278>
            - condition: template
              value_template: <homeassistant.helpers.template.Template object at 0x7fbeb52acd30>
            - condition: template
              value_template: <homeassistant.helpers.template.Template object at 0x7fbeb52b4438>
      hide_entity: False
      initial_state: True
      trigger: ?
        - platform: event
          event_type: state_changed
    - action: ?
        - data_template: ?
            entities: <homeassistant.helpers.template.Template object at 0x7fbeb529c6a0>
            object_id: <homeassistant.helpers.template.Template object at 0x7fbeb528d390>
          service: group.set
      alias: update_battery_status_group_members
      hide_entity: False
      initial_state: True
      trigger: ?
        - platform: homeassistant
          event: start
        - platform: time_pattern
          minutes: /5
          seconds: 0
    - action: ?
        - data_template: ?
            retain: <homeassistant.helpers.template.Template object at 0x7fbeb52b4cf8>
            topic: <homeassistant.helpers.template.Template object at 0x7fbeb52b4f60>
          service: mqtt.publish
        - data_template: ?
            retain: <homeassistant.helpers.template.Template object at 0x7fbeb52b4cc0>
            topic: <homeassistant.helpers.template.Template object at 0x7fbeb52b4048>
          service: mqtt.publish
        - data_template: ?
            retain: <homeassistant.helpers.template.Template object at 0x7fbeb52b4dd8>
            topic: <homeassistant.helpers.template.Template object at 0x7fbeb52b4b38>
          service: mqtt.publish
        - data_template: ?
            retain: <homeassistant.helpers.template.Template object at 0x7fbeb52b4da0>
            topic: <homeassistant.helpers.template.Template object at 0x7fbeb533c240>
          service: mqtt.publish
      alias: delete_battery_sensor
      condition: ?
        - condition: template
          value_template: <homeassistant.helpers.template.Template object at 0x7fbeb52b4710>
      hide_entity: False
      initial_state: True
      trigger: ?
        - platform: state
          entity_id: ?
            - input_text.delete_battery_sensor

Release 1.2.2 is available.

Added

  • Alert if battery state is unavailable

Removed

  • Removed startup trigger from automation.update_battery_status_group_members

Changed

  • Default to notify.notify if input_text.notifier_name doesn’t contain “notify.”

Might be helpful to add a link to the latest release from the original post, as it’s easy to get lost in this huge thread.

I would have long ago if it was possible. That post is not editable anymore.

1 Like

@NotoriousBDG I have deleted this package, deleted ha database, and deleted mqtt database, recreated clean ha docker container, and still I find those .battery sensors…
How to completley remove those?

1 Like

Deleting the package, deleting the MQTT database, then restarting is enough to remove it.

Actualy this works… I just forgot I have 2 mqtt servers (cloud and local). After deleting both db, sensors dissapered.

1 Like