How to restrict to only one same flow at a time?

So there is an entity “switch.myswitch” and “binary_sensor.mag_iaszone”.

The switch turns on/off the light.
The magnet is attached to a door.

The idea is to allow to turn on the light using the switch manually and turn on the light automatically when door opens by opening it would pressing the switch.

So there are 2 flows.

  1. When switch turns on wait for door to close, then wait for door to open and turn off the light automatically
  2. When door opens (checking if switch is off), switch on the lights, then wait for door to close, then wait for them to open again and turn off the lights.

The issue i run into is that flow 2 gets executed multiple times even though there is already one running, this sometimes results in a situation where person opens the door, the lights turn on and after 1s lights turn off again.

How can i limit to only one same flow running at the same time?

The built-in HomeAssistant automation system has this feature

Post your code please.

Edit: you can simplify some of your logic using the trigger state node, something like this using the add condition:

Then combining the flows into one should be easier.