Is there a way to "reset" idle time when unplugging laptop?

I have my laptop set to never suspend when plugged in, and to suspend after a period of time when on battery; this is the way I'd like to keep it.

I regularly switch between using my laptop and desktop, so sometimes my laptop sits plugged in but unused for a period of time. If I intend to use the laptop at my workbench that's no problem, but sometimes I want to unplug the laptop and take it with me to go work somewhere else. However, if it's been sitting idle long enough, I will either get a notification similar to this:

Automatic suspend notification

or if it's been long enough, the laptop will go right into suspend mode before I can react.

I'm literally picking up my laptop to use it, and would rather it not go into suspend immediately upon unplugging. Is there any setting I can change so that the inactivity timer restarts when unplugging my laptop? Failing that, is there any sort of workaround for this, such as some script that can be triggered by power management upon unplugging, that emulates some sort of activity to reset the timer?

EDIT 2021-09-17T18:21Z

I've attached a screenshot of my current power settings. This is how I'd like them to stay:

power settings

The problem is, if my laptop has been idle while plugged in for 29 minutes, then I unplug it, pick it up, walk into the living room, and sit down, it goes to sleep on me because it now thinks it's been idle for 30 minutes. I'd somehow like it to interpret connecting/disconnecting DC power as "activity", such that it would only suspend if I didn't touch the laptop for 30 minutes after unplugging it.


Reset idle time when unplugging power, or reset idle time while you are on AC?

Even if you'd manage to create an action on unplugging (you can), the question is wether you would be in time to prevent suspending.

A more reliable trick is to prevent getting idle time pass the idle-time threshold at all, but only if you are on AC.

If you are on X, how can we do that?

Run a tiny daemon-like background script that - only when you are on AC and idle for more than (let's say) a minute - simulates a key press Control. That's a key that does nothing if you are not actually working on the laptop, only resets idle time.
Then if you unplug, the virtual keypress is skipped and the laptop will do whatever you set it to do after x time.
Below an example of such a script. In a comment, you mentioned you are using Ubuntu 18.04, Gnome, which means you are using Mutter (X), so you can use the tools in the script below.

How to use?

  1. Make sure xprintidle, xdotool and acpi are installed: sudo apt install xdotool acpi xprintidle
  2. Copy the script into an empty file.
  3. Test-run it from a terminal: python3 /path/to/script. If all works fine, add it to your startup commands.

The script

#!/usr/bin/env python3
import subprocess
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import GLib, Gtk

def test(arg):
    # let's see what is the current idle time
    idletime = int(subprocess.check_output("xprintidle").decode("utf-8"))
    # and if we are plugged in
    onpower = "on-line" in subprocess.check_output(
        ["acpi", "-a"]
    ).decode("utf-8")
    # if idle for > 60 seconds AND on power, tap Ctrl to reset idle
    if all([idletime/1000 > 60, onpower]):
        subprocess.Popen(
            ["xdotool", "key", "Ctrl"]
        )
    # keep it running please
    return True

GLib.timeout_add_seconds(15, test, None)

Gtk.main()