[solved] Night LED timer for wifi router for OpenWrt

I have set LEDs to turn off at 9PM and turn on at 3AM everyday, now how can I execute the following commands on every reboot completion but only between the above 2 timings ? :

# echo none > /sys/class/leds/green:lan/trigger
# echo none > /sys/class/leds/green:wan/trigger
# echo none > /sys/class/leds/green:wlan-2/trigger
# echo none > /sys/class/leds/green:wlan-5/trigger
# echo none > /sys/class/leds/green:power/trigger

if the commands work then:

add commands to /etc/rc.local for boot

add commands to yours script leds_off

1 Like

Not a good idea when considering power failures at random times.
You need some logic like check time if 9pm to 3am off leds else on
...
something like:

#!/bin/sh

# Sync time
ntpd -q -p pool.ntp.org

# Check if the current time is between 9pm to 6am
if [[ $(date +%H) -ge 21 || $(date +%H) -lt 6 ]]; then
    /root/leds_off.sh
else
    echo "The current time is not between 9pm to 6am."
fi

save this to /root/boot_led.sh, chmod +x /root/boot_led.sh then add this to rc.local above exit 0

1 Like

No logic for checking time as @maurer pointed out in case of unexpected reboot by me mid time or random power failure

This code looks something like what I need. But before that Can you describe the logic in each lines ? As I am starting to see the positive side of learning shell scripting, and please also link a good article according to you where beginners can learn the needed shell scripting so that others and I can make our own scripts

I think there are enough descriptions for the few commands in the script

you can also use cheatgpt / bing "chat" to query for something like "busybox shell script that syncs time and checks if the time is between 9pm to 6am"

1 Like

Thanks :slightly_smiling_face: . Its much needed help

but ...

Didn't you say it's a bad idea?

1 Like

Actually its a time checking logic issue which your code lacked and @maurer 's code included.

LEDs will definitely turn off between 9PM and 3AM(nighttime period) but will also get turned off from 3AM in the morning till 9PM(daytime period) (which I don't want). As such a time checking code is necessary.

And sorry for my misexplanation, in my earlier reply. Actually the issue is not in power failure but in the time outside of 3AM and 9PM (daytime period)

I know this ...

but what happens if the router is unable to update the time via the internet because lightning struck a branch which destroyed the outgoing connection?

1 Like

For those corner cases the leds will be off
...
Of course the script can be improved with some wan connection checking code or rely on some on-site ntp server but you can never cover all cases...

2 Likes

It happened with me when ISP was down temporarily.

Routors don't have hardware clock running on a separate battery source as present on phones, watches and laptops.

LEDs would be according to the time already set by the system in case it has done a reboot during which its software clock sets to last touch -ed / time-stamped file in /etc/ (according to my knowledge) which is wrong time.

If routor doesn't reboot then software based time is same as the last time the internet was present suring which time synced, so time sync is correct/accurate until routor is manually rebooted in which case the accurate/correct time again gets lost

ok

I bring you my conclusion on the subject, I personally believe that the LEDs always remain off as I don't need an LED to tell me if a router is working, I only turn them on for a few seconds/milliseconds...

example:

cat wifi_up 
#!/bin/sh
echo "1" > /sys/class/leds/green:wlan/brightness
wifi up >/dev/null 2>&1
ping -c 1 192.168.8.2
if [ $? = "0" ]; then
    ssh root@192.168.8.2 /root/wifi_up
    if [ $? = "0" ]; then
       logger "exec: ssh root@192.168.8.2 /root/wifi_up ok"
    else
       logger "exec: ssh root@192.168.8.2 /root/wifi_up ko"
    fi
fi
ping -c 1 192.168.8.5
    if [ $? = "0" ]; then
    ssh root@192.168.8.5 /root/wifi_up
    if [ $? = "0" ]; then
        logger "exec: ssh root@192.168.8.5 /root/wifi_up ok"
    else
    logger "exec: ssh root@192.168.8.5 /root/wifi_up ko"
    fi
fi
sleep 5
echo "timer" > /sys/class/leds/green:wlan/trigger
echo "15" > /sys/class/leds/green:wlan/delay_on
echo "59985" > /sys/class/leds/green:wlan/delay_off

Actually more expensive ones (like turris omnia) does have such an option

2 Likes

Thats interesting, then 'most routors'

1 Like

There is also an option to add gps time sync via usb :slight_smile: https://openwrt.org/docs/guide-user/services/ntp/gps

1 Like

I noticed that this thread has been marked as solved and will close in 10 days. I didn't do it so how did the system auto do ?

I think it would be right to close

there is no additional functionality to add

the LEDs work fine

the rest is just defining the initial state of the LEDs off/on

create scripts and crontabs

the case is already solved from my point of view

2 Likes

I marked by mistake - undid it in the meantime but really we should mark and close this thread

1 Like

I am trying to convert the script to a daemon so that it always checks in background (I believe this is what a daemon/initscript does. Correct me if I am wrong)

A reboot is necessary for just changing LED states. Can this be done when the routor is on itself without reboot, yes through crontab. I researched and got that initscript can also be used which is more user friendly as it has start, stop, restart, enable, disable as parameters so more user friendly control without commands or hassle when modifing for one's custom purpose: routor model or time and is what used by the stock TP-Link Archer firmware.

So is this initscript correctly formed by me and derived from @maurer 's and @elbertmai 's code with addition of my own flavour ? My query is also present in the comment just below the concerned code line :

#!/bin/bash

  start)
    # Sync time
    ntpd -q -p pool.ntp.org
    if [[ $(date +%H) -ge 21 || $(date +%H) -lt 3 ]]; then
      echo none > /sys/class/leds/green:lan/trigger
      echo none > /sys/class/leds/green:wan/trigger
      echo none > /sys/class/leds/green:wlan-2/trigger
      echo none > /sys/class/leds/green:wlan-5/trigger
      echo none > /sys/class/leds/green:power/trigger
      echo "LED triggers modified based on time."
    else
      echo netdev > /sys/class/leds/green:lan/trigger
	  echo br-lan > /sys/class/leds/green:lan/device_name
	  echo 1 > /sys/class/leds/green:lan/link
	  echo 1 > /sys/class/leds/green:lan/tx
	  echo 1 > /sys/class/leds/green:lan/rx
	  
	  echo netdev > /sys/class/leds/green:wan/trigger
	  echo wan > /sys/class/leds/green:lan/device_name
	  echo 1 > /sys/class/leds/green:lan/link
	  echo 1 > /sys/class/leds/green:lan/tx
	  echo 1 > /sys/class/leds/green:lan/rx
	  
	  echo none > /sys/class/leds/green:power/trigger
	  # how to echo default state as 1 to this LED when trigger is set to 'none' ? ; As I don't want to follow the method where I have to set the etc/config/system file with following in comment, I want a command like echo something to some file like we do in trigger file for LEDs:
      # option name 'POWER'
      # option sysfs 'green:power'
      # option trigger 'none'
      # option default '1'
	  
	  echo phy0tpt > /sys/class/leds/green:wlan-2/trigger
	  # how to echo default state as 1 to this LED when trigger is set to 'phy0tpt' ?
	  
	  echo phy1tpt > /sys/class/leds/green:wlan-5/trigger
	  # how to echo default state as 1 to this LED when trigger is set to 'phy1tpt' ?
	  
      echo "LED triggers modified based on time."
    fi

For the my query in the comments of the above code, I was suggested to use this, eg, for power LED : echo 1 > /sys/class/leds/green:power/default
but output is error : -ash: can't create default: Permission denied
so how can I echo for setting default LED state ?