Automatic turn off keyboard backlight when idle

Hi,

I made a small script to automatically turn off keyboard backlight when idle, resume when there’s input, it works with my Asus Zenbook UX3405CA laptop, and I believe it works with others with minor modifiaction to the script to your specification.

#!/bin/bash

# brightnessctl and swayidle are required !!!


# Configuration
KEYBOARD_BACKLIGHT_DEVICE="asus::kbd_backlight"
IDLE_TIMEOUT=5 # in seconds

# Start swayidle to monitor for inactivity
swayidle \
  timeout $IDLE_TIMEOUT "brightnessctl --device='$KEYBOARD_BACKLIGHT_DEVICE' set 0" \
  resume "brightnessctl --device='$KEYBOARD_BACKLIGHT_DEVICE' set 1"

Don’t forget to chmod your script !!!

Just add the script to Autostart –> Login script and restart your system.

Sadly it doesn’t work when watching Youtube on Chrome but I believe it is related to the inhibit systemd function because while I’m on Youtube regardless of whether I’m playing or not the sleep function will not be triggered.

However you could minizie the browser if you aren’t watching it and just listening to it, the script will work.

Cheers!

1 Like

his is very nice, thanks for sharing!

It can be improved a little bit. The problem is that if you manually turn off the backlight, it will be turned on again by the resume command. But usually, when you manually turn it off, you want it to stay off until you manually tun it on again.

This version of the script will save the current state to a tmp file before turning the backlight off, and restore the previous state on resume. This is achieved with the “-s” and “-r” flags of brightnessctl (“save” and “restore”).
There is a default value in case the tmp file is not found on resume.

You will maybe have to adapt the constants.

#!/bin/bash 

# brightnessctl and swayidle are required !!! 


# Configuration 
KEYBOARD_BACKLIGHT_DEVICE="tpacpi::kbd_backlight" 
IDLE_TIMEOUT=15 # in seconds 
TMP_FILE="/run/user/1000/brightnessctl/leds/$KEYBOARD_BACKLIGHT_DEVICE" # tmp file saving location for brightnessctl -s 
DEFAULT_BRIGHTNESS=1 # default brightness value to use in case $TMP_FILE does not exist 

# Start swayidle to monitor for inactivity 
swayidle \ 
   timeout $IDLE_TIMEOUT "brightnessctl -d '$KEYBOARD_BACKLIGHT_DEVICE' -s set 0" \ 
   resume "[[ -e $TMP_FILE ]] \ 
      && brightnessctl --device='$KEYBOARD_BACKLIGHT_DEVICE' -r \ 
      || brightnessctl --device='$KEYBOARD_BACKLIGHT_DEVICE' set $DEFAULT_BRIGHTNESS"
1 Like

Thank you for sharing !

It’s really a pity I can’t edit my post here, so that I have to add a new one. The script I posted doesn’t work well, this one does:

#!/bin/bash

# brightnessctl and swayidle are required !!!


# Configuration
KEYBOARD_BACKLIGHT_DEVICE="tpacpi::kbd_backlight"
IDLE_TIMEOUT=15 # in seconds
TMP_FILE="/run/user/1000/brightnessctl/leds/$KEYBOARD_BACKLIGHT_DEVICE" # tmp file saving location for brightnessctl -s
DEFAULT_BRIGHTNESS=1 # default brightness value to use in case $TMP_FILE does not exist

# Start swayidle to monitor for inactivity
swayidle \
   timeout $IDLE_TIMEOUT "brightnessctl -d $KEYBOARD_BACKLIGHT_DEVICE -s set 0" \
   resume "[[ -e $TMP_FILE ]] \
      && brightnessctl -d $KEYBOARD_BACKLIGHT_DEVICE -r \
      || brightnessctl -d $KEYBOARD_BACKLIGHT_DEVICE set $DEFAULT_BRIGHTNESS"

Actually my original intention was to write a script that listen to the keyboard and the touchpad “events” and if both of them are idle then turn off the keyboard backlight, doing so I believe it’ll solve the systemd inhibit issue.

Sadly I’m not savvy enough to do so but I’m still researching it hopefully I’ll find the solution to that.

Cheers!