/ August 19, 2018/ Articles, Home Assistant/ 0 comments

With the following code you can make a custom Battery sensor in HomeAssistant. It will dynamically change the icon based on battery percentage.

battery_xiaomi_deurbel:
        friendly_name: 'Deurbel' 
        unit_of_measurement: '%'
        value_template: >
            {%- if states.binary_sensor.door_window_sensor_158d00016c3e09.attributes.battery_level %}
                {{ states.binary_sensor.door_window_sensor_158d00016c3e09.attributes.battery_level|round }}
            {% else %}
                {{ states.binary_sensor.door_window_sensor_158d00016c3e09.state }}
            {%- endif %}
        icon_template: '{%- if  states.binary_sensor.door_window_sensor_158d00016c3e09.attributes.battery_level <= 5 %}mdi:battery-outline{%- elif states.binary_sensor.door_window_sensor_158d00016c3e09.attributes.battery_level >= 95 %}mdi:battery{% else %}mdi:battery-{{(states.binary_sensor.door_window_sensor_158d00016c3e09.attributes.battery_level|float / 10)|round*10}}{%- endif %}'
Share this Post

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
*
*

ten − three =