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

1 Like

?? 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?

Hey there, I’ve been battling with this for a while now and struggling to get the battery levels showing in HA, I can see the events in the developer tools when I connect/disconnect the device but no data is being provided about the battery level, can I ask for a 2nd pair of eyes to check over this please?

# Bluetooth battery levels
template:
  - trigger:
      - platform: "event"
        event_type: "android.intent_received"
        event_data:
          android.bluetooth.device.extra.DEVICE: XX:XX:XX:XX:3A:DE
          intent: "android.bluetooth.device.action.BATTERY_LEVEL_CHANGED"
        variables:
          battery_level: "{{ trigger.event.data['android.bluetooth.device.extra.BATTERY_LEVEL'] | int }}"
  - sensor:
      - name: "Jabra Earbuds Battery Level"
        unique_id: "3e23961f-e6e5-4cfd-ad86-8293f7cafb84"
        device_class: "battery"
        unit_of_measurement: "%"
        state_class: "measurement"
        state: "{{ battery_level }}"
        availability: "{{ battery_level >= 0 }}"
        
      - name: "Jabra Earbuds Battery Level"
        unique_id: "3e23961f-e6e5-4cfd-ad86-8293f7cafb84"
        device_class: "battery"
        unit_of_measurement: "%"
        state_class: "measurement"
        state: "{{ battery_level if battery_level >= 0 else states(this.entity_id) }}"

Resolved, I realise my formatting was off. Great thread and have enjoyed working on this, thanks folks!