Help controlling a flow loop

Node red experts,

I have a flow that triggers when the back door opens. It then calls a image processor service, then waits 5 seconds checks to see if the door is open and fires the image processor again. this repeats until the door is detected as closed during the five second loop.

the problem is, if the door opens and closes between that 5 second loop another message gets sent and now I have two messages in the loop.

How can I rate limit or kill the current messages once the door closes.

Use a wait-until node that checks if the door closes and continue your flow out the timeout output.