Xiaomi Sensor - Battery level as entity

Hello,

I have a Xiaomi Temperature and Humidity Sensor connected via Zigbee (deCONZ / ConBee II) and I can select the temperature, humidity and air pressure as an entity, but I would also like the battery level as an entity in my dashboard (I am using HA 0.99.3). I can see the battery level as a value underneath every entity, but I cannot select it as its own entity.

Is there a way to display the battery level as an entity?

Thank you!

Anmerkung%202019-09-30%20215901

I think you need to create a sensor for it. That’s what I’ve done for my zwave door sensors:

  - platform: template
    sensors:
      battery_entrance_hallway_motion:
        friendly_name: 'Entrance Hallway Motion Battery Level'
        unit_of_measurement: '%'
        value_template: "{{ states.binary_sensor.entrance_hallway_motion.attributes.battery_level}}"
        icon_template: '{% if states.binary_sensor.entrance_hallway_motion.attributes.battery_level == 100 %}
                           mdi:battery
                        {% elif states.binary_sensor.entrance_hallway_motion.attributes.battery_level >= 90 %}
                           mdi:battery-90
                        {% elif states.binary_sensor.entrance_hallway_motion.attributes.battery_level >= 80 %}
                           mdi:battery-80
                        {% elif states.binary_sensor.entrance_hallway_motion.attributes.battery_level >= 70 %}
                           mdi:battery-70
                        {% elif states.binary_sensor.entrance_hallway_motion.attributes.battery_level >= 60 %}
                           mdi:battery-60
                        {% elif states.binary_sensor.entrance_hallway_motion.attributes.battery_level >= 50 %}
                           mdi:battery-50
                        {% elif states.binary_sensor.entrance_hallway_motion.attributes.battery_level >= 40 %}
                           mdi:battery-40
                        {% elif states.binary_sensor.entrance_hallway_motion.attributes.battery_level >= 30 %}
                           mdi:battery-30
                        {% elif states.binary_sensor.entrance_hallway_motion.attributes.battery_level >= 20 %}
                           mdi:battery-20
                        {% elif states.binary_sensor.entrance_hallway_motion.attributes.battery_level >= 10 %}
                           mdi:battery-10
                        {% else %}
                           battery-outline
                        {% endif %}'

The template gives you a varying battery symbol based on the battery level.

Hope that helps.

A template sensor is one solution as Philip suggested, but you can also use this custom card to display the battery levels in your frontend without creating template sensors for each one.

Here’s how I have that card set up to show multiple battery levels:

entities:
  - critical: 15
    entity: binary_sensor.front_closet
    type: 'custom:battery-entity'
    warning: 30
  - critical: 15
    entity: zha.lumi_weather_1
    type: 'custom:battery-entity'
    warning: 30
  - critical: 15
    entity: zha.lumi_weather_2
    type: 'custom:battery-entity'
    warning: 30
  - critical: 15
    entity: zha.water_sensor_1
    type: 'custom:battery-entity'
    warning: 30
  - critical: 15
    entity: zha.water_sensor_2
    type: 'custom:battery-entity'
    warning: 30
  - critical: 15
    entity: zha.button_1
    type: 'custom:battery-entity'
    warning: 30
  - critical: 15
    entity: binary_sensor.motion_sensor_motion
    name: Hue motion sensor
    type: 'custom:battery-entity'
    warning: 30
show_header_toggle: false
title: Other sensor battery status
type: entities
1 Like

Thank you both, I think I would prefer Philips option, but it’s not working for me… I am using this tempalte:

  - platform: template
    sensors:
      battery_sensor1:
        friendly_name: 'Sensor 1 Battery-Level'
        unit_of_measurement: '%'
        value_template: "{{ states.sensor.sensor1.battery_level}}"

This is my sensor:

Unbenannt

Any idea what’s wrong? I think I am using the battery_level not correctly…
I don’t need any battery mdis, I just want to see the percentage of the battery level :wink:

Thank you!

Best regards

You need this:

  - platform: template
    sensors:
      battery_sensor1:
        friendly_name: 'Sensor 1 Battery-Level'
        unit_of_measurement: '%'
        value_template: "{{ state_attr('sensor.sensor1', 'battery_level') }}"

It needs to be states.sensor.sensor1.attributes.battery_level

Since you’re wanting to extract an attribute called battery_level.

Hope that helps.

To clarify, either what I suggested or @Philip_Embleton suggested will work, but what I provided will prevent errors from appearing in the log upon startup if the sensor isn’t ready yet.

Both variants are working, thank you!! :slight_smile:

If you use device_class: battery then you can eliminate the lengthy icon_template.

Device class

  - platform: template
    sensors:
      battery_entrance_hallway_motion:
        friendly_name: 'Entrance Hallway Motion Battery Level'
        value_template: "{{ state_attr('binary_sensor.entrance_hallway_motion' 'battery_level') }}"
        device_class: battery
1 Like

Thanks for the tips guys.

I´ve just had those sensors like that for ages now but will try the above suggested. It´s a neater solution.

I´ve been cleaning up a load of my coding and automations (managed to get down from 16 automations for motion activated hallway lights down to just 2!).

Yeah, I’m glad I stumbled across the battery entity card because I was tired of making template sensors for each entity. Also, you can change the color of the icons by changing these values in battery-entity.js:

			.good {
				color: var(--label-badge-green);
			}
			.warning {
				color: var(--label-badge-yellow);
			}
			.critical {
				color: var(--label-badge-red);
			}

I changed the green color to the main color of the theme I’m using.

On the original post, you can see that the entity has been updated “7 minutes ago”. Is there a way to display this in an entity card?

Hi there,
i ended up adding some aqara sensors to my HA.
The first multi sensor (temperature, pression and humidity) showd also battery level sensor.
I added a second one, exactly the same as the previous but in the entities i don’t have the sensor related to battery level.
I tried to disconnect the sensor from HA through conbee 2 but the same, it doesn’t show battery level.
Did u ever have this issue?
Any suggestion on how to gain also the battery level?

Thanks!

Hello!

I had the same issue with most of my sensors. The solution was simple: Just wait :wink:
It took them up to 12 - 24 hours, and suddenly, the battery level appeared too.

Maybe this also helps for your sensors

You were right, 22 hours and magically appeared!!!
I had connecting issues because my sensor was far away in a bathroom.
I added an usb extension cable to put conbee in a better position and finally everything worked fine!