Awesome work by you! I only had to make a couple modifications to get it to work for me. First off I installed it on one of the ubiquiti pre-configured UVC-NVR’s which is using an older version of debian so i couldn’t do the systemctl script, but rather did the init script for automatic start at boot. I also am using cloudmqtt as an mqtt broker so I had to add some variables of port, username, and password to the mqtt script. Other than those changes though your instructions were perfect for me.
Haha also found that once it got darker the porch light turning off actually caused another motion event, which in turn caused the light to come back on and it was an infinite loop of on and off. So i created a couple timers. 1st when there is motion triggering the light to come on it also starts a timer of 2 minutes. Once that timer goes from active to idle it triggers another automation to start another 5 second timer, and turn off the light. In my original automation that is turning on the light when it detects motion there is a condition that the 5 second timer can’t be active. This keeps it from automatically coming back on when the light switches off. Really an excellent little system and helps utilize these awesome unifi camera’s full potential.
p.s. I do recommend going into your camera settings and changing the motion sensitivity to a lower setting, and extending the length of time that motion has to be present before triggering an event. Found that dust, and fast flying moth’s were constantly setting off my motion detection.