Windows laptop: Don't sleep when lid closed, but ONLY if an external monitor is plugged in?

On a Windows 10 laptop, I know you can make it so that the computer doesn’t go to sleep when you close the lid. However, is there a way to change that setting depending on whether an external monitor is connected?

If I’m using the laptop by itself, I want to be able to close the lid to make it go to sleep. But if I connect an external monitor, I also close the lid (to keep dust out of the keyboard), but don’t want it to sleep.

Is that nuance possible?

I can’t think of any power settings like that, though I’ve never run dual monitors, so I’ve never gone looking for it.
ISTM, the easiest thing to do would tell the computer never to sleep, just shut off the monitor when the lid is closed. From there, you could leave it like that, never sleeping, but if you want it to sleep you can manually put it to sleep, program a hot key to do it or tell windows that you want the power button to put it to sleep.
It’s an extra step, but I think it’ll work.

What if you told it to shut off the screen any time you closed the lid, but go to sleep after X minutes/hours if inactivity. If you have a second monitor, I presume you’re still using the computer, so it should stay awake, right?

I’m guessing when you have an external monitor connected, you also have the AC adapter connected. And when you’re using the computer on it’s own, you’re running on battery power. Because you can change the sleep settings depending on the power source. Will that work for you?

You could do it with a bit of programming. Not sure if that’s in your skill set, but AutoHotkey should be up to the task. Basically, you would have a little script running in the background, detecting the monitor count. If it’s 2, then you change the power settings to not go to sleep. Otherwise you put it in the standard mode.