Howto create battery alert without creating a template for every device

Awesome work! But I have some questions/issues:

  • Is it correct, that the package should create new sensors for the battery levels? Because there are no new sensors available on the state page
  • There are no alerts shown for any sensor with a battery_level attribute unless I put battery in the friendly name of the sensor. But then the script assumes that the main value of the sensor (e.g the temperature) is the battery level, right? So is the issue, that no new sensors are created and therefore no sensors with battery in the name are found? How can I fix that?

I’ve been using this for a while and at the weekend I had 2 Fibaro sensors go from 100% to not working, battery’s were totally flat. Plus I have another that always say 0% even with a new battery.

I just find their reporting far too flaky to be of any use for battery levels.

Is the battery_alert.yaml file stored in the folder named packages?

It will create a new sensor for entities that have attributes named battery_level, battery, and battery numeric. If the entity has battery in it’s name, it will not create a new sensor from it.

Correct, the alert will be for the battery sensor which are automatically created for entities that have battery attributes.

It might be that you don’t have mqtt discovery configured (MQTT - Home Assistant)?

If you want to ignore those, you can use customize to either disable alert on the battery sensor or just disable creating the sensor.

To disable alerts for a single entity:

homeassistant:
  customize:
    sensor.sensor_name_to_ignore_battery:
      battery_alert_disabled: true

To disable creating the batttery sensor:

homeassistant:
  customize:
   sensor.sensor_with_battery_attibute:
     battery_sensor_creation_disabled: true

Is there any way to store the min & max alert threshold value thats set from the input_number persistent in mqtt, to not have the value reset to initial value after reboot?

Make sure you have the recorder enabled and is set to save input_number entities.

Example:

# Log data updates to SQL Database
recorder:
   purge_interval: 2
   purge_keep_days: 14
   db_url: mysql://usrname:password@IP:PORT/DatabaseName
   include:
     domains:
     - sensor
     - input_boolean
     - input_select
     - input_number
     ...
2 Likes

With the release of Home Assistant 0.66, it’s now possible to set the icon for a sensor via MQTT Discovery. I’ve updated the package so it sets the icon to a battery icon. I was hoping to make the icon change based on battery level, but Home Assistant only sets the icon when the sensor is initially discovered and on startup, which doesn’t make that feasible yet.

Once you update the package on your system, the icons won’t change immediately. There needs to be a state change or poll for each device first, then Home Assistant will need to be restarted. After that’s done you should now have battery icons.

The latest code is available on GitHub…

7 Likes

I’ve got some good news and some bad news

First with the bad news. With the upcoming 0.69 release of Home Assistant, there is a breaking change where this package will cause problems with MQTT component in Home Assistant.

Now for the good news. To prevent an issue with the upgrade, you must update the package to the latest version then wait long enough for every battery device to report it’s latest battery level before you upgrade to Home Assistant 0.69**.

For some even better news, with 0.69, it’s finally possible to dynamically set the icon based on battery levels. The latest version of the package enables that feature so once you upgrade, you’ll have dynamic battery icons.

Lastly, for anyone that’s too impatient to wait for the package to fix the issue before upgrading. You accelerate things by installing the new package, restarting Home Assistant, uninstalling and reinstalling the MQTT addon (to purge retained data), then proceed to upgrade Home Assistant. Your battery sensors will automatically get recreated as they report in.

3 Likes

Thank you very much. Mine keep reporting the old memories of battery.
example.
Yesterday my battery is 44% report.
Today my actual battery is 90% but still keep reporting that it is 44%

Can you paste a screenshot of the /dev-state page showing the attributes of the sensor that’s having issues as well as the battery sensor that’s related to it?

I will capture the picture this evening after work and paste here. Thank you very much for your help.

I am trying to import your package and I get this error checking configuration:

Invalid config for [automation]: required key not provided @ data['action']. Got None
required key not provided @ data['trigger']. Got None. (See ?, line ?). Please check the docs at https://home-assistant.io/components/automation/

I am pretty sure that I have copied correctly the yaml file from your repository.

If you use the raw link (https://raw.githubusercontent.com/notoriousbdg/Home-AssistantConfig/master/packages/battery_alert.yaml) there are less chances for copy errors.

Used raw for copying… I will check if I have something else that interferes with this import.

Ok. I’d need to see more of the error to help identify the issue. Are you using this as a package or are you using it in another way?

Ok, I have found an error in another automation.
I am using it as a package and it works flawlessly right now
Thank you for the quick reply!

1 Like

Is there any way of grouping the sensors it creates using a wildcard etc?

Great work by the way, been looking to do this for a long time but it’s always been low on priorities for me but this is awesome! Thanks for sharing!!

1 Like

Thanks for the kind words. I’m glad that it’s helpful.

I don’t know of a way to make the group membership dynamic, but there is a way to make creating the group easier. Navigate to the /dev-template page then paste the template code below into the editor section. The right hand side will have the yaml needed to create a group that contains all of your battery sensors, which you can just copy/paste into your config.

battery_status:
  name: Battery Status
  icon: mdi:battery-charging
  entities:
{%- for item in states.sensor if (
  "battery" in item.name | lower
  and not "voltage" in item.name | lower
  and not "runtime" in item.name | lower
  and not "setpoint" in item.name | lower
  and not "charge" in item.name | lower
) %}
    - {{ item.entity_id }}
{%- endfor -%}

Perfect! Thank you.

I had a bunch of sensors I wanted to exclude, so I stopped Home Assistant, flushed out my MQTT broker and added the following to customize:

sensor.illumination_158b0001e67306:
  friendly_name: Office LUX
  battery_sensor_creation_disabled: true

But the sensor has been recreated. Am I missing something here?

That’s all you should need to do. Did you clear both the config and state topics? They should be homeassistant/sensor/illumination_158b0001e67306_battery/state and homeassistant/sensor/illumination_158b0001e67306_battery/config for your sensor with entity_id of sensor.illumination_158b0001e67306. You’ll have to restart Home Assistant after clearing those topics to complete the removal.

I did indeed. I actually deleted the entire mosquito database as there was a lot of old historical junk I there from testing etc but the sensors still created. I’ll try it again maybe I missed something.