Howto create battery alert without creating a template for every device

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.

Well, that definitely would remove them :slight_smile:. Can you show me a screenshot of sensor.illumination_158b0001e67306 and the battery sensor from the /dev/state page?

Could it be something to do with the friendly name?

After updating to 0.69.0 I received a ton of MQTT errors relating to “device_class” and all the battery entities are now missing.

Error below:

2018-05-11 19:04:49 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up platform mqtt
Traceback (most recent call last):
  File "/srv/homeassistant/homeassistant_venv/lib/python3.5/site-packages/homeassistant/helpers/entity_platform.py", line 129, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT, loop=hass.loop)
  File "/usr/lib/python3.5/asyncio/tasks.py", line 400, in wait_for
    return fut.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.5/site-packages/homeassistant/components/sensor/mqtt.py", line 57, in async_setup_platform
    config = PLATFORM_SCHEMA(discovery_info)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.5/site-packages/voluptuous/schema_builder.py", line 267, in __call__
    return self._compiled([], data)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.5/site-packages/voluptuous/schema_builder.py", line 587, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.5/site-packages/voluptuous/schema_builder.py", line 425, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: value is not allowed for dictionary value @ data['device_class']

owntracks_batt
diana_batt
sun_batt

Always owntracks battery show the same.

The name shouldn’t matter. I’ll try to repro on my system.

There is a post above about that issue.

Can you also provide screenshot where I can see the attributes for sun_owntracks and diana_owntracks entities?

Thanks. Let me know if you want me to try something differently or test anything.