If you check upthread you’ll see that a couple of different methods were suggested for doing this. They all work, just depends on what your own preferences are.
The most flexible will be core:
But many people would just be able to use a custom alert in smart home monitor, which is much easier to set up.
I have mine set up to alert me if the linen closet is kept open for more than two minutes, because that’s where we have cleaning supplies that I don’t want the dog to get into.