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-multiple.sh
77 lines (66 loc) · 2.45 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
76
77
#!/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"
# 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
# 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
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 -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
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 -h $MQTT_SERVER -p $MQTT_PORT $MQTT_USER_PASS -r $MQTT_ID_OPT -t $MQTT_TOPIC_BASE/$CAM2_NAME -m "ON" &
else
echo "Motion stopped on $CAM2_NAME"
mosquitto_pub -h $MQTT_SERVER -p $MQTT_PORT $MQTT_USER_PASS -r $MQTT_ID_OPT -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 -h $MQTT_SERVER -p $MQTT_PORT $MQTT_USER_PASS -r $MQTT_ID_OPT -t $MQTT_TOPIC_BASE/$CAM3_NAME -m "ON" &
else
echo "Motion stopped on $CAM3_NAME"
mosquitto_pub -h $MQTT_SERVER -p $MQTT_PORT $MQTT_USER_PASS -r $MQTT_ID_OPT -t $MQTT_TOPIC_BASE/$CAM3_NAME -m "OFF" &
fi
fi
done