Ubuntu/Debian – automatically dim display on battery

I needed something to power up display to 100% while on AC Power and to automatically dim it to 50% while on battery. Unfortunately this can’t be configured in UI in system settings. Hey, Apple, you haven’t something like this either.

I did some research and script triggered by udev should do the trick.

1. Install brightnessctl

sudo apt install brightnessctl

2. Create the script

Create a set-brightness.sh file

sudo nano /usr/local/bin/set-brightness.sh

Copy and Paste this (adapt to your needs)

#!/bin/bash

# Check if device is powered by AC Powers
AC_STATUS=$(cat /sys/class/power_supply/AC*/online)

if [[ "$AC_STATUS" == "1" ]]; then
    brightnessctl set 100%
else
    brightnessctl set 50%
fi

Make the script executable

sudo chmod +x /usr/local/bin/set-brightness.sh

Test the script by running (set your brightness manually to 20% or 80% before to see if the script actually changes the brightness)

sh /usr/local/bin/set-brightness.sh

3. Configure udev – adding a rule

create a 99-brightness.rules file

sudo nano /etc/udev/rules.d/99-brightness.rules

Copy and Paste:

SUBSYSTEM=="power_supply", ATTR{online}=="1", RUN+="/usr/local/bin/set-brightness.sh"
SUBSYSTEM=="power_supply", ATTR{online}=="0", RUN+="/usr/local/bin/set-brightness.sh"

4. Reload udev-rules

sudo udevadm control --reload-rules
sudo udevadm trigger

Optionally restart udev

sudo systemctl restart systemd-udevd

Beteilige dich an der Unterhaltung

1 Kommentar

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Seite verwendet Akismet, um Spam zu reduzieren. Erfahre, wie deine Kommentardaten verarbeitet werden..