tuned icon indicating copy to clipboard operation
tuned copied to clipboard

Automatic switch profile to powersave when battery below a certain threshold

Open josecastillolema opened this issue 1 year ago • 1 comments

Automatic switch profile to powersave when battery below a certain threshold

josecastillolema avatar Nov 08 '24 11:11 josecastillolema

Automatic switch profile to powersave when battery below a certain threshold

You may find this useful:

#!/bin/bash

BAT=$(echo /sys/class/power_supply/BAT*)
BAT_STATUS="$BAT/status"
BAT_CAP="$BAT/capacity"
LOW_BAT_PERCENT=20

AC_PROFILE="throughput-performance"
BAT_PROFILE="balanced"
LOW_BAT_PROFILE="powersave"
# tuned-adm list

# wait a while if needed
[[ -z $STARTUP_WAIT ]] || sleep "$STARTUP_WAIT"

# start the monitor loop
prev=0

while true; do
    # read the current state
    if [[ $(cat "$BAT_STATUS") == "Discharging" ]]; then
        if [[ $(cat "$BAT_CAP") -gt $LOW_BAT_PERCENT ]]; then
            profile=$BAT_PROFILE
        else
            profile=$LOW_BAT_PROFILE
        fi
    else
        profile=$AC_PROFILE
    fi  

    # set the new profile
    if [[ $prev != "$profile" ]]; then
        echo setting power profile to $profile
        # Fedora 41 uses tuned-adm, which is incompatible with power-profiles-daemon
        tuned-adm profile $profile # powerprofilesctl set $profile
    fi  

    prev=$profile

    # wait for the next power change event
    inotifywait -qq "$BAT_STATUS" "$BAT_CAP"
done

I found the original script here and made some tuning (pun intended) to make it work with tuned.

You can create a systemd service and enable it at boot, like the linked guide describes.

iseppe avatar Nov 28 '24 17:11 iseppe