/ 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 %}'

and if you make up an nice lovelace view, you can create an nice view:

You can create this with the custom component: Bar Card

The following LoveLace code is used:


  - url: /community_plugin/bar-card/bar-card.js
    type: js
-----
      - type: custom:bar-card
        attribute: battery_level
        columns: 1
        severity:
          - value: 15
            color: '#bf4040'
          - value: 40
            color: '#f39c12'
          - value: 50
            color: '#FFD700'
          - value: 100
            color: '#40bf40'
        title_position: left
        unit_of_measurement: '%'
        width: 42%
        height: 20px
        entities:
          - binary_sensor.door_window_sensor_158d0001a1ecd5 #voordeur
          - binary_sensor.door_window_sensor_158d0001dbcc33 #achterdeur
          - binary_sensor.door_window_sensor_158d00016d1372 #schuur
          - binary_sensor.door_window_sensor_158d0001b7baa0 #schutting_voor
          - binary_sensor.door_window_sensor_158d0001b91ae0 #schutting_achter
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>
*
*

eight + 15 =