[Google Pixel XL Guides, News, & Discussion] Aaron1017:
Intro:

I wanted some way to limit the charge level on my Pixel XL. I tried the BatteryChargeLimit app, but it never worked properly. Soon realized I could so the same thing as the app using Tasker after looking around in /sys/class/power_supply/battery folder.

This guide should work on all Pixel and Nexus devices. Can be adopted to other devices (See BatteryChargeLimit thread)

Why:

Not charging to 100% should in theory extend battery life. This study is starting to get outdated.

http://batteryuniversity.com/learn/article/how_to_prolong_lithium_based_batteries

Prerequisites:

1. Root Access

2. Tasker

How to:

1. Open Tasker. Add a new profile. Name it whatever you want. Select State -> Power -> Power. Keep "Any" selected as Source.

2. Add Enter Task

​

No Charge (6)
Run Both Together
<Get Charging Source>
A1: Run Shell [ Command:su
chmod 666 /sys/class/power_supply/battery/charging_source
cat /sys/class/power_supply/battery/charging_source Timeout (Seconds):0 Use Root:eek:n Store Output In:%PluggedIn Store Errors In: Store Result In: Continue Task After Error:eek:n ]
A2: Run Shell [ Command:su
chmod 666 /sys/class/power_supply/battery/battery_charging_enabled
echo "0" > /sys/class/power_supply/battery/battery_charging_enabled Timeout (Seconds):0 Use Root:eek:n Store Output In: Store Errors In: Store Result In: Continue Task After Error:eek:n ] If [ %BATT > 79 ]
A3: Wait [ MS:0 Seconds:0 Minutes:1 Hours:0 Days:0 ]
A4: Goto [ Type:Action Number Number:1 Label: ] If [ %PluggedIn neq 0 ]

3. Add Exit Task

Charge (14)
Run Both Together
A1: Wait [ MS:0 Seconds:0 Minutes:1 Hours:0 Days:0 ]
A2: Run Shell [ Command:su
chmod 666 /sys/class/power_supply/battery/battery_charging_enabled
echo "1" > /sys/class/power_supply/battery/battery_charging_enabled Timeout (Seconds):0 Use Root:eek:n Store Output In: Store Errors In: Store Result In: Continue Task After Error:eek:n ]

4. Long click on Profile and select Properties. Uncheck: Enforce Task Order"

5. Save everything and test it out.

How it works:

1. Enter task checks /sys/class/power_supply/charging_source to see if phone is plugged in and stores the value in a variable (%PluggedIn).

2. Writes "1" or "0" to /sys/class/power_supply/battery_charging_enabled depending on the battery level.

3. Waits 1 minute.

4. Goes back to top and runs everything over again is variable (%PluggedIn) doesnt equal 0.

Known Issues:

1. Phone could go maybe 1% over defined limit in the 60 seconds before the task runs again. Try changing the wait time to 45 or 30 seconds if this bothers you.

Misc Information and rambling:

1. I used 80% charge limit in my example. Just change it to whatever you want.

2. Having the phone check this every 60 seconds is not a big deal. The phone never sleeps when plugged in anyways. The Profile exits and nothing runs when phone is not plugged in.

3. /sys/class/power_supply/battery/charging_source

0 = Not plugged in

1 = USB

2 = A/C

4. /sys/class/power_supply/battery/constant_charge_current_max

Default value: 1600000

This is 1.6 amps constant charge. You could increase/decrease this number. I tested 2.0 amps and 1.0 amps. Could have your phone only charge at 1.0 amp, even when plugged into the factory charger.

Contributions:

sriharshaarangi

I am no longer monitoring or updating this guide. If you poke around in sys/class/power_supply/ you can most likely modify this basic script for any phone.