BakSeeDaa wrote: ↑Thursday 31 August 2017 11:42
salvation wrote: ↑Monday 20 October 2014 9:47
......
If someone has tips for improving it let me know.
Hi @salvation and thanks for sharing your script. I like it because it's simple and easy to maintain.
I made some changes to it though and I like to share them here in case it can help someone with similar needs.
I have 2 access points in my house, handled by a single unifi controller but on different sites. So I need the unifi controller bash script to handle multiple sites. I also modified so that the script runs continuously and updates Domoticz devices only if needed. Domoticz doesn't have to call the script. The script will update Domoticz devices (But only if needed)
Site IDs need to be configured in the array:
Code: Select all
declare -A sites=([first_site]=default [second_site]=iw0jsks9)
You can find the site ID in the unifi controller Web GUI if you fiddle around with right click and "inspect" in various places where sites are listed. (Chrome).
There is always a default site and the ID 'default' can be used. If you have more sites you'll need to find the IDs. An ID will look something like
iw0jsks9
Script name
/home/pi/domoticz/scripts/unifi-detect/unifi_detect.sh
Code: Select all
#!/usr/bin/env bash
# We want a single instance of this script
status=`ps -efww | grep -w "[u]nifi_detect.sh" | awk -vpid=$$ '$2 != pid { print $2 }'`
numProcs=( $status )
if [ ${#numProcs[@]} -gt 2 ]; then
echo "[`date`] : unifi_detect.sh : Process is already running: " "$status"
exit 1;
fi
unifi_username=roger
unifi_password=secret123
unifi_controller=https:/UNIFIIP:8443
cookie=/tmp/unifi_cookie
domoticz_ip=username:[email protected]
domoticz_port=8080
declare -A sites=([first_site]=default [second_site]=iw0jsks8)
declare -A cellPhones
cellPhones["Cell Phone Person A"]=domoDevIdxA
cellPhones["Cell Phone Person B"]=domoDevIdxB
cellPhones["Cell Phone Person C"]=domoDevIdxC
cellPhones["Cell Phone Person D"]=domoDevIdxD
declare -A lastStates
curl_cmd="curl --silent --cookie ${cookie} --cookie-jar ${cookie} --insecure "
unifi_login() {
# authenticate against unifi controller
${curl_cmd} --output /dev/null -H "Content-Type: application/json" -X POST -d "{\"password\":\"$unifi_password\",\"username\":\"$unifi_username\"}" $unifi_controller/api/login
}
unifi_logout() {
# logout
${curl_cmd} $unifi_controller/logout
}
switchDevice() {
#echo Checking idx $1 so that it is $2
domoState=$(curl --silent "http://$domoticz_ip:$domoticz_port/json.htm?type=devices&rid=$1")
#echo "$domoState"
if echo "$domoState" | grep -q "\"Status\" : \"On\""; then
oldState=On;
else
oldState=Off;
fi
if [ "$oldState" != "$2" ]; then
echo Switching idx $1 state to $2
curl --silent --output /dev/null "http://$domoticz_ip:$domoticz_port/json.htm?type=command¶m=switchlight&idx=$1&switchcmd=$2"
fi
}
# stat/sta
unifi_list_sta() {
for i in "${!sites[@]}"; do
${curl_cmd} --write-out \\n%{http_code} --data "json={}" $unifi_controller/api/s/${sites[$i]}/stat/sta
done
}
unifi_login
loopCounter=999999
while [ 1 -lt 2 ]; do
((loopCounter++))
if [ "$loopCounter" -gt 240 ]; then
#echo "Resetting the counter"
loopCounter=1
lastStates["Cell Phone Person A"]="Unknown"
lastStates["Cell Phone Person B"]="Unknown"
lastStates["Cell Phone Person C"]="Unknown"
lastStates["Cell Phone Person D"]="Unknown"
fi
#put unifi_list_sta output in variable
var=$(unifi_list_sta)
#echo "$var"
resultCode="${var##*$'\n'}"
for i in "${!cellPhones[@]}"; do
if echo "$var" | grep -q "$i"; then
newState="On";
else
newState="Off";
fi
if [ "$newState" != "${lastStates[$i]}" ]; then
lastStates[$i]="$newState"
switchDevice "${cellPhones[$i]}" "$newState";
fi
done
if [ "$resultCode" != "200" ]; then
echo "Exiting with a result code of : $resultCode"
exit 1
fi
#echo "Let's have some sleep..."
sleep 30
done
unifi_logout
Put this in crontab:
Code: Select all
*/5 * * * * /home/pi/domoticz/scripts/unifi-detect/unifi_detect.sh
I don't need to mention that you need to
Code: Select all
sudo chmod 755 /home/pi/domoticz/scripts/unifi-detect/unifi_detect.sh
do I?
Putting the script in the crontab works like a "watch dog" so that there will always be one instance of the script running.
You're the best!
I was just searching for a better way of checking presence in my system. Before I used Geolocation and Tasker but it's now very unstable due to the use of a OpenVPN connection on the clients (mobile devices).
So I have to integrate a presence detection system from the server to the client and not the other way around.
I will use you script, because i also have 2 ubiquity AP's in my home.
But I have a couple of questions about the script:
#1: where do i put in the MAC adresses of the Mobile devices? couldn't find this in the script
#2: If one of the mobile phones hops to the other AP (takes about a couple of seconds) will this cause the script to set the virtual switch to “off” (away)? Or is there some delay here? how long is this delay? same as seen in the topic about 5 minutes?
#3: what happens if both mobile devices are switched off from the WIFI, but still in reach for the Access points?
Explanation: Sometimes we have a downtime in the internet connection to the Internet Serice Provides. And when this happens and we are at home, we switch to the mobile internet connection. And switched off from the WIFI.
#4: An other solution to the issue by Question 3 is to do the Switch Off by the Geofence, and the Switch “on” by your Script. What part do I have to disable in your script to let the script only set the Virtual Switch to “On”