These instructions are primarily based on Thinkpad laptops. However, many users have notified that Asus and other manufacturers’ laptops are starting to support the threshold using almost the same methods. I have mentioned these variations and suggestions as well.
Table of Contents
- Why Setting a Threshold Is Important?
- Step 1: Check if Your Laptop Supports Setting the Battery Charging Threshold.
- Step 2: Set the Battery Charging Threshold.
- Step 3: Make the Changes Made Under the Previous Step Permanent.
- Way Ahead
Why Setting a Threshold Is Important?
Frequent charging and discharging loops lead to degradation in the battery. Therefore, to promote the battery’s lifespan, you need to reduce this loop. And if your work environment involves sitting at a table all the time, your laptop should stay connected to the electric outlet all the time.
But, that will put your laptop always at 100% charged level which is another cause of battery degradation. Hence, many manufacturers (like Lenovo, and Asus) ask you to set charging limit/threshold.
Unfortunately, these manufacturers provide their tools only for Windows and not for Linux. Hence, through this article, I would like to assist you in how to set the battery charging threshold on Linux. Just follow these steps and you are done.
Step 1: Check if Your Laptop Supports Setting the Battery Charging Threshold.
You can use any file manager such as Nautilus or Dolphin to find a special directory /sys/class/power_supply/BAT0/ or /sys/class/power_supply/BAT1/ or /sys/class/power_supply/BATT/ or /sys/class/power_supply/BATC/ or something like that.
Or, you can use the following ls commands (or something like that):
~$ ls --recursive /sys/class/power_supply/BAT*
A sample Output on my Thinkpad:
Now, check if there are threshold-like words in the above output. If you can find any file like that, your laptop should support setting the threshold, and hence, you can move on to the next step.
📔Note: I could not find any such files on the gaming laptop Lenovo Legion 5 in Linux. However, as an ad-hoc arrangement, I was able to set the battery charging threshold using the Lenovo Vantage app preinstalled on the Windows partition. Just enable the conservation mode. These settings are applied in both the Windows and Linux partitions.
Step 2: Set the Battery Charging Threshold.
Now, you need to write desired thresholds in the two files found above. For ThinkPad laptops, they are /sys/class/power_supply/BAT0/charge_control_start_threshold and /sys/class/power_supply/BAT0/charge_control_end_threshold.
Lenovo’s Recommendations for these two thresholds:
- If you put your laptop on charger all the time, set start and end thresholds to 40% and 50% respectively.
- If the laptop is occassionally on the battery’s power, set start and end limits to 85% and 90% respectively. This is what I am using in one of my thinkpad laptops.
For other manufacturers, if you cannot find any value on their websites or other places, try these values and see if they work. Please comment below if these values work for your manufacturers.
To write these thresholds, first, get the root privileges using su:
~$ sudo su
Now, you can use any text editor like nano or vim. Or use the following commands directly:
# echo START_THRESHOLD > START_THRESHOLD_FILE; echo END_THRESHOLD > END_THRESHOLD_FILE
Replace the SART_THRESHOLD, END_THRESHOLD, START_THRESHOLD_FILE, and END_THRESHOLD_FILE with the appropriate files. For example, for ThinkPad laptops:
# echo 85 > /sys/class/power_supply/BAT0/charge_control_start_threshold; echo 90 > /sys/class/power_supply/BAT0/charge_control_end_threshold
And you are done. But, these files’ contents are reverted back to the default state on the next reboot and hence you need to make these changes permanent.
Step 3: Make the Changes Made Under the Previous Step Permanent.
To make the changes permanent, your computer needs to write these two limits at every system reboot.
For this purpose, you will need to create a systemd-service. To create this service, create a file /etc/systemd/system/battery-charge-threshold.service using nano or your favorite text editor:
~$ sudo nano /etc/systemd/system/battery-charge-threshold.service
And now, fill the file with the following contents:
[Unit]
Description=Set the battery charge threshold
After=multi-user.target
StartLimitBurst=0
[Service]
Type=oneshot
Restart=on-failure
ExecStart=/bin/bash -c 'YOUR_THRESHOLD_COMMAND'
[Install]
WantedBy=multi-user.target
Here, you need to replace the YOUR_THRESHOLD_COMMAND in the ExecStart line with the echo command mentioned above. For example, for ThinkPad laptops, this whole line would become
ExecStart=/bin/bash -c 'echo 85 > /sys/class/power_supply/BAT0/charge_control_start_threshold; echo 90 > /sys/class/power_supply/BAT0/charge_control_end_threshold'
Now, save the file and enable the above systemd service using the following systemctl command:
~$ sudo systemctl enable battery-charge-threshold.service
Note:
You might be thinking why I am not using a cronjob to write these thresholds into the relevant files at every reboot using
@reboot echo 85 > /sys/class/power_supply/BAT0/charge_control_start_threshold; echo 90 > /sys/class/power_supply/BAT0/charge_control_end_threshold
Using the above cronjob would have been much easier. But the above cronjob does not work always. The reason is that the battery’s directory is created by the Linux kernel at every boot. And sometimes, the directory is created before the cronjob and sometimes after the cronjob. In the first case, cronjob works but in the second case, it does not. On the other hand, systemd can keep banging on until the directory is created. The Restart=on-failure
line in the file /etc/systemd/system/battery-charge-threshold.service makes sure this. It can easily be understood using the following picture:
Now, to apply the above changes, restart your computer.
Note: You might see the error message battery-charge-threshold.service: Failed with result 'exit-code'
at the boot time (and/or at the shutdown). This is completely normal. The thresholds will still work. These failures occur 3-4 times just before the service passes. You can verify it by looking into the ‘threshold’ files once your system boots.
[ajay@lenovo ~] $ journalctl --unit=battery-charge-threshold.service
-- Boot 566b1db1387e4f16b871b2e3c90c1ad7 --
Jun 05 17:29:45 lenovo systemd[1]: Starting Set the battery charge threshold...
Jun 05 17:29:45 lenovo bash[1172]: /bin/bash: line 1: /sys/class/power_supply/BAT0/charge_control_start_threshold: Permission denied
Jun 05 17:29:45 lenovo bash[1172]: /bin/bash: line 1: /sys/class/power_supply/BAT0/charge_control_end_threshold: Permission denied
Jun 05 17:29:45 lenovo systemd[1]: battery-charge-threshold.service: Main process exited, code=exited, status=1/FAILURE
Jun 05 17:29:45 lenovo systemd[1]: battery-charge-threshold.service: Failed with result 'exit-code'.
Jun 05 17:29:45 lenovo systemd[1]: Failed to start Set the battery charge threshold.
Jun 05 17:29:46 lenovo systemd[1]: battery-charge-threshold.service: Scheduled restart job, restart counter is at 1.
Jun 05 17:29:46 lenovo systemd[1]: Stopped Set the battery charge threshold.
Jun 05 17:29:46 lenovo systemd[1]: Starting Set the battery charge threshold...
Jun 05 17:29:46 lenovo bash[1277]: /bin/bash: line 1: /sys/class/power_supply/BAT0/charge_control_start_threshold: Permission denied
Jun 05 17:29:46 lenovo bash[1277]: /bin/bash: line 1: /sys/class/power_supply/BAT0/charge_control_end_threshold: Permission denied
Jun 05 17:29:46 lenovo systemd[1]: battery-charge-threshold.service: Main process exited, code=exited, status=1/FAILURE
Jun 05 17:29:46 lenovo systemd[1]: battery-charge-threshold.service: Failed with result 'exit-code'.
Jun 05 17:29:46 lenovo systemd[1]: Failed to start Set the battery charge threshold.
Jun 05 17:29:46 lenovo systemd[1]: battery-charge-threshold.service: Scheduled restart job, restart counter is at 2.
Jun 05 17:29:46 lenovo systemd[1]: Stopped Set the battery charge threshold.
Jun 05 17:29:46 lenovo systemd[1]: Starting Set the battery charge threshold...
Jun 05 17:29:46 lenovo bash[1302]: /bin/bash: line 1: /sys/class/power_supply/BAT0/charge_control_start_threshold: Permission denied
Jun 05 17:29:46 lenovo bash[1302]: /bin/bash: line 1: /sys/class/power_supply/BAT0/charge_control_end_threshold: Permission denied
Jun 05 17:29:46 lenovo systemd[1]: battery-charge-threshold.service: Main process exited, code=exited, status=1/FAILURE
Jun 05 17:29:46 lenovo systemd[1]: battery-charge-threshold.service: Failed with result 'exit-code'.
Jun 05 17:29:46 lenovo systemd[1]: Failed to start Set the battery charge threshold.
Jun 05 17:29:46 lenovo systemd[1]: battery-charge-threshold.service: Scheduled restart job, restart counter is at 3.
Jun 05 17:29:46 lenovo systemd[1]: Stopped Set the battery charge threshold.
Jun 05 17:29:46 lenovo systemd[1]: Starting Set the battery charge threshold...
Jun 05 17:29:46 lenovo systemd[1]: battery-charge-threshold.service: Deactivated successfully.
Jun 05 17:29:46 lenovo systemd[1]: Finished Set the battery charge threshold.
lines 983-1019/1019 (END)
Way Ahead
That’s all about setting your battery charging thresholds. Thank you for staying till this point. If there is any recommendation or I have committed any mistakes, point them out in the comment section below.
And please mention your variations, if any, for your non-ThinkPad laptops. It will help the Linux community.
You can see this article if your brightness key is not working in your Linux system.
how do i undo or change the values? i also tried removing the config file but it doesnt resume charging
To undo the changes, just disable it using the systemd command ‘~$ sudo systemctl disable battery-charge-threshold.service’ and then restart your PC (or use systemctl restart command). And to change the value, just change the numbers 85 and 90 and then restart or your pc.
Awesome thanks