Automatically suspend machine after screen lock is active

When I plug in the power supply while the notebook is in sleep mode, it turns on and stays on for 45 Minutes, as configured in the power saving settings.

Is there a way to configure KDE Neon to get back to sleep in a shorter time period while the lock screen is active?

I have a little script that sets custom dim screen / screen off / suspend timeouts.

It takes modes (--bat, --ac, --lowbat) and timeout values for dim_screen, screen_off and suspend (in that order) in seconds.

To have shorter timeouts I when locked have the script setup like this:

System settings > Notifications > Application-specific settings (Configure) > Screen Saver > Configure events > Run command

Script:

#!/usr/bin/env bash
# requires: kwriteconfig5 qdbus
conf_file="powermanagementprofilesrc"

function set_time() {
  local time=$1
  local group=$2
  local mode=$3

  local time_ms=$time
  if [[ $group != "DPMSControl" ]]; then
    ((time_ms *= 1000))
  fi
    if ((time > 0)); then
      echo "  ${group}: ${time}s"
      kwriteconfig5 --file "$conf_file" --group "$mode" --group "$group" --key idleTime "$time_ms"
    else
      echo "  ${group}: disabled"
      kwriteconfig5 --file "$conf_file" --group "$mode" --group "$group" --key idleTime --delete
    fi
}

function print_help() {
  echo -e "Automate screen & sleep management settings for Battery & AC\n"
  echo -e "Usage:\n$0 <mode> <TIMEOUTS>\n\n--bat [SCREEN_DIM_TIME SCREEN_OFF_TIME SUSPEND_TIME]\n--ac [SCREEN_DIM_TIME SCREEN_OFF_TIME SUSPEND_TIME]\n--lowbat [SCREEN_DIM_TIME SCREEN_OFF_TIME SUSPEND_TIME]"
  echo -e "Time is in seconds, 0 disables the timeout.\n"
  echo -e "Example:\n$0 --ac 300 600 3600 --bat 120 300 600 --lowbat 60 120 300"
  exit 0
}

[[ $1 == "-h" || $1 == "--help" ]] && print_help

mode=""
tmp="$0"
while (("$#")); do
  case "$1" in
  *ac*)
    mode="AC"
    tmp="$1"
    shift
    ;;
  *lowbat*)
    mode="LowBattery"
    tmp="$1"
    shift
    ;;
  *bat*)
    mode="Battery"
    tmp="$1"
    shift
    ;;
  *)
    if [[ ! $1 =~ ^[0-9]+$ || ! $2 =~ ^[0-9]+$ || ! $3 =~ ^[0-9]+$ ]] && [[ -n "$mode" ]]; then
      echo -e "Error: Less than three values passed for $tmp\n\nUse --help to view usage"
      exit 1
    fi
    [[ ! $1 =~ ^[0-9]+$ ]] && shift
    [[ -z "$mode" ]] && echo "Bad format, use --help to view usage" && exit 1
    echo "${mode}:"
    set_time "$1" "DimDisplay" "$mode"
    set_time "$2" "DPMSControl" "$mode"
    set_time "$3" "SuspendSession" "$mode"
    shift 3
    ;;
  esac
done

qdbus org.kde.Solid.PowerManagement /org/kde/Solid/PowerManagement org.kde.Solid.PowerManagement.refreshStatus

Screen unlocked command:

/home/luis/scripts/suspend-time.sh --ac 300 600 3600 --bat 120 300 600 --lowbat 60 120 300

Screen locked command:

/home/luis/scripts/suspend-time.sh --ac 30 60 120 --bat 15 30 60 --lowbat 10 60 120

Output of the command:

AC:
  DimDisplay: 30s
  DPMSControl: 60s
  SuspendSession: 120s
Battery:
  DimDisplay: 15s
  DPMSControl: 30s
  SuspendSession: 60s
LowBattery:
  DimDisplay: 10s
  DPMSControl: 60s
  SuspendSession: 120s
1 Like