Schedule LED on/off Unifi controller

As you may or may not know by now, I am passionate Unifi user, and all of my networking equipment is based on their products. Recently, my son told me that LED light that was coming from LR Access Point mounted on the hallway wall is bothering him during night and that he can not sleep.

Of course, that was just lame attempt to not go to sleep 🙂 , but it got me thinking that it would definitely be cool feature to have in Unifi controller. My controller is running in Ubuntu virtual machine in Azure, so this was a case of creating two scripts that would use Cron to run and turn off and on all LED light on all APs.

My controller is set up to control LED status of all devices and devices are set up to obey controllers settings.

After some time on tinkering and searching I came up with viable solution that works, until UBQT exposes it in GUI of the controller. You can vote for this feature here.

Script to turn OFF LED lights:

#!/bin/sh

username=Admin username
password=Admin password
baseurl=https://localhost:8443
site=default
cookie=/tmp/unifi_cookie

curl_cmd="curl --tlsv1 --silent --cookie ${cookie} --cookie-jar ${cookie} --insecure "
${curl_cmd} --data "{'username':'$username', 'password':'$password'}" $baseurl/api/login

${curl_cmd} --data "{ 'led_enabled': false}" $baseurl/api/s/$site/set/setting/mgmt

Script to turn ON LED lights:

#!/bin/sh

username=Admin username
password=Admin password
baseurl=https://localhost:8443
site=default
cookie=/tmp/unifi_cookie

curl_cmd="curl --tlsv1 --silent --cookie ${cookie} --cookie-jar ${cookie} --insecure "
${curl_cmd} --data "{'username':'$username', 'password':'$password'}" $baseurl/api/login

${curl_cmd} --data "{ 'led_enabled': true}" $baseurl/api/s/$site/set/setting/mgmt

Notice the only difference is in line 12, true or false. If you want this to run on your first created site, value for variable site should default. If you want it to run on different site in controller, use site name for value.

I have created two files on Ubuntu and saved them as .sh so they can be executed. Nano is my preferred editor although I have been seen using vi editor in extreme situations :).

Do not forget to use  chmod command so the file can be executed.

chmod +x filename.sh

After that, by simple editing Crontab with

Crontab -e

and adding these lines in it

0 21 * * * /PathToYourFiles/unifi_led_disabled.sh >/dev/null 2>&1
0 8 * * * /PathToYourFiles/unifi_led_enabled.sh >/dev/null 2>&1

I have scheduled LED lights to go off every day at 21:00 and to turn on every day at 08:00.

If you are not familiar with Cron syntax I can recommend you this easy to use online Cron generator. For some of these action you will need root privileges. Script can also be set at user level instead of root which is the recommended way.

Scripts could definitely be modified to turn on or off selected APs and not all of them, but I do not have that many so this works for me. Feel free to comment and add your input.

You May Also Like

About the Author: Marin

Started as trainer and administrator in Algebra, then through some years became head of operating systems department at Algebra private college. At that time, he became IT Pro group lead and 5-year Microsoft MVP. After four years in Microsoft covering roles of Infrastructure and Azure TSP, Marin moved to Span to take the role of Senior Solutions Architect for cloud solutions. Personal time is occupied by enjoying short trips with his family and close friends, tinkering with home automation and networking and spending money on too many gadgets.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.