This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
unifi-video-mqtt.sh
63 lines (51 loc) · 1.81 KB
/
unifi-video-mqtt.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
# Unifi Video Vars
UNIFI_MOTION_LOG=/var/log/unifi-video/motion.log
# MQTT Vars
MQTT_SERVER="192.168.x.x"
MQTT_PORT="1883"
MQTT_TOPIC_BASE="camera/motion"
# MQTT User/Pass Vars, only use if needed
#MQTT_USER="username"
#MQTT_PASS="password"
#MQTT_ID="yourid" ## To make it work with hassio
# Camera Defs
CAM1_NAME="camera_name"
CAM1_ID="F0xxxxxxxxxx"
# --------------------------------------------------------------------------------
# Script starts here
# Check if a username/password is defined and if so create the vars to pass to the cli
if [[ -n "$MQTT_USER" && -n "$MQTT_PASS" ]]; then
MQTT_USER_PASS="-u $MQTT_USER -P $MQTT_PASS"
else
MQTT_USER_PASS=""
fi
# Check if a MQTT_ID has been defined, needed for newer versions of Home Assistant
if [[ -n "$MQTT_ID" ]]; then
MQTT_ID_OPT="-I $MQTT_ID"
else
MQTT_ID_OPT=""
fi
# Check for version of log file, the format changed in Unifi Video 3.10
VER_TEST=`tail -1 $UNIFI_MOTION_LOG | awk {'print $5'}`
while inotifywait -e modify $UNIFI_MOTION_LOG; do
LAST_MESSAGE=`tail -n1 $UNIFI_MOTION_LOG`
if [[ $VER_TEST == "[uv.analytics.motion]" ]]; then
# New Format
LAST_CAM=`echo $LAST_MESSAGE | awk -F '[][]' '{print $6}' | cut -d '|' -f 1`
else
# Old Format
LAST_CAM=`echo $LAST_MESSAGE | awk -F '[][]' '{print $2}'`
fi
LAST_EVENT=`echo $LAST_MESSAGE | cut -d ':' -f 5 | cut -d ' ' -f 1`
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 $MQTT_ID_OPT -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 $MQTT_ID_OPT -t $MQTT_TOPIC_BASE/$CAM1_NAME -m "OFF" &
fi
fi
done