Doggy Door Rule

I am hoping you guys can help me work through the logic of a rule that I am trying to create. I currently have a rule using a global variable that mostly works, but I am trying to refine it to be better. I have a contact sensor, light switch and motion sensor.

Here is what I am trying to accomplish:

Only between Sunset and Sunrise:

  1. Open Patio Door turns on Patio Light to let dog out.
  2. Close Patio Door. Patio Light remains on.
  3. Open Patio Door to let dog in. Patio Light remains on.
  4. Close Patio Door. Patio Light turns off (only if no subsequent motion detected).

Here's the rub. Sometimes we interrupt this sequence by going out onto the patio after step 3-4 instead of letting the dog in. In that case, we do not want the light to turn off while we are out there. I'm trying to figure out how I can use the motion sensor to determine if we are on the patio and cancel turning off the light until the next door close event.

I'm thinking I need to do something like wait for the motion sensor to go inactive after the door closes, then see if it goes active during a certain period of time. If so, then do not turn off the light. I only want the light to turn on/off based on the door sensor. I only need the motion sensor to cancel the turning off of the light.

I hope I am explaining this well. Thanks in advance for any guidance you can provide.

My initial thought is to use 2 rules. I’m thinking Rule Machine, but Basic Rules would probably also work in this case.

Rule 1:
Required Expression: Time between sunset and sunrise
Trigger: Patio Door Open
Action: Turn on Patio light

Rule 2:
Required Expression: Motion: Active
Trigger: Patio Door Open
Action: Wait for Door Closed and Motion Inactive: Duration X minutes

The X minutes duration would need to be adjusted to a setting that works.

Please post your results, and welcome to the community!