Most of the time when using my laptop I am plugged into it’s AC charger. But, I don’t want my lithium-ion battery charged to 100% all of the time, as that is unhealthy for the battery and will lead to it prematurely losing capacity.
My Lenovo X1 Carbon allows you to set a value less than 100 in the:
/sys/class/power_supply/BAT0/charge_control_end_threshold
field and will stop charging your battery if it goes above that amount.
So you can issue a command like the following to prevent your laptop from charging your battery above 80%:
echo "80" | sudo tee /sys/class/power_supply/BAT0/charge_control_end_threshold
(and if you wanted to get fancy, you could tell it to not start charging the battery until it fell below 70% with a command like the following)
echo "70" | sudo tee /sys/class/power_supply/BAT0/charge_control_start_threshold
Then, you would have to unplug the laptop to discharge the battery below 80, and it would just stay there. If you were planning on taking your laptop somewhere without a power cord, you would want to reset this to 100 and fully charge it before you left.
On the X1 carbon with Ubuntu 21.04, I got some “/sys/class/power_supply/BAT0/charge_control_end_threshold: Invalid argument” type errors when trying to set the end amount to a value lower than the current start amount (or the start amount to a value higher than the current end amount). So if you are messing with one, you should probably set the other appropriately before-hand. For example, I use the following commands, in this specific order to limit to 80 (start charging below 70):
echo "70" | sudo tee /sys/class/power_supply/BAT0/charge_control_start_threshold echo "80" | sudo tee /sys/class/power_supply/BAT0/charge_control_end_threshold
And then to reset to 100 (start charging below 99 or less) I use the commands in this order:
echo "100" | sudo tee /sys/class/power_supply/BAT0/charge_control_end_threshold echo "99" | sudo tee /sys/class/power_supply/BAT0/charge_control_start_threshold
Using this ordering prevents the Invalid argument errors for me.
The above commands reset the default values, and will not survive a reboot. So if you want them to always be active, you can set up a system service which will fire every time your computer reboots by creating a file in /etc/systemd/system/ called charge-threshold.service that looks like the following:
[Unit] Description=Set the battery charge threshold After=multi-user.target StartLimitBurst=0 [Service] Type=oneshot Restart=on-failure ExecStart=/bin/bash -c 'echo 70 | tee /sys/class/power_supply/BAT0/charge_control_start_threshold; echo 80 | tee /sys/class/power_supply/BAT0/charge_control_end_threshold' [Install] WantedBy=multi-user.target
Then enable the service with the following:
X1:/etc/systemd/system$ sudo systemctl enable charge-threshold.service X1:/etc/systemd/system$ sudo systemctl start charge-threshold.service
Oh, nice feature! I wonder if my XPS from a couple years ago has a sysfs driver for this.
Maybe a helpful tip; you can combine those commands by doing `systemctl enable –now `
That works for modern systems, but I left them as separate commands just in case anybody is using an older version.