Possible solution for setting battery threshold in Qubes

I want to share a small discovery on creating a battery threshold within Qubes OS for ThinkPad users and possible for other laptop brands. I searched on the Qubes forum and found nothing on a workaround for battery threshold, so perhaps this will be useful for others. This finding works in both ThinkPad X230 and T430, running Coreboot via Skulls and working in Qubes 4.0.4 and 4.1. Maybe this is a fluke, but I would like other users to try to see if they can replicate it.

When I first started to set up Qubes OS as my daily driver, I researched online and couldn’t find a way to install TLP to set the battery threshold without installing TLP in dom0. As I could not find any solution apart from installing in dom0, I gave up the search as I didn’t want to install anything in dom0.

The solution came when I wanted to have a backup OS in the laptops just for Zoom calls as Zoom is still a hit and miss with Qubes. I outfitted an mSATA drive on both computers and installed Linux Mint, Zoom and TLP. I have noticed that after setting the battery threshold via TLP on Mint, it carried over to Qubes. The battery threshold would only break if I were to remove the laptop’s battery physically. I would then have to restart in Mint, and then it would carry over again in Qubes.

In addition to an Ubuntu variation, I tried Fedora 35 KDE to replace Mint. It seems to have built-in battery management (perhaps from gnome?) that does not require TLP, which can be found under Advanced Power Management. Like Mint, Fedora 35’s battery threshold appears to carry over to Qubes.

I know that ThinkPads have some chip that controls the batteries. Perhaps after setting the threshold over TLP, this threshold is stored within the battery itself that carries over to Qubes? In my case, this has worked on both original and 3rd party batteries. Since TLP’s threshold is only specifically for ThinkPads, perhaps the Fedora 35’s threshold will work on other brands.

It would be interesting if any users could test this and see if this finding is correct.

2 Likes

This can be done by opening a terminal at dom0 and:

echo '85' | sudo tee /sys/class/power_supply/BAT0/charge_control_end_threshold
echo '75' | sudo tee  /sys/class/power_supply/BAT0/charge_control_start_threshold

This is set to:

  • battery starts charging: 75%
  • battery stops charging: 85%
1 Like

you need sudo echo ....

1 Like

Is this only work for coreboot or it can work with standard BIOS?

What is the problem with Zoom? I mainly use Skype, but have Zoom installed on separte template and it looks like it works. Only thing I noticed is that I have to update it manually since it not has any repo.

Good catch, changed to sudo tee instead.

Yes, standard

Good to know there is a more straight forward method to set the threshold. Will try this and report back. Out of curiosity, will this work with non-ThinkPad models? Thanks everyone.

I think so. You are going straight to the kernel.

Just a little script to spare the laptop batteries which I originally wanted to publish in the guides section (Guides - Qubes OS Forum), to small for github…

[user@dom0 ~]$ cat battery_thresholds.sh 
#! /usr/bin/bash
echo -n "Start Charging BAT0 at: " ; echo 55 | sudo tee /sys/class/power_supply/BAT0/charge_control_start_threshold
echo -n "Stop Charging BAT0 at:  " ; echo 65 | sudo tee /sys/class/power_supply/BAT0/charge_control_end_threshold
echo -n "Start Charging BAT1 at: " ; echo 55 | sudo tee /sys/class/power_supply/BAT1/charge_control_start_threshold
echo -n "Stop Charging BAT1 at:  " ; echo 65 | sudo tee /sys/class/power_supply/BAT1/charge_control_end_threshold

The batteries keep that setting until you drain them accidentally to zero.

1 Like

Thanks for the script. Apologies for the newbie question here but which folder should this script reside in?

Literally anywhere you like in dom0, as long as it’s executable:

chmod +x battery_thresholds.sh

If you’d like it to run this every time you boot, you can make it a systemd service:

  1. Become root:
sudo su -
  1. Put battery_thresholds.sh in /usr/local/bin
cp battery_thresholds.sh /usr/local/bin

(you can literally put it anywhere, as long as you remember where it is, so you can tell systemd where to find it)

  1. Create the following file /etc/systemd/system/battery_thresholds.service:
[Unit]
Description=Set Battery Charging Thresholds

[Service]
Type=Oneshot
ExecStart=/usr/local/bin/battery_thresholds.sh
RemainAfterExit=True

[Install]
WantedBy=multi-user.target
  1. Enable it to run on boot:
systemctl enable battery_thresholds

That script will then run every time you boot your machine :slight_smile:

Thanks so much for the clear explanation! The original battery_thresholds.sh file is located in the home folder. Once I have copied it to the /usr/local/bin folder, per your instruction, do I need to delete the one in the home folder or let it be?