I have seen a few posts about getting the battery level of Bluetooth device in homeassistant but none of them actually fully explain and none of them actually worked for me at first.
First open the homeassistant app on your phone and navigate to Settings > Companion App > Manage Sensors > Last Update Trigger.
Ensure that it is enabled.
Click Add New Intent at the bottom of the screen and then on Intent 1.
Enter exactly android.bluetooth.device.action.BATTERY_LEVEL_CHANGED.
IMPORTANT: Restart the app, it is important that you perform a forced restart (there will be a button in your phones settings on the homeassistant app info page that will say something along the lines of Force stop).
Swap <mac_address> and <device_name> for the mac address and device name of your bluetooth device.
Swap for a unique id (in the Studio Code Server you can do this from the right click menu).
Either restart HomeAssistant or open the developer tools page and click reload template entities.
You did it!
Troubleshooting:
Open the developer tools page and navigate to the events tab, enter android.intent_received into Event to subscribe to, then click START LISTENING and then power off and on your bluetooth device you should see two events pop up one for power off and one for power on.
Check the system logs in settings.
Check that your phone is showing a battery charge for the device in Bluetooth settings.
Had some games with this! First off, how do you find the MAC Address of your Bluetooth Device? Mine’s a Garmin watch, so you need to investigate the menus, it will be device specific for your model, and it won’t be in the obvious first place to look, like Bluetooth or network settings.
When you come to edit the YAML, here’s a hint as to where to find the “Generate UUID” at step 8
Swap for a unique id (in the Studio Code Server you can do this from the right click menu).
Lastly, be dissappointed that your Garmin watch will not report its battery level. There are posts on the Garmin forum from annoyed users about this oversight by Garmin: View battery status of device on Connect App. Boo Garmin!
The good news is, I got my headphones battery level to report just fine. Again you’ll have fun getting the MAC address. Here are two suggestions:
The sensor is created but doesn’t get any battery data. Listening to android.intent_received is totally quiet. I dont see anything in logs and the phone is showing a battery charge.
Have you received any events since setting up the template sensor? If not, the best way to trigger them is to turn the headphones on and off or connect and disconnect them.
To fully achieve this, it is necessary to use an input_number help (so that it persists over ha restarts).
I have an automation that is triggered on changes in the state of the template sensor, checks that the sensor is available and updates the input_number. Then I have a template sensor that copies the value of that input_number to make it show up as a battery.
This is the automation that I use, it will do as many batteries as you like:
alias: Bluetooth Battery Level (Last)
description: ""
trigger:
- platform: state
entity_id:
- [[ list of sensors ]]
not_to:
- unknown
- unavailable
condition: []
action:
- service: input_number.set_value
target:
entity_id: input_number.{{ trigger.entity_id.split('.')[1] }}_last
data:
value: "{{ trigger.to_state.state }}"
mode: single
The input_number and template sensor then need to be setup via the helpers menu.
This is nice work, my one note is that in my experience, the template sensors forget their values when Home-Assistant restarts and input_numbers do not, so while it is more annoying to setup it was necessary in my case. This solution should work apart from when you restart Home-Assistant, thank you for sharing.
There is not a HACS integration for this, as it uses the mobile app and template sensors (which are built in). There is no installation as such, it is just configuration, as is shown in the post above.