forked from mzac/unifi-video-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunifi-video-mqtt-multiple.sh
75 lines (64 loc) · 2.21 KB
/
unifi-video-mqtt-multiple.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
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
# Unifi Video Vars
UNIFI_MOTION_LOG=/var/log/unifi-video/motion.log
# MQTT Vars
MQTT_SERVER="192.168.x.x"
#MQTT_PORT="123"
MQTT_TOPIC_BASE="camera/motion"
# MQTT User/Pass Vars, only use if needed
#MQTT_USER="username"
#MQTT_PASS="password"
# Camera Defs
CAM1_NAME="camera1_name"
CAM1_ID="F0xxxxxxxxxx"
CAM2_NAME="camera2_name"
CAM2_ID="F0xxxxxxxxxx"
CAM3_NAME="camera3_name"
CAM4_ID="F0xxxxxxxxxx"
# --------------------------------------------------------------------------------
# Script starts here
if [[ -n "$MQTT_PORT" ]]; then
MQTT_SERVER_AND_PORT="-h $MQTT_SERVER -p $MQTT_PORT"
else
MQTT_SERVER_AND_PORT="-h $MQTT_SERVER"
fi
if [[ -n "$MQTT_USER" && -n "$MQTT_PASS" ]]; then
MQTT_USER_PASS="-u $MQTT_USER -P $MQTT_PASS"
else
MQTT_USER_PASS=""
fi
while inotifywait -e modify $UNIFI_MOTION_LOG; do
LAST_MESSAGE=`tail -n1 $UNIFI_MOTION_LOG`
LAST_CAM=`echo $LAST_MESSAGE | awk -F '[][]' '{print $2}'`
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 $MQTT_SERVER_AND_PORT $MQTT_USER_PASS -r -t $MQTT_TOPIC_BASE/$CAM1_NAME -m "ON" &
else
echo "Motion stopped on $CAM1_NAME"
mosquitto_pub $MQTT_SERVER_AND_PORT $MQTT_USER_PASS -r -t $MQTT_TOPIC_BASE/$CAM1_NAME -m "OFF" &
fi
fi
if echo $LAST_CAM | grep -n1 $CAM2_ID; then
# Camera 2 triggered
if [[ $LAST_EVENT == "start" ]]; then
echo "Motion started on $CAM2_NAME"
mosquitto_pub $MQTT_SERVER_AND_PORT $MQTT_USER_PASS -r -t $MQTT_TOPIC_BASE/$CAM2_NAME -m "ON" &
else
echo "Motion stopped on $CAM2_NAME"
mosquitto_pub $MQTT_SERVER_AND_PORT $MQTT_USER_PASS -r -t $MQTT_TOPIC_BASE/$CAM2_NAME -m "OFF" &
fi
fi
if echo $LAST_CAM | grep -n1 $CAM3_ID; then
# Camera 3 triggered
if [[ $LAST_EVENT == "start" ]]; then
echo "Motion started on $CAM3_NAME"
mosquitto_pub $MQTT_SERVER_AND_PORT $MQTT_USER_PASS -r -t $MQTT_TOPIC_BASE/$CAM3_NAME -m "ON" &
else
echo "Motion stopped on $CAM3_NAME"
mosquitto_pub $MQTT_SERVER_AND_PORT $MQTT_USER_PASS -r -t $MQTT_TOPIC_BASE/$CAM3_NAME -m "OFF" &
fi
fi
done