I wanted to use Home Assistant and ESPHome to automate periodic charging for a less-used cell phone. When I was working through the hardware, I had thought I would just use a time-based system. “Charge daily for [X] hours” and fine-tune the value of X as I go. This can easily be done, but as I learned more about Home Assistant, I realized an even smarter option is possible: turn off charging once the battery surpasses a threshold.
I had originally dismissed doing any decision based on battery state of charge because I thought I would have to open up the phone and probe battery voltage directly. Or if I want to do it in software, I’d have to write my own Android app to listen for battery status events. But that’s before I installed the Home Assistant mobile app on my Pixel 5a primary phone. When I connected the app to my server, I saw that it reported battery level as one of its entities. Here is a graph for 30 hours:
My normal daily phone usage only occasionally interacted with the app, so most of these reports came while it was running in the background. This is enough to receive updates once every 20-30 minutes. When installed on a little-used phone, however, something inside the Pixel 3a (probably Android’s internal power management algorithms) decided Home Assistant app doesn’t need to run as often. As a result, there were far less frequent reports on battery level. This is within the same 30-hour window as the above graph. (But with slightly different Y-axis because of Home Assistant dashboard scaling.)
While the phone is charging, battery level is reported more frequently, but they can be as much as half an hour apart. It gets worse when the phone is not charging, the middle of this graph has a period where we went almost 14 hours without a single update!
Despite this low frequency, I can write a Home Assistant automation that would still be better than doing it blind on a time-based basis. I wouldn’t be able to stop charging exactly at 80%, but I should still be able to catch it surpassing 80% sometime afterwards and still stop charging before battery is full.
[UPDATE: I’ve found that I could improve update frequency by telling Android 12 that I grant the Home Assistant app more leeway on battery use. In the Android settings menu, under Apps/Home Assistant/Battery the option defaults to “Optimized”. We can change it to “Unrestricted” which has the explanation “Allow battery usage in the background without restrictions. May use more battery.”]
I’m only mildly disappointed the battery level reporting rate is infrequent when running on a less-used Android phone, because it is still good enough for my purpose of keeping the battery in its most effective center band. (Avoiding full charge and also avoiding full discharge.) Besides, these infrequent updates are still more useful than the Home Assistant iOS app, which I installed on my iPad and saw no updates for this entire 30-hour period.
I’ll let my Pixel 3a charging logic run and see how well it works (or not) as I work on the next automation: power for a Raspberry Pi dedicated to NAS replication.