Last active
November 13, 2023 16:27
-
-
Save mbierman/45246bee96a819ca6e49da144bbd4529 to your computer and use it in GitHub Desktop.
Turn on/off lights on Unifi APs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Variables | |
result="" | |
count=0 | |
log=/data/logs/lights.log | |
private=/home/pi/.firewalla/config/post_main.d/pass.txt | |
dir=$(dirname "$0") | |
user=$(cat $dir/lights.txt | grep user | cut -f2 -d "=" ) | |
baseIP=192.168.0 | |
lightpath=/proc/gpio/led_pattern | |
days=150 # this saves 30 days of logs based on 2 APs turned on/off 2x/day | |
sleep=10 | |
# test for input | |
if [ -z "$2" ]; then | |
echo "Two parapeters are required: last three digits of IP and [0|1] in that order. For example:\n\n" $(basename $0) " 100 1" | |
exit 1 | |
fi | |
ip=$1 | |
# These are the "friendly" names of the APs so the logs are more readable. | |
if [ "$1" -eq "100" ]; then | |
AP="Bedroom AP" | |
elif [ "$1" -eq "101" ]; then | |
AP="Living room AP" | |
fi | |
ask=$2 | |
if [ "$ask" -eq "0" ]; then | |
state=OFF | |
elif [ "$ask" -eq "1" ]; then | |
state=ON | |
fi | |
logClean () { | |
echo "$(tail -n $days $log)" > $log | |
} | |
logClean | |
cont () | |
{ | |
/usr/bin/sshpass -f $private ssh $user@$baseIP.$ip \ | |
"echo '$ask'>$lightpath" && command=$(echo $(date +"%a %b %d %H:%M:%S") $AP: $state ); echo $command | |
echo "waiting $sleep seconds to test..." | |
sleep $sleep | |
} | |
testit () | |
{ | |
result=$(/usr/bin/sshpass -f $private ssh $user@$baseIP.$ip \ | |
"cat $lightpath") | |
count=`expr $count + 1` | |
} | |
while [ "$ask" != "$result" ]; do | |
cont | |
testit | |
done | |
if [ "$result" -eq "0" ]; then | |
result="OFF" | |
elif [ "$result" -eq "1" ]; then | |
result="ON" | |
fi | |
echo $result | |
echo $command | tee -a $log | |
echo $(date +"%a %b %d %H:%M:%S") [Result] $AP: $result \($count\) | tee -a $log | |
echo ================================== >> $log | |
logClean |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Save to /data and set permissions.
~/.firewalla/config/user_crontab
). Note after you edituser_crontab
you have to restart Firewalla for it to take effect. see https://crontab-generator.org/ for help with the cron syntax. Making a mistake can cause issues with Firewalla.I moved the Unifi username to a local file (defaults to same directory you run the script from lights.txt
user=unifi username here
Important note: tell your controller you want the LED lights on. I saw that if they were off on the controller I could turn them on, but they might turn off unexpectedly. Telling the Controller leave them on seems to allow this method of controlling them on a schedule.