Howto create battery alert without creating a template for every device

Anyone else still using this and found the icons have stopped working correctly?
(2021.10.x)

Which icons??

these Howto create battery alert without creating a template for every device - #29 by riemers

Just check that the icons you want to use werenā€™t in the removed list from HA 2021.10

nope - there are no (reported) changes to battery MDI icons, plus there should be a warning logged - there isnā€™t.

AFAIK, the battery icons are chosen by HA, not Battery Alertā€¦

image

Standard MDI icons:

no - this package generated the ICON: customisation itself - Howto create battery alert without creating a template for every device - #162 by NotoriousBDG

anyway - Iā€™ve traced it to the MQTT payload no longer having the ICON: entity:

{
  "name": "Motion - Study Battery",
  "state_topic": "homeassistant/sensor/motion_sensor_158d000221a1ad_battery/state",
  "value_template": "{{ value_json.value | int }}",
  "unit_of_measurement": "%",
  "device_class": "battery",
  "unique_id": "motion_sensor_158d000221a1ad_battery",
  "json_attributes_topic": "homeassistant/sensor/motion_sensor_158d000221a1ad_battery/attributes"
}

(compared to Howto create battery alert without creating a template for every device - #921 by imellor)

which means the battery_sensor_from_attributes automation isnā€™t triggering the icon section the same any more:

          {
            {% if trigger.event.data.new_state.attributes.battery_level is defined -%}
              {%- set attribval = trigger.event.data.new_state.attributes.battery_level -%}
              {%- set attribname = 'battery_level' -%}
            {%- elif trigger.event.data.new_state.attributes.battery is defined -%}
              {%- set attribval = trigger.event.data.new_state.attributes.battery -%}
              {%- set attribname = 'battery' -%}
            {%- elif trigger.event.data.new_state.attributes['Battery numeric'] is defined -%}
              {%- set attribval = (trigger.event.data.new_state.attributes['Battery numeric'] | int + 1) * 10 -%}
              {%- set attribname = 'Battery numeric' -%}
            {% elif trigger.event.data.new_state.attributes.battery_critical is defined -%}
              {%- set attribval = trigger.event.data.new_state.attributes.battery_critical -%}
              {%- set attribname = 'battery_critical' -%}
            {%- endif -%}
            "name": "{{ trigger.event.data.new_state.name }} Battery",
            "state_topic": "homeassistant/sensor/{{ trigger.event.data.entity_id.split('.')[1] }}_battery/state",
            {% if trigger.event.data.new_state.attributes.battery_template is defined -%}
            "value_template": "{{ trigger.event.data.new_state.attributes.battery_template }}",
            "unit_of_measurement": "%",
            "device_class": "battery",
            {% elif trigger.event.data.new_state.attributes.battery_template_string is defined -%}
            "value_template": "{{ trigger.event.data.new_state.attributes.battery_template_string }}",
            "icon": "mdi:battery",
            {% elif trigger.event.data.new_state.attributes.battery_critical is defined -%}
            "value_template": "{{ "{{" }} 'low' if value_json.value else 'full' {{ "}}" }}",
            "icon": "mdi:battery",
            {% else -%}
            "value_template": "{{ "{{" }} value_json.value | int {{ "}}" }}",
            {% if attribval | int == attribval or attribval | float == attribval or attribval | length == attribval | float | string | length or attribval | length == attribval | int | string | length -%}
            "unit_of_measurement": "%",
            "device_class": "battery",
            {%- elif attribval == "High" or attribval == "Full" -%}
            "icon": "mdi:battery",
            {%- elif attribval == "Medium" or attribval == "Med"-%}
            "icon": "mdi:battery-50",
            {%- elif attribval == "Low" -%}
            "icon": "mdi:battery-alert",
            {%- else -%}
            "icon": "mdi:battery-unknown",
            {%- endif %}
            {% endif -%}
            "unique_id": "{{ trigger.event.data.entity_id.split('.')[1] }}_battery",
            "json_attributes_topic": "homeassistant/sensor/{{ trigger.event.data.entity_id.split('.')[1] }}_battery/attributes"
          }

ok - I think Iā€™m wrong. sigh.

as the device_class is battery, @dbrunt is correct and HA is doing the translation. Which isnā€™t working in my case - all the batteries are given the 100% icon.

I have a few battery devices connected via Zigbee2MQTT. Hereā€™s oneā€¦

{"availability":[{"topic":"zigbee2mqtt/bridge/state"}],"device":{"identifiers":["zigbee2mqtt_0x00158d0004869868"],"manufacturer":"Xiaomi","model":"Aqara human body movement and illuminance sensor (RTCGQ11LM)","name":"Living Room Motion","sw_version":"Zigbee2MQTT 1.21.2"},"device_class":"battery","enabled_by_default":true,"json_attributes_topic":"zigbee2mqtt/Living Room Motion","name":"Living Room Motion battery","state_class":"measurement","state_topic":"zigbee2mqtt/Living Room Motion","unique_id":"0x00158d0004869868_battery_zigbee2mqtt","unit_of_measurement":"%","value_template":"{{ value_json.battery }}"}

Now that I am examining my icons more closely, I appear to have the same issueā€¦

image

Orbit BHyve | Life360 | Z-WaveJS
Mobile App | Zigbee | Z-WaveJS
Z-WaveJS | Zigbee | Z-WaveJS
Zigbee | Zigbee | Zigbee

The Zigbee2MQTT devices Z-WaveJS devices appear to be incorrect IMO.

Correct:

battery_level: 7
unit_of_measurement: '%'
friendly_name: Front Timer
icon: mdi:battery-10
device_class: battery
templates:
  icon_color: >
    if (state < 30) return 'red'; if (state < 60) return 'yellow'; return
    'green';
icon_color: red
unit_of_measurement: '%'
friendly_name: Daniel's Cell Phone
icon: mdi:battery-80
device_class: battery
templates:
  icon_color: >
    if (state < 30) return 'red'; if (state < 60) return 'yellow'; return
    'green';
icon_color: green

Incorrect as it is missing icon: mdi:battery-80:

state_class: measurement
battery: 86
humidity: 93.7
linkquality: 24
pressure: 1013
temperature: 7.03
voltage: 2975
unit_of_measurement: '%'
friendly_name: Outside Temperature
device_class: battery
templates:
  icon_color: >
    if (state < 30) return 'red'; if (state < 60) return 'yellow'; return
    'green';
icon_color: green

Okay, maybe what I am seeing here is a front end difference.
This view is using the custom:auto-entities, type: glance card:

This view is custom:battery-state-card:

It could be the absence of the icon: mdi:battery-## attribute as I only have three with that and they display correctly in both views whereas I think the battery-state-card may be compensating for the lack or or overriding the icon: mdi:battery-## attribute:

  • Danielā€™s Cell Phone (Mobile App)
  • Front Timer (BHyve)
  • Rear Timer (BHyve)

Fixed.

image

default_config:

homeassistant:
  customize: !include customize.yaml

  customize_domain:
    switch:
      templates:
        icon_color: >
          if (state == 'on') return 'green';
          return 'red';
        icon: >
          if (state == 'on') return 'mdi:toggle-switch-outline';
          return 'mdi:toggle-switch-off-outline';

    light:
     templates:
        icon: >
          if (state == 'on') return 'mdi:lightbulb';
          return 'mdi:lightbulb-outline';

    lock:
      templates:
        icon_color: >
          if (state == 'locked') return 'green';  return 'red';

    sensor.*_battery:
      templates:
        icon_color: >
          if (state < 30) return 'red';
          if (state < 80) return 'yellow';
          return 'green';
        icon: >
          if (state < 15) return 'mdi:battery-10';
          if (state < 25) return 'mdi:battery-20';
          if (state < 35) return 'mdi:battery-30';
          if (state < 45) return 'mdi:battery-40';
          if (state < 55) return 'mdi:battery-50';
          if (state < 65) return 'mdi:battery-60';
          if (state < 75) return 'mdi:battery-70';
          if (state < 85) return 'mdi:battery-80';
          if (state < 95) return 'mdi:battery-90';
          return 'mdi:battery';

    binary_sensor.*:
      templates:

    binary_sensor.*_motion:
      templates:
        icon: >
          if (state == 'on') return 'mdi:motion-sensor';
          return 'mdi:human-male';
        icon_color: >
          if (state == 'on') return 'yellow';
          return 'grey';
    
    binary_sensor.*_occupancy:
      templates:
        icon: >
          if (state == 'on') return 'mdi:motion-sensor';
          return 'mdi:human-male';
        icon_color: >
          if (state == 'on') return 'yellow';
          return 'grey';

  packages: !include_dir_named packages/
1 Like

OK - so you are fixed via custom-UI rather than fix the bug :slight_smile:

so it is an actual HA bug. ok - I was going nuts trying to work out why if the templating was broken

Yes, I use custom-ui so I fixed it for me for now, via that option. Wil revert once HA is fixed.

Icons are NOT fixed in current HA release 2021.10.6ā€¦

you mean 2021.10.7?

I stand corrected. I was looking at the wrong card, again.
:slightly_frowning_face:

1 Like

Is there a way to use this for batteries that arenā€™t directly monitorable in a device? As in, the periodic replacement of batteries in things like smoke alarms, UPS units that donā€™t have monitoring, or other offline devices that should be checked on the basis of dates?

The lead acid batteries a UPS typically need replacing every 3 years. I have several units spread around the house (for DVRs, PCs, etc). Itā€™d be handy to have a way to see if thereā€™s several with upcoming battery replacement deadlines, so I could perhaps make quantity order and do them all together rather than an endless cascade of yet another one diedā€¦ order another replace, repeatā€¦ A card on my HA dashboard that pops up when one is getting close, and shows the values of others, maybe?

The same sort of thing applies for filters in a furnace air handler, the de/humidifier pads or just periodic cleaning of the condensate pumps.

Simple answer, no. This is meant only for low battery levels that are already reported to HA.
Youā€™d have to devise something else like input helpers that store a date and then automations that trigger on days elepsed since the input date value and alert you plus maybe an input boolean for actual replacement that you could toggle to true which would trigger another automation to reset the date last changed helper to todayā€™s date and reset the boolean helper back to false. Most Smart UPSā€™s have some means of monitoring battery expiration and alerting. There are HA add-ons for some USB based UPSā€™sā€¦

1 Like