Multi script mode button

Hi all,

Just thought I’d post my scripts to turn the mode-button on WiFi units into a multi usage button.

There’s two scripts, one called mode-button which is what pressing the mode-button runs, the other is mode-action which has the actions in it and is run by the system scheduler.

#
# mode-button
#
# run by pressing the mode button
#
:global modeCount
:local maxCount 3
:local runDelay 10
:local runDelayFull "00:00:$runDelay"

:set $modeCount ($modeCount + 1)
:if ($modeCount > $maxCount) do={:set $modeCount 0}

:local modeTime ([/system clock get time]+($runDelayFull))

:local modeSchedule [/system scheduler find name=modeAction]
:if ($modeSchedule = "") do={
    /system scheduler add name=modeAction on-event=mode-action start-time=$modeTime
}

#
# mode-action
#
# run by system scheduler
#
:global modeCount

:if ($modeCount = 1) do={
    /system scheduler remove [/system scheduler find name=modeAction]
    :set modeCount 0
    :if ([/system leds settings get all-leds-off] = "immediate") do={
        /system leds settings set all-leds-off=never
    } else={
        /system leds settings set all-leds-off=immediate
    }
}
:if ($modeCount = 2) do={
    /system scheduler remove [/system scheduler find name=modeAction]
    :set modeCount 0
    /system reboot
}
:if ($modeCount = 3) do={
    /system scheduler remove [/system scheduler find name=modeAction]
    :set modeCount 0
    /system shutdown
}

/system scheduler remove [/system scheduler find name=modeAction]

As you can see from the code, a single press within 10 seconds will toggle the LEDs, a double press will reboot, and a triple press will shutdown. It can be modified for shorter or longer time for the button presses, and obviously more actions can be added or even removed if wanted.

I have something very similar in my scripts collection: Mode button with multiple presses
I think it has some advantages and configuration goes to a central script.