Help with automation with scene create and restart needed

Hi,

I’ve a a template sensor that reads whether either our back or side door sensor is open.

We have a number of external lights, one of which may or may not be on prior to any automation.

With this in mind I’ve an automation that turns on external lights when the template sensor shows either of doors are open and then waits for the template sensor to show all doors are closed for 2 mins and turns them off.
The external light which may be on independently is controlled by service calling a scene create on trigger to take its status, turn it on and then apply the scene (ie the previous on or off status) at the end.

So the challenge is if I open the door, go outside, close the door and remain outside for less than 2 mins when I open the door to go back inside that second opening triggers the scene create again while the light is still on from initial door opening and means it doesn’t actually revert to pre automation status as intended

i.e. if it was originally off the second door opening within 2 mins recreates the scene with the light on and then applys it at loop close with door closed for 2 mins.

Any ideas to fix this?