1. Overview

Laptops and other portable devices most often utilize batteries. The performance of such batteries degrades over time due to multiple factors, such as overcharging and exposure to high temperatures. Therefore, limiting battery charging up to a certain level can help extend the lifespan of a battery.

In this tutorial, we’ll explore two methods for limiting the maximum battery charging of a Linux device to a certain level.

A common percentage level cited by different manufacturers and at least partially tested as a good state for preserving batteries healthy for longer is 80%. Hence, this is the level we use with each tool.

2. Using TLP

TLP is an advanced power management tool in Linux. Moreover, we can configure it based on our needs, making it ideal for balancing performance and energy conservation in Linux. Furthermore, we can use the TLP tool to optimize battery life by applying power-saving tweaks.

2.1. Installation

First, let’s deploy TLP on the system. To install TLP, we can utilize the apt command in Debian-based systems:

$ sudo apt-get install tlp

After installation, we employ the systemctl command to start the TLP tool:

$ sudo systemctl enable --now tlp
Synchronizing state of tlp.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable tlp

Finally, let’s verify the status of the TLP service:

$ sudo systemctl status tlp
tlp.service - TLP system startup/shutdown
     Loaded: loaded (/lib/systemd/system/tlp.service; enabled; preset: enabled)
...output truncated...

As we can see, we successfully enabled the TLP service.

2.2. Limiting Battery Charging

Before setting the battery charging threshold, let’s check the battery information using the TLP tool:

$ sudo tlp-stat -b
--- TLP 1.5.0 --------------------------------------------

+++ Battery Care
Plugin: generic

+++ Battery Status: BAT0
/sys/class/power_supply/BAT0/manufacturer                   = innotek
/sys/class/power_supply/BAT0/model_name                     = 1
/sys/class/power_supply/BAT0/cycle_count                    =  0 (or not supported)
/sys/class/power_supply/BAT0/energy_full_design             =  50000 [mWh]
/sys/class/power_supply/BAT0/energy_full                    =  50000 [mWh]
/sys/class/power_supply/BAT0/energy_now                     =  47000 [mWh]
/sys/class/power_supply/BAT0/power_now                      = (not available)
/sys/class/power_supply/BAT0/status                         = Discharging
Charge                                                      =   94.0 [%]
Capacity                                                    =  100.0 [%]

The output shows information about the battery, including the manufacturer, model, current status, charge, and capacity.

To limit battery charging, we need to modify the TLP configuration file. Hence, let’s open the TLP configuration file using an editor like nano:

$ sudo nano /etc/tlp.conf
 ------------------------------------------------------------------------------
# /etc/tlp.conf - TLP user configuration (version 1.4)
# See full explanation: https://linrunner.de/tlp/settings
#
# Settings are read in the following order:
#
# 1. Intrinsic defaults
# 2. /etc/tlp.d/*.conf - Drop-in customization snippets
# 3. /etc/tlp.conf     - User configuration (this file)
# BAT0: Primary / Main / Internal battery (values in %)
# Note: also use for batteries BATC, BATT and CMB0
# Default: <none>

#START_CHARGE_THRESH_BAT0=75
#STOP_CHARGE_THRESH_BAT0=80

# BAT1: Secondary / Ultrabay / Slice / Replaceable battery (values in %)
# Note: primary on some laptops
# Default: <none>
...output truncated...

In the TLP configuration file, we search for the STOP_CHARGE_THRESH_BATO variable. Once we find it, we uncomment it and set a value that indicates the maximum charge percentage of the battery:

STOP_CHARGE_THRESH_BAT0=80

After we modify the STOP_CHARGE_THRESH_BATO variable, we restart the TLP service:

$ sudo systemctl restart tlp

Thus, the battery’s maximum charging level is now set to 80%.

3. Using upower

We can use the upower tool to limit the maximum battery charging in Linux. In particular, the tool fetches various data from power devices such as AC adapters and batteries. Additionally, upower can be used to manage power settings, monitor system power states, and automate power policies.

3.1. Installation

Before using the upower tool, we must install it in the system. Hence, we utilize the apt command to install the upower tool in Debian-based systems from the terminal:

$ sudo apt install upower

After installation, let’s check the version of the upower tool:

$ upower --version
UPower client version 1.90.2
UPower daemon version 1.90.2

Therefore, we have installed the upower tool in the system.

Next, let’s start the upower service utilizing the systemctl command:

$ sudo systemctl start upower

Like other similar tools, upower functions as a service, which monitors and regulates power according to its configuration.

3.2. Limiting Battery Charging

First, let’s check all the power devices present in the system using the enumerate option:

$ upower --enumerate
/org/freedesktop/UPower/devices/line_power_AC
/org/freedesktop/UPower/devices/battery_BAT0
/org/freedesktop/UPower/devices/DisplayDevice

As we can see, three power devices are connected to the system: an AC adapter, a battery, and a display device. Using the upower tool and its -d option, we can extract detailed information about the power devices:

$ upower -d
Device: /org/freedesktop/UPower/devices/line_power_AC
  native-path:          AC
  power supply:         yes
  updated:              Fri 22 Mar 2024 10:45:09 PM EET (3405 seconds ago)
  has history:          no
  has statistics:       no
  line-power
    warning-level:       none
    online:              no
    icon-name:          'ac-adapter-symbolic'

Device: /org/freedesktop/UPower/devices/battery_BAT0
  native-path:          BAT0
  vendor:               innotek
  model:                1
  serial:               0
  power supply:         yes
  updated:              Fri 22 Mar 2024 11:41:53 PM EET (1 seconds ago)
  has history:          yes
  has statistics:       yes
...output truncated...

Now, to limit the maximum battery charging, we use the battery information generated by the upower tool and create an udev rule using the nano editor:

$ sudo nano /etc/udev/rules.d/r_battery.rules

Specifically, we insert some instructions to change the maximum charging limit:

SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/bin/sh -c 'echo 80 > /sys/class/power_supply/BAT0/charge_control_end_threshold'"

In the instructions, we specify the maximum charging as 80% when the AC adapter is plugged in. Additionally, it’s essential to include the correct name of the battery device. In this case, the name of the battery is BAT0.

Now, we save the file and close the editor. Furthermore, let’s refresh the udev rules using the reload-rules option to apply the changes:

$ sudo udevadm control --reload-rules

Finally, we must restart the upower tool to reflect the changes we made:

$ sudo systemctl restart upower

At this point, we have successfully set the maximum charging level of the battery to 80%.

4. Conclusion

In this article, we discussed two methods to limit the maximum battery charging level in Linux.

The TLP tool contains several advanced features to control and monitor power-related tasks. Additionally, using the TLP configuration file, we can directly control the battery’s maximum charging level.

On the other hand, the upower tool provides information on the power devices present in the system. However, there is no direct option like TLP to set the maximum charging level of the battery.