Tutorial - LED-scheduling for APs

So, inspired by and based on @rutman286’s scripts for link light scheduling for switches and Route10, and while waiting for a UI-driven scheduling, I’m unveiling a script for switching the Blue/White top-LED on and off and control color and brightness for APs.

A prerequisite might be that the LED-light is set to White in the UI, to release the system’s continuous control over the Blue channel channels. I already had it on White and haven’t checked if Blue would prevent this script. If so, there might possibly be a way to get around that too, but I haven’t dug into that to any extent.

It injects brightness levels for the different Blue and White channels (e.g., /sys/class/leds/white/brightness).

It works, at least, on AP6 Pro.

Set COLOR_CHOICE to integer 0-6 according to preferred colour and brightness (or off altogether, via 0, which doesn’t make much sense of course…)


#!/bin/sh
#
# AP LED scheduler with colour presets
# Set COLOR_CHOICE to a number (0–6) to pick the colour for ON
time.
COLOR_CHOICE=2   # 0=Off 1=Blue 2=White 3=Both 4=Dim-blue 5=Dim
-blue+white 6=Dim-white
# --- Map number to blue/white values ---
set_color_values() {
  case "$COLOR_CHOICE" in
    0) # Off
       LED_BLUE_ON=0;   LED_WHITE_ON=0 ;;
    1) # Blue only
       LED_BLUE_ON=255; LED_WHITE_ON=0 ;;
    2) # White only
       LED_BLUE_ON=0;   LED_WHITE_ON=255 ;;
    3) # Both (pale blue)
       LED_BLUE_ON=255; LED_WHITE_ON=64 ;;
    4) # Dim blue
       LED_BLUE_ON=64;  LED_WHITE_ON=0 ;;
    5) # Dim blue + white
       LED_BLUE_ON=64;  LED_WHITE_ON=32 ;;
    6) # Dim white
       LED_BLUE_ON=0;   LED_WHITE_ON=64 ;;
    *) echo "Unknown color choice $COLOR_CHOICE, defaulting to
off"
       LED_BLUE_ON=0; LED_WHITE_ON=0 ;;
  esac
}
turn_on_lights() {
    echo "Turning AP LED ON (colour $COLOR_CHOICE) at $(date)"
    # ensure manual control
    echo none > /sys/class/leds/blue/trigger
    echo none > /sys/class/leds/white/trigger
    # set brightness values
    set_color_values
    echo "$LED_BLUE_ON"  > /sys/class/leds/blue/brightness
    echo "$LED_WHITE_ON" > /sys/class/leds/white/brightness
}
turn_off_lights() {
    echo "Turning AP LED OFF at $(date)"
    echo none > /sys/class/leds/blue/trigger
    echo none > /sys/class/leds/white/trigger
    echo 0 > /sys/class/leds/blue/brightness
    echo 0 > /sys/class/leds/white/brightness
}
LAST_ACTION=""
while true; do
    TIME=$(date +%H%M)
    # turn ON at 06:30
    if [ "$TIME" = "0630" ] && [ "$LAST_ACTION" != "ON_0630" ];
 then
        turn_on_lights
        LAST_ACTION="ON_0630"
    # turn OFF at 19:30
    elif [ "$TIME" = "1930" ] && [ "$LAST_ACTION" != "OFF_1930"
 ]; then
        turn_off_lights
        LAST_ACTION="OFF_1930"
    elif [ "$TIME" != "0630" ] && [ "$TIME" != "1930" ]; then
        LAST_ACTION=""
    fi
    sleep 30


2 Likes

Very cool! Another thing for me to test out when I have a little more free time :sweat_smile:

1 Like