Automatically limit (stop) battery charging at a given percentage Device : Moto X Play ( Rooted, Stock, Android 6.0.1, msm8916 Board, XT1562)

Why do I want to limit charging ?

( Let's leave out whether my reasons are valid or not and accept that I want to, if you disagree with my reasons)

To improve the longevity of battery , I wish to limit charging when it reaches 90%, as explained here 
Ideal charging / discharging percentage for maximum battery life?

Is it possible?

Yes, at least on my previous device Honor 6, as brought out here How do I override charging current on Huawei Honor 6?, with two different ways of doing it

I have tried  unsuccessfully to identify the file responsible in which changing values would limit charging

I am looking for the system file responsible for stopping charging, with it's true path (without symlinks)

(as I saw on on my previous device, locating the path of file was a big challenge after identifying it)
Okay.. Got it :)

The first thing to note is on Honor 6, my previous device ,  there was a single file I could play around with to stop charging automatically

On Moto X play, there are two distinct  files - one for disconnecting the charger and other for removing charging symbol ( In other words, you could stop charging and still have the charger connected symbol showing). Till I realised this, I was running around in circles, since I took charging symbol to show that charging was happening ( and assuming I was dealing with the wrong file ). While experimenting, when I noticed charge drop despite charging symbol showing, I verified with Ampere app and wisdom dawned !


What are the files?
(Edit:30 Mar 20 Jackpot! Found this awesome repository
of files for various devices by the developer of Advanced Charging Controller
Magisk Module. You are better off using that versatile module but in case you want to DIY, it's a great start) .
There may be other files, but the files I homed on too are :

For Charging Icon : /sys/devices/soc.0/78d9000.usb/power_supply/usb/online  ( For disabling icon change  value of file from 0 to 1)

For  charging : /sys/devices/soc.0/78b5000.i2c/i2c-1/1-001c/power_supply/battery/charging_enabled ( For disabling charging change  value of file from 1 to 0)


For automating this, approach was similar to my answer here using Terminal Emulator and Macrodroid as explained here - How to automate command line when specific application is launched?
How did I find the files?
Inelegantly,  and I am hoping that somebody posts a good way. Here is a way that may work for you in identifying the file and path
Assumptions

Long hours on the net and some coding on GitHub ( which I don't understand at all ) pointed me to look at i2c and usb related files with names like charger_enabled or enable_charger. This seems to be true for Honor 6, Moto X and some Nexus devices. I have no clue whether it is chipset or device dependent

The true path of this file is in /sys.  Again, may vary


Next Steps

Do a search for charger enabled or enable_charger or a wild card search  with similar words in /sys

Change rw permissions to enable changing the content from 1to 0 or vice versa

Connect your charger and see the value in file, disconnect and repeat. If  the value changes, it looks like the right file. While the charger is connected, change the value in file and if charging stops, you have hit it (More so, if the path is in i2c directory). Verify with Ampere app to confirm charging status

While you are at it, may as well copy original and modified files to a separate directory in your storage. Needed for automation

If your device is like mine where charging can be stopped but charging icon, shows, more grind to identify that file by keywords like online or connected or ???


If you are lucky, above should work for you

Just as additional information for posterity the Terminal Emulator shortcuts are :

For disabling charging and removing the charging icon

su –c &quot;cp  /storage/emulated/0/moto_charging/disable_charging/charging_enabled /sys/devices/soc.0/78b5000.i2c/i2c-1/1-001c/power_supply/battery/&quot; ; su -c &quot;cp /storage/emulated/0/moto_charging/no_symbol_charging/online /sys/devices/soc.0/78d9000.usb/power_supply/usb/&quot;

For enabling charger and charging icon

su -c &quot;cp /storage/emulated/0/moto_charging/show_charging/online /sys/devices/soc.0/78d9000.usb/power_supply/usb/&quot; ; su –c &quot;cp  /storage/emulated/0/moto_charging/enable_charging/charging_enabled /sys/devices/soc.0/78b5000.i2c/i2c-1/1-001c/power_supply/battery/&quot;
moto_charging is a folder created in Internal SD that has original and modified copies ( to enable and disable charging), and these files are copied into the system. Of course, requires root