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:
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: