Unifi Video Automation triggers


#21

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!


#22

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


#23

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

#24

@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…


#25

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.


#26

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?


#27

@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.

#28

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


#29

Has any of this worked with Unifi Protect?


#30

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.


#31

Contemplating a move to UniFi protect. Any updates with getting this to work?


#32

@Camwise
Looking at getting the motion detection in the cameras trigger a message to be sent to selected devices. Would you like to share how you got the motion sensors to trigger your lights?

I have set up the first part with mqtt reading from the log. Thanks!


#33

I just create a Binary Sensor in Hass for it. Then when it switches from off to on I created an automation to turn on my switch.porch_lights. I then run a 1 minute timer. When that timer is up I start another 10 second timer while I simultaneously turn off the porch light. The automation to turn on the porch light has a condition to not turn on if that 10 second timer is active. This is due to the fact that the porch light turning off triggers another motion event and I needed something to keep it from having the light in a constant on/off loop.

binary_sensor:
  - platform: mqtt
    name: Driveway Motion
    state_topic: "camera/motion/Driveway"

Then my automation looks like this:

  alias: Driveway Motion
  trigger:
  - payload: 'ON'
    platform: mqtt
    topic: camera/motion/Driveway
  condition:
  - condition: state
    entity_id: timer.driveway
    state: idle
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - data:
      entity_id: switch.porch_lights
    service: switch.turn_on
  - data:
      duration: 0:01:00
      entity_id: timer.motion
    service: timer.start

Porch light off automation:

  alias: Driveway Motion Off
  trigger:
  - entity_id: timer.motion
    from: active
    platform: state
    to: idle
  condition:
  - condition: state
    entity_id: binary_sensor.driveway_motion
    state: 'off'
  action:
  - data:
      entity_id: switch.porch_lights
    service: switch.turn_off
  - data:
      entity_id: timer.driveway
    service: timer.start