Wait until "FOR" but in the middle of the Node RED flow

I did not look at your code to troubleshoot but there is a good discussion about Motion activated light automation here:

Specifically, you might want to try this Node-Red flow: