Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic switch profile to powersave when battery below a certain threshold #708

Open
josecastillolema opened this issue Nov 8, 2024 · 1 comment

Comments

@josecastillolema
Copy link

Automatic switch profile to powersave when battery below a certain threshold

@xChAmeLIoNz
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants