Controlling the battery charging threshold on Asus laptop with Ubuntu 20.04
From kernel 5.4+ there is an ability to control charging of the laptop battery. In the /sys/class/power_supply/BAT0/
are some variables useful for battery monitoring and controlling.
ls /sys/class/power_supply/BAT0/
alarm device@ manufacturer serial_number uevent capacity energy_full model_name status voltage_min_design
capacity_level energy_full_design power/ subsystem@ voltage_now charge_control_end_threshold energy_now power_now technology cycle_count hwmon2/ present type
One of those variables is charge_control_end_threshold
, it is just an integer number indicating when to stop charging.
Whith:
cat /sys/class/power_supply/BAT0/charge_control_end_threshold
we can see actual value of treshold.
Simply whit writing into a variable, we can control when charging stops:
echo 70 | sudo tee /sys/class/power_supply/BAT0/charge_control_end_threshold
Whit this command, treshold is set to 70% and charging will stop on 69%.
Just for convenience, I’ve been made this bash script: /usr/bin/tresho
!/bin/bash
echo $1 | sudo tee /sys/class/power_supply/BAT0/charge_control_end_threshold
And now just type on command prompt: tresho 50
or whatever value you want.
Set this variable during system startup, for making it permanent
Create the following systemd service: etc/systemd/system/tresho.service
[Unit]
After=network.service
[Service]
ExecStart=/usr/bin/tresho70.sh
[Install]
WantedBy=default.target
And this bash script: /usr/bin/tresho70.sh
#!/bin/bash
echo '70' | sudo tee /sys/class/power_supply/BAT0/charge_control_end_threshold
Then activate the service:
sudo service tresho start
Alternate solution
For adjusting battery charging threshold on system startup create the following systemd service:
/etc/systemd/system/battery-charge-threshold.service
[Unit]
Description=Set the battery charge threshold
After=multi-user.target
[Service]
Type=oneshot
ExecStart=/bin/bash -c 'echo 70 > /sys/class/power_supply/BAT0/charge_control_end_threshold'
[Install]
WantedBy=multi-user.target
and then enable it.
Warning: As of systemd 246, activation of this unit may cause to infinite restart of service above.
/etc/systemd/system/battery-charge-threshold.path
[Path]
PathExists=/sys/class/power_supply/BAT0/
Unit=battery-charge-threshold.service
[Install]
WantedBy=multi-user.target
Create this unit file and enable it. The service above should be disabled since the path unit activating it.