Capping Battery Percentage in Ubuntu, on an Asus A14 Laptop
Laptop batteries do not especially enjoy sitting at 100% charge all day. Since this machine is often plugged in, I wanted to cap the battery at 80% rather than letting it stay fully charged whenever it was on mains power. This should extend the life of the lithium battery.
Capping the Charge Percentage
On this A14, the setting is exposed through the Linux power supply interface under /sys/class/power_supply/. The
relevant file is:
/sys/class/power_supply/BAT0/charge_control_end_threshold
First, I checked the current value:
cat /sys/class/power_supply/BAT0/charge_control_end_threshold
It was set to 100, meaning the battery would charge all the way. To change it to 80%, I wrote the new threshold into
that file:
echo 80 | sudo tee /sys/class/power_supply/BAT0/charge_control_end_threshold
That worked immediately. The battery stopped charging at 80%, and upower showed the expected behaviour:
upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E 'state|percentage'
While charging, the output looked like this:
state: charging
percentage: 79%
Then, once it reached the cap:
state: pending-charge
percentage: 80%
That was the important confirmation: the threshold was being respected by the firmware/kernel power stack, not just written to a decorative config file.
Making the Change Persistent
However, there was one catch. After startup, the threshold file kept reverting to 100. So the manual command proved
that charge limiting worked, but it was not enough to make the setting persistent.
The fix was to add a small systemd oneshot service that writes the threshold during boot.
I created a service file:
sudo nano /etc/systemd/system/set-battery-charge-threshold.service
With this content:
[Unit]
Description=Set battery charge threshold to 80%
[Service]
Type=oneshot
ExecStart=/bin/sh -c 'echo 80 > /sys/class/power_supply/BAT0/charge_control_end_threshold'
[Install]
WantedBy=multi-user.target
Then I enabled and started it:
sudo systemctl enable set-battery-charge-threshold.service
sudo systemctl start set-battery-charge-threshold.service
After that, I checked the threshold again:
cat /sys/class/power_supply/BAT0/charge_control_end_threshold
This time it reported:
80
The useful lesson is that the kernel interface was the right control point, but on this machine the setting needed to be re-applied at boot. A tiny systemd service was enough to make the 80% charge cap stick.