Timer turn off light only if the door sensor didn't turn light on

Looking for help with a certain process…here is what it needs to do:

Have these three smart items:

  1. Door sensor

  2. Wall-mounted momentary switch

  3. Outside smart light with always-on power

And these two things work:

  • Can turn on the light with the wall switch.

  • Door sensor can turn on light based on sun position – only triggers the light if it is dark (or dark-ish) outside.

What we need:

  • Automatically turn off the outside light after 2 minutes, but ONLY if it was turned on by the door sensor. I can do the first part but not the second part.

  • If the light was turned on from the door sensor and the door sensor is triggered again before the 2 minutes have finished, then the timer should restart the 2 minute timer. I can do this part.

  • If the light is already on from the wall switch, the door sensor needs to not do anything, including not starting any turn-off timer. Don’t know how to tell how the light was turned on.

  • The wall switch can turn off the light regardless of how the light is turned on. I can do this.

One way I tried to solve this was with Node-RED. This worked well for the timer part. Have a node that will easily do the timer that will reset if the door sensor triggers again. Just don’t know how to run the timer conditional based on how the light originally turned on. I don’t want someone to turn the patio light, go outside to do something for a bit, but the door sensor is triggered, turning on the timer and shutting off the light after 2 minutes.

I’d prefer not to do this in YAML… have tried to understand YAML’s logic, but you have to know a bunch of unforgiving settings without knowing what the possible options are and no automated prompts to help along the way. This seems like it shouldn’t be that difficult. The HA Visual Editor is okay for some stuff, but I don’t see how to make that one condition work (about how to remember how the light was turned on).

These are truly first-world problems.

Setup 2 triggers in the UI.

  1. for the state of the light, with a state of on, press the 3 dots and enter a trigger_id eg “light_turned_on”
  2. setup a trigger for the state of the door sensor becoming on, press the 3 dots and choose to add a trigger id “door_sensor_on”

In the actions of the automation, you can then use if or choose to be able to check if the trigger was “door_sensor_on” and if it’s not, then you can basically stop the automation, because the light was turned on manually.