[solved] Night LED timer for wifi router for OpenWrt

Ok i am checking, but why did you use \ between green and :lan or :wan. Using \ and not using it, gives different results, at one time 1 other time 0 . Like here:

  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 OpenWrt 23.05.2, r23630-842932a63d
 -----------------------------------------------------
root@openwrt:~# cd /sys/class/leds/
root@openwrt:/sys/class/leds# ls
amber:wan     green:lan     green:power   green:wan     green:wlan-2  green:wlan-5  mt76-phy0     mt76-phy1
root@openwrt:/sys/class/leds# cd green:lan
root@openwrt:/sys/class/leds/green:lan# ls
brightness      device_name     link            rx              trigger         uevent
device          interval        max_brightness  subsystem       tx
root@openwrt:/sys/class/leds/green:lan# cd bin
-ash: cd: can't cd to bin: No such file or directory
root@openwrt:/sys/class/leds/green:lan# cd
root@openwrt:~# cd bin
-ash: cd: can't cd to bin: No such file or directory
root@openwrt:~# ls
leds_off  leds_on   trigger?
root@openwrt:~# cd /
root@openwrt:/# ls
bin      etc      mnt      proc     root     sys      usr      www
dev      lib      overlay  rom      sbin     tmp      var
root@openwrt:/# cd bin
root@openwrt:/bin# ls
ash              date             grep             mkdir            passwd           sh               uclient-fetch
board_detect     dd               gunzip           mknod            pidof            sleep            umount
busybox          df               gzip             mktemp           ping             sync             uname
cat              dmesg            ipcalc.sh        mount            ping6            tar              vi
chgrp            echo             kill             mv               ps               touch            zcat
chmod            egrep            ln               netmsg           pwd              traceroute
chown            false            lock             netstat          rm               traceroute6
config_generate  fgrep            login            nice             rmdir            true
cp               fsync            ls               opkg             sed              ubus
root@openwrt:/bin# cd echo
-ash: cd: can't cd to echo: Not a directory
root@openwrt:/bin# cd
root@openwrt:~# cd
root@openwrt:~# cat /sys/class/leds/green\:lan/brightness
1
root@openwrt:~# cat /sys/class/leds/green:lan/brightness
0
root@openwrt:~# cat /sys/class/leds/green\:lan/brightness
1
root@openwrt:~# 

Yes it worked each time I executed the 4 variations of your command as below :

  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 OpenWrt 23.05.2, r23630-842932a63d
 -----------------------------------------------------
root@openwrt:~# /bin/echo "0" > /sys/class/leds/green\:lan/brightness
root@openwrt:~# /bin/echo "1" > /sys/class/leds/green\:lan/brightness
root@openwrt:~# /bin/echo "0" > /sys/class/leds/green:lan/brightness
root@openwrt:~# /bin/echo "1" > /sys/class/leds/green:lan/brightness
root@openwrt:~#

alright (and happy new year!) let's try an approach based on my working commands and @elbertmai 's scripts:

  1. Write this script and store in /root/leds_on:
#!/bin/sh
/bin/echo "1" > /sys/class/leds/green:lan/brightness
/bin/echo "1" > /sys/class/leds/green:wan/brightness
  1. Write this script and store in /root/leds_off:
#!/bin/sh
/bin/echo "0" > /sys/class/leds/green:lan/brightness
/bin/echo "0" > /sys/class/leds/green:wan/brightness
  1. Do chmod +x on both scripts.
  2. Verify that the scripts do what you want by directly invoking them in SSH.
  3. In the LuCI interface, go to System → Scheduled Tasks and enter your desired crontab entries. Look up "cron syntax" with your favorite search engine to learn more. For example, to turn on LEDs at 6:00 AM and turn off LEDs at 9:00 PM:
0 6 * * * /root/leds_on
0 21 * * * /root/leds_off

as for \ is escaping special characters like : being interpreted as commands instead of file/folder names as we want it - google it up

1 Like

Happy new year to you and everyone! Updating you after checking

Now again an issue as below of permission denied :

  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 OpenWrt 23.05.2, r23630-842932a63d
 -----------------------------------------------------
root@openwrt:~# ls
leds_off  leds_on   trigger?
root@openwrt:~# rm trigger?
root@openwrt:~# ls
leds_off  leds_on
root@openwrt:~# ls
leds_off     leds_on      ledsoff2.sh  ledson2.sh
root@openwrt:~# ls
leds_off     leds_on      ledsoff2.sh  ledson2.sh
root@openwrt:~# cat /sys/class/leds/green:lan/brightness
1
root@openwrt:~# cat /sys/class/leds/green:wan/brightness
1
root@openwrt:~# ./ledsoff2.sh
-ash: ./ledsoff2.sh: not found
root@openwrt:~# sh ledsoff2.sh
: Permission denied: can't create /sys/class/leds/green:lan/brightness
root@openwrt:~# chmod +x ledsoff2.sh
root@openwrt:~# chmod +x ledson2.sh
root@openwrt:~# sh ledsoff2.sh
: Permission denied: can't create /sys/class/leds/green:lan/brightness
root@openwrt:~# cd /
root@openwrt:/# sh ledsoff2.sh
sh: can't open 'ledsoff2.sh': No such file or directory
root@openwrt:/# cd
root@openwrt:~# sh ledsoff2.sh
: Permission denied: can't create /sys/class/leds/green:lan/brightness
root@openwrt:~# sh ledson2.sh
: Permission denied can't create /sys/class/leds/green:lan/brightness
root@openwrt:~#

I inferred that I can execute all commands individually from shell also echo none > trigger which turn off leds too as before as seen in below 2nd output but not from script file which again brings us back to initial issue of script file not executing commands :

root@openwrt:~# cd /sys/class/leds/green:wan/brightness
-ash: cd: can't cd to /sys/class/leds/green:wan/brightness: Not a directory
root@openwrt:~# cd /sys/class/leds/green:wan
root@openwrt:/sys/class/leds/green:wan# ls
brightness      device          max_brightness  subsystem       trigger         uevent
root@openwrt:/sys/class/leds/green:wan# cat trigger
[none] timer heartbeat default-on netdev mt7530-0:00:link mt7530-0:00:1Gbps mt7530-0:00:100Mbps mt7530-0:00:10Mbps mt7530-0:01:link mt7530-0:01:1Gbps mt7530-0:01:100Mbps mt7530-0:01:10Mbps mt7530-0:02:link mt7530-0:02:1Gbps mt7530-0:02:100Mbps mt7530-0:02:10Mbps mt7530-0:03:link mt7530-0:03:1Gbps mt7530-0:03:100Mbps mt7530-0:03:10Mbps mt7530-0:04:link mt7530-0:04:1Gbps mt7530-0:04:100Mbps mt7530-0:04:10Mbps phy0rx phy0tx phy0assoc phy0radio phy0tpt phy1rx phy1tx phy1assoc phy1radio phy1tpt
root@openwrt:/sys/class/leds/green:wan# echo none > trigger
root@openwrt:/sys/class/leds/green:wan# cat trigger
[none] timer heartbeat default-on netdev mt7530-0:00:link mt7530-0:00:1Gbps mt7530-0:00:100Mbps mt7530-0:00:10Mbps mt7530-0:01:link mt7530-0:01:1Gbps mt7530-0:01:100Mbps mt7530-0:01:10Mbps mt7530-0:02:link mt7530-0:02:1Gbps mt7530-0:02:100Mbps mt7530-0:02:10Mbps mt7530-0:03:link mt7530-0:03:1Gbps mt7530-0:03:100Mbps mt7530-0:03:10Mbps mt7530-0:04:link mt7530-0:04:1Gbps mt7530-0:04:100Mbps mt7530-0:04:10Mbps phy0rx phy0tx phy0assoc phy0radio phy0tpt phy1rx phy1tx phy1assoc phy1radio phy1tpt
root@openwrt:/sys/class/leds/green:wan# echo netdev > trigger
root@openwrt:/sys/class/leds/green:wan# cat trigger
none timer heartbeat default-on [netdev] mt7530-0:00:link mt7530-0:00:1Gbps mt7530-0:00:100Mbps mt7530-0:00:10Mbps mt7530-0:01:link mt7530-0:01:1Gbps mt7530-0:01:100Mbps mt7530-0:01:10Mbps mt7530-0:02:link mt7530-0:02:1Gbps mt7530-0:02:100Mbps mt7530-0:02:10Mbps mt7530-0:03:link mt7530-0:03:1Gbps mt7530-0:03:100Mbps mt7530-0:03:10Mbps mt7530-0:04:link mt7530-0:04:1Gbps mt7530-0:04:100Mbps mt7530-0:04:10Mbps phy0rx phy0tx phy0assoc phy0radio phy0tpt phy1rx phy1tx phy1assoc phy1radio phy1tpt
root@openwrt:/sys/class/leds/green:wan# echo timer > trigger
root@openwrt:/sys/class/leds/green:wan# cat trigger
none [timer] heartbeat default-on netdev mt7530-0:00:link mt7530-0:00:1Gbps mt7530-0:00:100Mbps mt7530-0:00:10Mbps mt7530-0:01:link mt7530-0:01:1Gbps mt7530-0:01:100Mbps mt7530-0:01:10Mbps mt7530-0:02:link mt7530-0:02:1Gbps mt7530-0:02:100Mbps mt7530-0:02:10Mbps mt7530-0:03:link mt7530-0:03:1Gbps mt7530-0:03:100Mbps mt7530-0:03:10Mbps mt7530-0:04:link mt7530-0:04:1Gbps mt7530-0:04:100Mbps mt7530-0:04:10Mbps phy0rx phy0tx phy0assoc phy0radio phy0tpt phy1rx phy1tx phy1assoc phy1radio phy1tpt
root@openwrt:/sys/class/leds/green:wan# echo 0 > trigger
ash: write error: Invalid argument
root@openwrt:/sys/class/leds/green:wan# echo default-on > trigger
root@openwrt:/sys/class/leds/green:wan# echo none > trigger
root@openwrt:/sys/class/leds/green:wan# cat trigger
[none] timer heartbeat default-on netdev mt7530-0:00:link mt7530-0:00:1Gbps mt7530-0:00:100Mbps mt7530-0:00:10Mbps mt7530-0:01:link mt7530-0:01:1Gbps mt7530-0:01:100Mbps mt7530-0:01:10Mbps mt7530-0:02:link mt7530-0:02:1Gbps mt7530-0:02:100Mbps mt7530-0:02:10Mbps mt7530-0:03:link mt7530-0:03:1Gbps mt7530-0:03:100Mbps mt7530-0:03:10Mbps mt7530-0:04:link mt7530-0:04:1Gbps mt7530-0:04:100Mbps mt7530-0:04:10Mbps phy0rx phy0tx phy0assoc phy0radio phy0tpt phy1rx phy1tx phy1assoc phy1radio phy1tpt
root@openwrt:/sys/class/leds/green:wan#

alright let's try exactly as it works for me:
fill /etc/crontabs/root with (adjusting the times per your needs):

# enable all LEDs at 7am
0      7      *       *       *         /bin/echo "1" > /sys/class/leds/green:lan/brightness ; /bin/echo "1" > /sys/class/leds/green:wan/brightness
# disable all LEDs at 9pm
0      21    *       *       *        /bin/echo "0" > /sys/class/leds/green:lan/brightness ; /bin/echo "0" > /sys/class/leds/green:wan/brightness

hopefully that should do it

1 Like

Can you explain how?

1 Like

I did some variation of my own in crontab as already told by @maurer and its working, still last 2 problems left to solve after careful observation :

# Reboot at 12:00am every day
# Note: To avoid infinite reboot loop, wait 70 seconds
# and touch a file in /etc so clock will be set
# properly to 12:01am on reboot before cron starts.
0 0 * * * sleep 70 && touch /etc/banner && reboot
#
# enable all LEDs at 4am
0 4 * * * /bin/echo "1" > /sys/class/leds/green:lan/brightness ; /bin/echo "1" > /sys/class/leds/green:wan/brightness ; /bin/echo "1" > /sys/class/leds/green:power/brightness ; /bin/echo "1" > /sys/class/leds/green:wlan-2/brightness ; /bin/echo "1" > /sys/class/leds/green:wlan-5/brightness
# disable all LEDs at 9pm
0 21 * * * /bin/echo "0" > /sys/class/leds/green:lan/brightness ; /bin/echo "0" > /sys/class/leds/green:wan/brightness ; /bin/echo "0" > /sys/class/leds/green:power/brightness ; /bin/echo "0" > /sys/class/leds/green:wlan-2/brightness ; /bin/echo "0" > /sys/class/leds/green:wlan-5/brightness

A) A/c to my settings in crontab all 5 leds turn off at 9 pm, system reboot occurs at 12 am, and finally leds turn on at 3 am (but at this step, after turning on, all 5 leds' state are continuous on instead of the setting done for them which is blinking state according to link, tx and rx activity. Note that this is my 1st problem)

B) Secondly, I have a logical query as my 2nd problem. If all 5 leds are turned off at 9 pm, then will they still be kept off after system reboot completes at 12 am ?

"1

Just go to LED settings in luci, and select all of the LEDs one by one and select off.

1 Like

:sweat_smile:
Its funny

I mistook the word "completely" for "Permanently" - lol, OK yes.

I thought you meant something other than a system setting.

1 Like

For point 1 you need the script in post 3
For point 2 - no the leds will be on (defaults) so you need to issue another off command let's say 5minutes after the router rebooted

1 Like

Why though it works exactly as intended?

1 Like

What is post 3 ?

Thanks, this is better than having nothing but its a little less absolute trick than my original tp link stock firmware where leds turned off immediately after reboot completed without any confusion of how to implement led turn off with absolute timing without defining any timings.

It works as intended but not exactly. But better than having nothing. Trying if the commands can be polished more to be absolute and as I exactly intend.

Sorry, I didn't mean that in a negative sense. I've had people "helpfully" withhold important information when asking for help (which of course is frustrating). But I realize that it sounds accusatory after a second read, so I removed that bit in my reply since that isn't my intention.

I still don't know why cd doesn't work here, but it appears just using the absolute path works.

When you turn off the LEDs, the kernel erases the trigger state. If you want to restore the blinkenlights you also need to also write the tx, rx, link, and device_name files in addition to writing netdev to trigger.

1 Like

Thank you for taking time to explain to a noob like me. And don't be sorry because I didn't take it negatively. You mentioned you doubt and I clarified it. Its just that. And also I understand you position where someone is trying to help someone who is knowingly or unknowingly withholding info which makes matter frustrating to help further so don't feel that I am taking it negatively. I understand that and thanks for helping me

Not 3 but

Well we can implement this in openwrt as well.
I suggest to open a new thread and I'm sure someone with more time currently (kids vacation time) can help you

1 Like

That is post 3. ok. And, Thanks I will try to open a new thread soon.

Finally I found reason for this error of " can't find cd to directory..." coming when executing script over ssh and now I can execute scripts over ssh to continue forward to solve LED timing feature.
Look here in these 2 photos :

Script created in Windows Notepad & viewed in Windows Notepad :
Screenshot 2024-01-03 231357

Script created in Windows Notepad & viewed in Linux Vim :

I then edited the script in Linux Vim by omitting " ^M " from end of each line, saved it and then it successfuly executed over ssh.

So, remember this note for future newbies, Windows Notepad disrupts formatting of scripts and appends unwanted characters

I am trying the scripts and updating any coming errors soon

1 Like