Skip to content

Instantly share code, notes, and snippets.

@mbierman
Last active November 13, 2023 16:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mbierman/45246bee96a819ca6e49da144bbd4529 to your computer and use it in GitHub Desktop.
Save mbierman/45246bee96a819ca6e49da144bbd4529 to your computer and use it in GitHub Desktop.
Turn on/off lights on Unifi APs
#!/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
@mbierman
Copy link
Author

mbierman commented Mar 20, 2021

Save to /data and set permissions.

cd /data
# save file with your favorite editor (nano or vi)
sudo vi lights.sh
sudo chmod a+x lights.sh
  • Note you have to set the ips of your APs. Best if they have fixed addresses.
  • Requires you install sshpass on Firewalla.
  • Credentials are stored in an external file (private=/home/pi/.firewalla/config/post_main.d/pass.txt) Put your ssh password for your APs on a line by itself.
  • Set "user" to the name of your AP username.
  • To make this reusable, I pass two inputs: The last 3 digits of the AP IP and "0" (off) or "1" (on).
  • set baseIP so that it has the common IP for both APs up to the last 3 dgits.
  • fire this off using cron (~/.firewalla/config/user_crontab). Note after you edit user_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.
8 20  * * *  /data/lights.sh 100 0
6 6   * * *  /data/lights.sh 100 1
10 22 * * *  /data/lights.sh 101 0
6 6   * * *  /data/lights.sh 101 1

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment