Template Sensor with Dynamic Battery Icon

Thanks @NotoriousBDG, but it seems this is because the zwave devices aren’t “ready” at startup and only a few seconds later.
I found it worked by replacing the “value_template” you had above with the “value_template” as seen here.
No more error :slight_smile:

1 Like

Awesome. I’ll update my templates with that fix too.

1 Like

Tried this:

value_template: '{% if states.zwave.vannsensor_oppvaskmaskin %}
  {{ states.zwave.vannsensor_oppvaskmaskin.attributes.battery_level }}
{% else %}
  n/a
{% endif %}'
icon_template: >-
  {% set battery_level = (states.zwave.vannsensor_oppvaskmaskin.attributes.battery_level)|int('unknown') %}
  {% set battery_round = (battery_level|int / 10)|int * 10 %}
  {% if battery_level == 'unknown' %}
    mdi:battery-unknown
  {% else %}
    {% if battery_round >= 100 %}
      mdi:battery
    {% elif battery_round > 0 %}
      mdi:battery-{{ battery_round }}
    {% else %}
      mdi:battery-alert
    {% endif %}
  {% endif %}

Still got this error:
Could not render icon template Vanndetektor Oppvaskmaskin Batteri: UndefinedError: ‘mappingproxy object’ has no attribute ‘battery_level’

Do you not get this error?

Your first line below icon_template is currently this:

{% set battery_level = (states.zwave.vannsensor_oppvaskmaskin.attributes.battery_level)|int(‘unknown’) %}

It should rather be this:

{% set battery_level = states(‘name of your sensor here’)|int(‘unknown’) %}

Sorry, not sure what you are calling your sensor as you haven’t included that part of the code. In @NotoriousBDG’s example, it would be sensor.mailbox_battery

Hope that helps?

Yes! How could I miss that. Thank you, no more errors! :smile:

Awesome!! Enjoy :slight_smile:

Have a problem with my smokesensor templates. Get this error:

Could not render template Branndetektor Kjøkken, the state is unknown.

. Do you know if its possible to get this to work?

  branndetektor_kjokken_template:
    friendly_name: 'Branndetektor Kjøkken'
    value_template: >-
     {%- if is_state("sensor.brannvarsler_kjokken_smoke", "2") -%}
      Brann
     {%- elif is_state("sensor.brannvarsler_kjokken_smoke", "3") -%}
      Brann Test
     {%- elif float(states.sensor.timestamp_now.state | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated)) | float > (5000) -%}
      Error
     {%- else -%}
      Ok
     {%- endif -%}

It is this line that is causing the problem:

 {%- elif float(states.sensor.timestamp_now.state | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated)) | float > (5000) -%}
  Error

But really want that function, but meaby possible to fix?

Did you ever get this to work? I’m seeing similar errors, also with a Z-wave device. I’ve got the following in my sensors.yaml:

- platform: template
  sensors:
    battery_entry_sensor:
      value_template: '{% if states.zwave.vision_unknown_type0004_id0000_2 is not none %}
        {{ states.zwave.vision_unknown_type0004_id0000_2.attributes.battery_level }}
        {% else %}
          n/a
        {% endif %}'
      unit_of_measurement: '%'
      friendly_name: 'Door Lock'
      icon_template: >
        {% set battery = ['-outline','-20','-20','-30','-40','-40','-60','-70','-80','-90',''] %}
        {% set charge = (states('battery_entry_sensor') | float / 10) | round %}
        mdi:battery-{{ battery[charge] }}

for battery, why don’t you just use device_class: battery instead of the icon_template:

- platform: template
  sensors:
    corridor_motion_sensor_battery:
      friendly_name: 'Corridor battery'
      unit_of_measurement: '%'
      value_template: >
        {{ states.sensor.corridor_motion_sensor.attributes.battery }}
      device_class: battery

#      icon_template: >
#        {% set battery_level = states.sensor.corridor_motion_sensor.attributes.battery|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 %}
6 Likes

That was too easy… thank you!

1 Like

Thanks for suggestion.

Hi, have tried several combinations of the suggestions here, but cant seem to get it working. The sensor always shows the battery-alert icon for all battery levels:

image

Running Home Assistant v0.92.2. Hope someone can find the error please?

configuration.yaml

image

using the device_class is the way to go, why did you take that out?

try

{{ state_attr('device_tracker.life360_callum','battery')|round }}

Blockquote

Hi Mariusthvdb, thanks for your help.

Sorry, but I am not sure where I would put the line you suggested and which line(s) it replaces. Here is my original config in text form (hopefully easier to edit):

>  - platform: template
>     sensors:
>       callum_battery:
>         value_template: >-
>           {{ states.device_tracker.life360_callum.attributes.battery }}
>         friendly_name: 'Callum Battery'
>         unit_of_measurement: '%'
>         #device_class: battery
>         icon_template: >-
>           {% set battery_level = states.sensor.callum_battery|default(0)|int %}
>           {% set battery_round = (battery_level|int / 10)|int * 10 %}
>           {% if battery_level == 'unknown' %}
>             mdi:battery-unknown
>           {% else %}
>             {% if battery_round >= 100 %}
>               mdi:battery
>             {% elif battery_round > 0 %}
>               mdi:battery-{{ battery_round }}
>             {% else %}
>               mdi:battery-alert
>             {% endif %}
>           {% endif %}

its the value_template…

try:

  - platform: template
    sensors:
      callum_battery:
        value_template: >
           {{ state_attr('device_tracker.life360_callum','battery')|round }}
        friendly_name: 'Callum Battery'
        unit_of_measurement: '%'
        device_class: battery
1 Like

Thanks Mariusthvdb thats works great!

Here is my working and easy solution (no need to define icon level and it does it automatically):

  - platform: template
    sensors:
# Battery Templates
      walkway_motion_sensor_batt:
        friendly_name: "Walkway Motion Sensor Battery"
        value_template: '{{ states.zwave.zooz_unknown_type_0001_id_0005.attributes.battery_level}}'
        unit_of_measurement: '%'
        device_class: battery
    
      front_door_sensor_batt:
        friendly_name: "Front Door Sensor Battery"
        value_template: '{{ states.zwave.aeon_labs_zw120_door_window_sensor_gen5.attributes.battery_level}}'
        unit_of_measurement: '%'
        device_class: battery

and it comes up like this:
image

2 Likes

Not sure if the simple solution can show charging, but here’s my sensor template which does.

      person_battery:
        friendly_name: "Battery"
        unit_of_measurement: '%'
        value_template: "{{states.device_tracker.life360_person.attributes.battery }}"
        icon_template: >-
          mdi:battery{% if is_state_attr('device_tracker.life360_person', 'battery_charging', true) %}-charging{% else %}{% endif %}{% if 0<(state_attr('device_tracker.life360_person', 'battery') | float / 10 ) | round(0) * 10 < 100%}-{{ (state_attr('device_tracker.life360_person', 'battery') | float / 10 ) | round(0) * 10 }}{% else %}{% if (state_attr('device_tracker.life360_person', 'battery') | float / 10 ) | round(0) * 10 == 0%}-outline{%else%}{% if is_state_attr('device_tracker.life360_person', 'battery_charging', true) %}-100{% endif %}{% endif %}{% endif %}

I then use the config-template-card on the frontend to have the color change using this in a picture elements card

            - entity: '${''sensor.person_battery''}'
              style:
                '--paper-item-icon-color': >-
                  ${'hsl(' + (states['sensor.person_battery'].state*1.1
                  )*((states['sensor.person_battery'].state/100)) +
                  ',90%,40%)'}
                

Some examples of how it looks are here.

1 Like

For anyone visiting this post with a recent version of HA, you can now add device_class: battery you will automatically get the dynamic icon!

List of available device classes: Sensor - Home Assistant

1 Like

it has been that way for some time now…Template Sensor with Dynamic Battery Icon - #17 by Mariusthvdb