Unifi Video Automation triggers

I came up with a python script that I run as a shell_command when a motion detector (z-wave) is tripped, but I think that’s the reverse direction from what you’re looking for.

I was looking into this and I found that Unifi Video writes to a log file in /var/log/unifi-video/motion.log

If we could have a script to tail this file in realtime and then push motion events to MQTT it could work?

example:

1529103489.838 2018-06-15 18:58:09.838/EDT: INFO   Camera[F0xxxxxxxxxx] type:start event:4065 clock:3601035892 (Front Door) in ApplicationEvtBus-14
1529103519.809 2018-06-15 18:58:39.809/EDT: INFO   Camera[F0xxxxxxxxxx] type:stop event:4065 clock:3601065845 (Front Door) in ApplicationEvtBus-17
1529103562.699 2018-06-15 18:59:22.699/EDT: INFO   Camera[F0xxxxxxxxxx] type:start event:4066 clock:3601108687 (Front Door) in ApplicationEvtBus-7
1529103623.339 2018-06-15 19:00:23.339/EDT: INFO   Camera[F0xxxxxxxxxx] type:stop event:4066 clock:3601169338 (Front Door) in ApplicationEvtBus-7
1529103747.788 2018-06-15 19:02:27.788/EDT: INFO   Camera[F0xxxxxxxxxx] type:start event:4067 clock:3601293800 (Front Door) in ApplicationEvtBus-2
1529103789.536 2018-06-15 19:03:09.536/EDT: INFO   Camera[F0xxxxxxxxxx] type:stop event:4067 clock:3601335598 (Front Door) in ApplicationEvtBus-3

This looks even better to modify, I’ll give it a go and see if I can get it working with unifi video

Ok, I’ve gotten something to work but might need some more enhancements…

2 Likes

How did you set it up in the home assistant to properly catch the publish and handle the notifications?

d3vilb0y, Just follow the mqtt binary sensor component guide.

I spent about an hour playing with this last night and it works awesome! Did a little automation that when my driveway camera detects motion to turn on the porch light.
cameramotion

Thank you mzac for your work on this. Been waiting for something like this for like a year. LOL, just bought some esp8266 chips and PIR sensors to build my own motion detector unit, but couldn’t figure out a pretty way to set it up on the outside of my house that my wife would be ok with. Now I don’t have to.

1 Like

Wow awesome!!

1 Like

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.

1 Like

Got it working now, the debian package of mosquitto_pub is not yet able to specify version as in the ubuntu version (when you try and runt he integrated broker), got disconnected the whole time. so instead i went for the cloudmqtt, cheers for mentioning it Camwise!

1 Like

Thanks @mzac ! Anyone get this working for more than 1 camera? I have it working for 1 but I have 3 others I’d like it to work for and it doesn’t seem to parse for the other ones…

I’ll see what I can do to write up another copy of the script for multiple cameras and post it in my github repo. I’ll let you know when it’s done!

See if this works for you, of course you’ll have to modify the camera names and IDs at the top of the script:

1 Like

Thanks @mzac! I was trying to hard to get it down to 3 lines, must remember to follow the KIS principle :wink:

For anyone interested in using non-local MQTT you can use the following:

MQTT_SERVER="yyy.xxx.com"
MQTT_PORT="12345"
MQTT_TOPIC_BASE="whatever"
if echo $LAST_CAM | grep -n1 $CAM1_ID; then
# Camera 1 triggered
    if [[ $LAST_EVENT == "start" ]]; then
       echo "Motion started on $CAM1_NAME"
       mosquitto_pub -h $MQTT_SERVER -p $MQTT_PORT $MQTT_USER_PASS -r -t $MQTT_TOPIC_BASE/$CAM1_NAME -m "ON" &
     else
       echo "Motion stopped on $CAM1_NAME"
       mosquitto_pub -h $MQTT_SERVER -p $MQTT_PORT $MQTT_USER_PASS -r -t $MQTT_TOPIC_BASE/$CAM1_NAME -m "OFF" &
     fi

fi

and the auth section had an error that needed to be fixed on my system…

 if [[ -n "$MQTT_USER" && -n "$MQTT_PASS" ]]; then
   MQTT_USER_PASS="-u $MQTT_USER -P $MQTT_PASS"
 else
   MQTT_USER_PASS=""
 fi

@Camwise care to share on exactly how you got your script to auto-run on the NVR? Definitely not my area of expertise :slight_smile: I currently have the unifi-video-mqtt.sh just running from the command prompt for testing but I’d like to push it into production for full time use…

Hey pjo,

I’m no expert either. Spent a crap ton of time reading different things, and feel like i’m 50% of the way there. I copied the unifi-video-mqtt.sh script into the init.d directory. Then run the following command:
sudo update-rc.d unifi-video-mqtt.sh defaults

This starts up the script on boot of the nvr, but after a couple days it stops running. I think we need to create an init.d service instead of simply running the script this way, but i don’t know how. Haha I’m a hobbyist and that’s it on my skills. Assume in the service there would be some type of restart option in it.

Hi, I’m looking for something like that. But I’ve run my unfivideo NVR on a windows machine … Is there any way to make it work? Some mqttt client for windows, who can read the folder?

@mzac

I need to ask, I use username and password.
But when I fill it in the unifi-vide-mqtt.sh it gives me this when i run it:

./unifi-video-mqtt.sh: line 22: =-u myrealusername -P myrealpassword: command doesn't exist.

@kernehed did you use the chmod command on your script?

Has any of this worked with Unifi Protect?

Thank you for this.

I modified the Dockerfile and script to get this working on a QNAP which is also running my NVR Container but all working well.