Tutorial - LED-scheduling for switches and Route10

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 RGBW top-LED on and off and controlling color and brightness.

A prerequisite seems to be that the LED-light is set to White in the UI, to release the system’s continuous control over RGB channels. 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 R/Red, G/Green, B/Blue and W/White channels (e.g., /sys/class/leds/W/brightness).

It works, at least, on Switch S8 and Route10.

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


#!/bin/sh
#
# Route10 top LED scheduler with colour presets
# Set COLOR_CHOICE to a number (0–9) to pick the colour for ON time.
COLOR_CHOICE=7   # 0=Off 1=Green 2=Turquoise 3=Red 4=Blue 5=White 6=Orange 7=Purple 8=Yellow 9=Off (alias)
# --- Map number to channel values ---
set_color_values() {
  case "$COLOR_CHOICE" in
    0|9) # Off
       LED_R_ON=0;   LED_G_ON=0;   LED_B_ON=0;   LED_W_ON=0 ;;
    1) # Green
       LED_R_ON=0;   LED_G_ON=255; LED_B_ON=0;   LED_W_ON=0 ;;
    2) # Turquoise (cyan)
       LED_R_ON=0;   LED_G_ON=255; LED_B_ON=255; LED_W_ON=0 ;;
    3) # Red
       LED_R_ON=255; LED_G_ON=0;   LED_B_ON=0;   LED_W_ON=0 ;;
    4) # Blue
       LED_R_ON=0;   LED_G_ON=0;   LED_B_ON=255; LED_W_ON=0 ;;
    5) # White
       LED_R_ON=0;   LED_G_ON=0;   LED_B_ON=0;   LED_W_ON=255 ;;
    6) # Orange (red+green mix)
       LED_R_ON=255; LED_G_ON=128; LED_B_ON=0;   LED_W_ON=0 ;;
    7) # Purple (red+blue)
       LED_R_ON=255; LED_G_ON=0;   LED_B_ON=255; LED_W_ON=0 ;;
    8) # Yellow (red+green)
       LED_R_ON=255; LED_G_ON=255; LED_B_ON=0;   LED_W_ON=0 ;;
    *) echo "Unknown color choice $COLOR_CHOICE, defaulting to off"
       LED_R_ON=0; LED_G_ON=0; LED_B_ON=0; LED_W_ON=0 ;;
  esac
}
turn_on_lights() {
    echo "Turning top LED ON (colour $COLOR_CHOICE) at $(date)"
    # ensure manual control
    for c in R G B W; do
        echo none > /sys/class/leds/$c/trigger
    done
    # set colour values
    set_color_values
    echo "$LED_R_ON" > /sys/class/leds/R/brightness
    echo "$LED_G_ON" > /sys/class/leds/G/brightness
    echo "$LED_B_ON" > /sys/class/leds/B/brightness
    echo "$LED_W_ON" > /sys/class/leds/W/brightness
}
turn_off_lights() {
    echo "Turning top LED OFF at $(date)"
    for c in R G B W; do
        echo none > /sys/class/leds/$c/trigger
        echo 0 > /sys/class/leds/$c/brightness
    done
}
LAST_ACTION=""
while true; do
    TIME=$(date +%H%M)
    if [ "$TIME" = "0700" ] && [ "$LAST_ACTION" != "ON_0700" ]; then
        turn_on_lights
        LAST_ACTION="ON_0700"
    elif [ "$TIME" = "2100" ] && [ "$LAST_ACTION" != "OFF_2100" ]; then
        turn_off_lights
        LAST_ACTION="OFF_2100"
    elif [ "$TIME" != "0700" ] && [ "$TIME" != "2100" ]; then
        LAST_ACTION=""
    fi
    sleep 30
done
2 Likes