Howto create battery alert without creating a template for every device

will i stil be able to use this template though:

corridor_motion_sensor_battery:
      friendly_name: 'Corridor battery'
      unit_of_measurement: '%'
      value_template: '{{ states.sensor.corridor_motion_sensor.attributes.battery }}'
      icon_template:  "{%- if states.sensor.corridor_motion_sensor_battery.state <= '100' %}mdi:battery
                       {%- elif states.sensor.corridor_motion_sensor_battery.state <= '50' %}mdi:battery-50
                       {%- elif states.sensor.corridor_motion_sensor_battery.state <= '30' %}mdi:battery-alert
                       {%- elif states.sensor.corridor_motion_sensor_battery.state <= '10' %}mdi:battery-outline
                       {%- endif %}"

would you please add this then?
It would prevent at least 75% of the issues above :wink: (trying to define ‘necessary’…)

ok I will. What could be the reason your package only sees my second remote and creates a battery sensor for only that, leaving out the first (which is unnumbered)

04
just to be clear, the first 2 entries here are the 2 devices irl, the bottom _battery is created by the package , obviously based on the second device.

I looked at doing what @firstof9 suggested, but there isn’t an easy way to determine what created the battery sensor. Since this is an edge case, I decided to implement it in a different way. To prevent creating a battery sensor from an entity, you can now use customize to set battery_sensor_creation_disabled to true. You’ll need to clear the retained MQTT topics to delete the duplicate sensor.

homeassistant:
 customize:
   sensor.sensor_with_battery_attibute:
     battery_sensor_creation_disabled: true

The latest version is on GitHub.

Either sensor.tradfri_remote_control has “battery” in its friendly name or there hasn’t been a state change yet to trigger the creation of the battery sensor.

1 Like

@NotoriousBDG

hi,

is there any reason thinkable why this icon_template won’t show its correct icon:

icon_template: >
          {% set battery_level = states.sensor.hue_dimmer_switch_1_battery.state|default(0)|int %}
          {% set battery_round = (battery_level / 10) |int * 10 %}
          {% if battery_round >= 100 %}
            mdi:battery
          {% elif battery_round > 0 %}
            mdi:battery-{{ battery_round }}
          {% else %}
            mdi:battery-alert
          {% endif %} 

19

the template machine shows the correct template result…

i have more template’s that show the correct calculation, but don’t show it in the Frontend.

another try i did was this:

#  icon_template: >
#    {%- if states.sensor.hue_dimmer_switch_1_battery.state <= '100' %}mdi:battery
#                   {%- elif states.sensor.hue_dimmer_switch_1_battery.state <= '50' %}mdi:battery-50
#                   {%- elif states.sensor.hue_dimmer_switch_1_battery.state <= '30' %}mdi:battery-alert
#                   {%- elif states.sensor.hue_dimmer_switch_1_battery.state <= '10' %}mdi:battery-outline
#                   {%- endif %}

Also calculating fine, but showing in the frontend.

This package and icon_templates are mutually exclusive. Either you manually create templates for every battery device or you let this package automatically create them all for you without the fancy icon. The primary goal for this package is to avoid creating templates.

ok thanks very much.

the template tool isn’t really showing the frontend value then, but creates its own environment?

See Template Sensor with Dynamic Battery Icon - #5 by NotoriousBDG if you want to create all the battery templates with dynamic icons. You’ll need to disable the automations in this package that create sensors from attributes if you go that route.

yes, thanks, ive seen that, and use its icon_template.
not really sure what improvement Home Assistant 0.41.0 Update | Battery Values are Gone - #13 by sebk-666 brings though. you where kind enthusiastic about that ;-)? is it it only creates the template if it exists?

Sorry for this dumb question, what is the significance of time trigger? would it only create a notification at the above said time? so if my battery goes down the 40% mark at 16:00, would i come to know at 18:00 (the next time trigger?

Yes, it only sends slack notifications at 10:00 and 18:00. A battery that drops below 40 at 16:00 would cause a notification at 18:00 and every day at 10:00 and 18:00 until that battery level is above 40.

Excuse me, I’m a newbie…
I installed your package in my Hassio following your instructions… but in the dashboard it do not show me any “Battery Alert” section… what is wrong?

I have to add group battery_alert manually in configuration.yaml to see it… is this correct?

Did you add packages: !include_dir_named packages under homeassistant: section of your config? If so, check Home Assistant logs for errors.

I’ve got instructions embedded in the package at https://github.com/notoriousbdg/Home-AssistantConfig/blob/master/packages/battery_alert.yaml if you need them.

1 Like

Yes… I added it…

homeassistant:
      #packages
      packages: !include_dir_named packages

but it it do to show me in dshboard … and I Haven’t error in the home-assistant.log about it

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