i changed two things in this awesome script for the following reasons.
First: Because it takes a long time (20+ minutes) before the wifi connected devices get removed from the "clients" list due to a known bug (wifi devices that disconnect, will first be moved to the LAN connected devices before they get removed from the list +- 20 minutes later) i made sure an extra check is build in that checks if the device is wired or not.
If it is not wired, the device is on the WiFi
If it's not in the list or marked as a wired correction it means that the device isn't on WiFi
Code: Select all
for i in "${!cellPhones[@]}"; do
onWifi=$(echo "$var" | head -n 1 | jq -r '.data[] | select(.hostname=='\"$i\"') and select(.is_wired==false)')
if [ ! -z "$onWifi" ]; then
newState="On";
else
newState="Off";
fi
if [ "$newState" != "${lastStates[$i]}" ]; then
lastStates[$i]="$newState"
switchDevice "${cellPhones[$i]}" "$newState";
fi
done
In order to get this to work you need to install "jq"
Second: I don't want my password to be in this script so i created a password file that only root can access.
followed by
In the script i cat the password file because the script started as user root ( sudo crontab -e )
Off course this unifi_passwd.txt file contains my unifi password
Code: Select all
unifi_password=`cat /home/pi/domoticz/scripts/unifi-detect/unifi_passwd.txt`