using the device_class is the way to go, why did you take that out?
try
{{ state_attr('device_tracker.life360_callum','battery')|round }}
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
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:
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.
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
it has been that way for some time now…Template Sensor with Dynamic Battery Icon - #17 by Mariusthvdb