Auto-switching power profiles when plugging in your charger using power-profiles-daemon on Linux
power-profiles-daemon present in Ubuntu and Fedora is fantastic, I’ve
been been able to squeeze out a lot of extra battery life and performance by
switching the profiles. The only problem is the lack of support for
auto-switching the profile based on whether or not your laptop is currently
charging. Ideally, I wanted my laptop to go into performance mode when plugged
in, and when it goes on battery it should switch back to balanced mode.
To do this I wrote a little script that monitors the charging state of your
laptop, which then switches the power-profile whenever you plug or unplug it
from your charger. The script is started automatically when you log in using a
systemd user service. Before you’ll be able to use this script you need to
Copy the following script somewhere (I like using
~/.local/bin), and make
sure that it is executable. You can adjust which power-profiles should be used
for the charging, discharging and low battery states by changing the values in
LOW_BAT_PROFILE variables, respectively.
Once you have the script stored somewhere and ready to execute, you can add the
following service file in your local systemd config folder, i.e.
~/.config/systemd/user/power-monitor.service (you might need to create the
folder). Be sure to update the
ExecStart parameter to your script’s path. You
can then enable and start the service using
systemctl --user enable --now power-monitor.service.
That should be it. You can then monitor the script’s operation using
systemctl --user status power-monitor.service, you should be getting log entries when it
updates the power profile. The script will wait 30 seconds before starting up,
I’ve found if I don’t add this delay the power-profiles daemon will throw an
error when I log in (presumably because it is not yet ready).