Bluetooth Battery Levels (Android)

after 2024.10 all my sensor created with the yaml in this post stopped working even if i still recive the event info under developer options (i think for the new template sensor dimension limitations) any ideas?

It looks like my sensors have also stopped working, but I donā€™t think it is the new limits:

Limit maximum template render output to 256KiB to ensure template renders do not inject unreasonable data into the system and cause a crash.

The new limits limit the output and the output here is only 4 characters maximum.

I will investigate.

iā€™ve found a fix
like this:

android.bluetooth.device.extra.DEVICE: "XX:XX:XX:XX:35:C1"

(where 35:C1 are the last four digit of the device you intend to monitor
instead of the full mac address)
i donā€™t understand why it works but it does

?? what?
Are you saying you replaced the mac address with X? or what?

i replace the all mac with X minus the last four digits
iā€™ve tested this with two of my bluetooth headphones and it works like before

Ok so you mask a device that has about 10 m range.
Sureā€¦ But what did you change then? Didnā€™t you have the last four characters before or what?

no i used the X in the template not because i have posted it here

this is the full working template copied from my config as is with no mod (yours might need some mod if you donā€™t use packages in your config like i do)

trigger: 
  - platform: event
    event_type: android.intent_received
    event_data:
      android.bluetooth.device.extra.DEVICE: "XX:XX:XX:XX:B7:42"
      intent: android.bluetooth.device.action.BATTERY_LEVEL_CHANGED
    variables:
      battery_level: "{{ trigger.event.data['android.bluetooth.device.extra.BATTERY_LEVEL'] | int }}"
sensor:
  - name: "Fairbuds XL Battery Level"
    unique_id: "0a7476cc-d6c1-40ba-8ae1-606518cfbxlbl"
    device_class: battery
    state_class: measurement
    unit_of_measurement: "%"
    state: "{{ battery_level }}"
    availability: "{{ battery_level >= 0 }}"

  - name: "Fairbuds XL Battery Level (Last)"
    unique_id: "0a7476cc-d6c1-40ba-8ae1-60651fbxlbll"
    device_class: battery
    state_class: measurement
    unit_of_measurement: "%"
    state: "{{ battery_level if battery_level >= 0 else states(this.entity_id) }}"

Hmmā€¦

I canā€™t really see any difference out of significance from my config.
You use a variable but thatā€™s about it.

  - trigger:
      - platform: "event"
        event_type: "android.intent_received"
        event_data:
          android.bluetooth.device.extra.DEVICE: "41:42:94:CC:13:23"
          intent: "android.bluetooth.device.action.BATTERY_LEVEL_CHANGED"
    sensor:
      - name: "Urbanista Battery Level"
        unique_id: "Urbanista battery"
        device_class: "battery"
        unit_of_measurement: "%"
        state_class: "measurement"
        #availability: "{{ trigger.event['data']['android.bluetooth.device.extra.BATTERY_LEVEL'] != '-1' }}"
        state: >- 
                {%- if trigger.event['data']['android.bluetooth.device.extra.BATTERY_LEVEL'] | int <= 0 -%}
                    {{ states('sensor.urbanista_battery_level') }}
                {%- else -%}
                    {{ trigger.event['data']['android.bluetooth.device.extra.BATTERY_LEVEL'] }}
                {%- endif -%}

this line is the difference

replace it with this

android.bluetooth.device.extra.DEVICE: "XX:XX:XX:XX:13:23"

obviously iā€™m assuming that before the last update your battery sensor was working if this wasnā€™t the case there might be another issue

1 Like

This feels more like a Android change or companion app change.
I have not yet updated to October so I canā€™t verify

Youā€™re probably right since the home assistant and companion app update happened at the same time

Thanks guys!! It has worked for me! After a long time trying to get the battery for my Amazfit watch from my Home Assistant!! Now the problem I am facing is that my wife and my son have the Xiaomi Miband 7 and with these devices it is impossible for me. I have observed that the difference between their bracelets and my watch is that their devices are connected, not paired, and even though I tried to bond with nrf connect, I have not been able to do itā€¦ any clues?