Safeguard your Android's battery with Tasker + Home Assistant
A few months ago, I started using the Accubattery app to keep a closer eye on how I'd been charging my phones. The app has a handy feature that notifies you once the battery level reaches a certain threshold so you can pull the phone off the charger and extend the lithium battery's service life, and it even offers an estimate for what that impact might be. For instance, right now the app indicates that charging my Pixel 5 from 51% to 100% would cause 0.92 wear cycles, while stopping the charge at 80% would impose just 0.17 cycles.
But that depends on me being near my phone and conscious so I can take action when the notification goes off. That's often a big assumption to make - and, frankly, I'm lazy.
I'm fortunately also fairly crafty, so I came up with a way to combine my favorite Android automation app with my chosen home automation platform to take my laziness out of the picture.
- Wemo Mini Smart Plug
- Raspberry Pi 3 with Home Assistant installed
- Home Assistant Plug-In for Tasker
I'm not going to go through how to install Home Assistant on the Pi or how to configure it beyond what's strictly necessary for this particular recipe. The official getting started documentation is a great place to start.
- Plug the Wemo into a wall outlet, and plug a phone charger into the Wemo. Add the Belkin Wemo integration in Home Assistant, and configure the device and entity. I named mine
switchy. Make a note of the Entity ID:
switch.switchy. We'll need that later.
- Either point your phone's browser to your Home Assistant instance's local URL, or use the Home Assistant app to access it. Tap your username at the bottom of the menu and scroll all the way down to the Long-Lived Access Tokens section. Tap to create a new token. It doesn't matter what you name it, but be sure to copy to token data once it is generated since you won't be able to display it again.
- Install the Home Assistant Plug-In for Tasker. Open Tasker, create a new Task called 'ChargeOff', and set the action to
Plugin > Home Assistant Plug-in for Tasker > Call Service. Tap the pencil icon to edit the configuration, and then tap the plus sign to add a new server. Give it whatever name you like, and then enter your Home Assistant's IP address for the Base URL, followed by the port number
8123. For example,
http://192.168.1.99:8123. Paste in the Long-Lived Access Token you generated earlier. Go on and hit the Test Server button to make sure you got it right. It'll wind up looking something like this: For the Service field, you need to tell HA what you want it to do. We want it to turn off a switch so enter
switch.turn_off. We'll use the Service Data field to tell it which switch, in JSON format:
Tap Test Service to make sure it works - and verify that the switch does indeed turn off.
- Hard part is over. Now we just need to set up a profile in Tasker to fire our new task. I named mine 'Charge Limiter'. I started with
State > Power > Battery Leveland set it to trigger between 81-100%., and also added
State > Power > Source: Anyso it will only be active while charging. I also only want this to trigger while my phone is charging at home, so I added
State > Net > Wifi Connectedand then specified my home SSID. Link this profile to the Task you created earlier, and never worry about overcharging your phone again.
You can use a similar Task to turn the switch back on at a set time - or you could configure that automation directly in Home Assistant. I added an action to turn on the switch to my Google Assistant bedtime routine and that works quite well for my needs.