Xiaomi sensor battery attribute

Hi,

I’m trying to get the battery attribute as a sensor for my Xiaomi Aqara temperature/humidity sensors. I have tried many permutations with no luck and I have followed what others have done without success. A difference may be that I am using zigbee2MQTT (using the sensors without the Xiaomi gateway).

Here is a few of my attempts that all fail. All return “Unknown” except T2 which returns “0.0”. The attribute for all of them should be “battery” but I was just guessing with the “battery_level” on T2.

- platform: template
  sensors:
    batt_outside:
      friendly_name: Battery Outside
      value_template: >
        {{ states("sensor.0x00158d000272b4f7_temperature.attributes['battery']") }}
      unit_of_measurement: '%'
    batt_t1:
      friendly_name: T1
      value_template: >
        {{ states('sensor.0x00158d000272b4f7_temperature').attributes.battery }}
    batt_t2:
      friendly_name: T2
      value_template: >
        {{ "states.sensor.0x00158d00027263ee_temperature.attributes.battery_level" |float }}
    batt_t3:
      friendly_name: T3
      value_template: >
        {{ states.sensor.temperature_0x00158d000272b4f7.attributes.battery }}

The sensor below works in my configuration fine. I have had some issues with the “0x00” in the sensor name which has caused me some problems in the past.

This sensor works fine for me to give you an idea of what success looks like…

- platform: template
  sensors:
    temp_diff:
      friendly_name: Temp Diff
      value_template: "{{ (states('sensor.0x00158d000272b4f7_temperature') |float - states('sensor.0x00158d00027263ee_temperature') |float) |round(1) }}"
      icon_template: "mdi:thermometer"
      unit_of_measurement: '°C'

And this is what the entity looks like in HASS

Any advice would be much appreciated!

Hi,
Try using ‘{{ states.binary_sensor.motion_sensor_158d000272b4f7.attributes.battery_level }}’ as value_template.

That’s how I set up mine and it’s working fine.

Thanks, tried that plus several variations with no luck.

‘{{ states.sensor.temperature_158d000272b4f7.attributes.battery_level }}’

I’m pretty sure my attribute name is “battery” (which also doesn’t work).

{{ state_attr('sensor.master_bedroom_temperature', 'battery_level') | round(0) }} works for me incase anyone else finds through search

any upates ?