[RESPONDED] Changing power button LED with battery

Hi, i recently discovered ec-tool, it opens a lot of possibilities and one that i like a lot is changing led colors. So i make a little script that would change the power button color depending on battery percentage, it’s pretty short and therefor primitive

#!/bin/bash
running="1"
while [ $running -eq 1 ]
do
	level=$(cat /sys/class/power_supply/BAT1/capacity)
	if [[ $level -gt 70 ]]
	then
		sudo ectool led power green
	elif [[ $level -gt 45 ]] 
	then
		sudo ectool led power white
	elif [[ $level -gt 20 ]] 
	then
		sudo ectool led power yellow
	else
		sudo ectool led power red
	fi
	sleep 10
done

It’s working perfectly along with its .service file:

[Unit]
Description=Changes the color of the power led with battery level

[Service]
User=root
WorkingDirectory=/usr/bin
ExecStart=power-led

[Install]
WantedBy=multi-user.target

but it has one flaw, the light stays on even when the laptop is asleep, would it be possible to make a code to turn the lights off when the lid is closed or something along those lines?

If anyone wants to apply this to their framework computer i’ll make a Github tiny repository

8 Likes

Does sudo ectool led power off work?
Is the question just how to trigger it before sleep?

yup that’s it

Try this for your triggers

[Unit]
Description=Turn off fingerprint led before suspend
Before=suspend.target

[Service]
ExecStart=
Type=oneshot

[Install]
WantedBy=suspend.target

And if needed for resume

[Unit]
Description=Resume fingerprint led battery level after suspend
After=suspend.target

[Service]
ExecStartPre=/usr/bin/sleep 5
ExecStart=
Type=oneshot

[Install]
WantedBy=suspend.target
2 Likes

It works! awesome thanks a lot :slight_smile:

1 Like

if you remember the previous level in a variable and only do the if statements when the new level is not equal to the previous level, it safes running the same command every 10 seconds.

Also you could probably do something like while true on line 3, then you could also remove the running=“1” line

something like this. (i didn´t test if the syntax is correct, but it should give an idea)

#!/bin/bash
previouslevel = 0
while true
do
	level=$(cat /sys/class/power_supply/BAT1/capacity)
	if [ previouslevel -ne level ]
	then
		if [[ $level -gt 70 ]]
		then
			sudo ectool led power green
		elif [[ $level -gt 45 ]] 
		then
			sudo ectool led power white
		elif [[ $level -gt 20 ]] 
		then
			sudo ectool led power yellow
		else
			sudo ectool led power red
		fi
	fi
	previouslevel = level
	sleep 10
done

i understand the concept, good thinking. i tweaked it to make it work, just a few variable re-phrasing.

#!/bin/bash
previouslevel=0 
while true
do
  	level=$(cat /sys/class/power_supply/BAT1/capacity)
        if [[ previouslevel -ne $level ]]
        then

                if [[ $level -gt 70 ]]
                then
                    	sudo ectool led power green
                elif [[ $level -gt 45 ]] 
                then
                    	sudo ectool led power white
                elif [[ $level -gt 20 ]] 
                then
                    	sudo ectool led power yellow
                else
                    	sudo ectool led power red
                fi
        fi
	previouslevel=$level
        sleep 10
done
4 Likes

Nothing to add per se, but yeah, this is was a good call (service).

unfortunately i realized later on that this method blocks rebooting the laptop
when i try to do reboot in the terminal it outputs

Call to Reboot failed: Transaction for reboot.target/start is destructive (power-led-on.service has 'start' job queued, but 'stop' is included in transaction).
Failed to open /run/initctl: Permission denied
Failed to talk to init daemon: Interrupted system call

and when i try to do systemctl stop power-led-on.service i get this output:

Failed to stop power-led-on.service: Transaction for power-led-on.service/stop is destructive (power-led-on.service has 'start' job queued, but 'stop' is included in transaction).
See system logs and 'systemctl status power-led-on.service' for details.

This is the output of ‘systemctl status power-led-on.service’

Oct 18 13:46:15 Framework-12 sudo[5952]: pam_unix(sudo:session): session closed for user root
Oct 18 13:47:16 Framework-12 sudo[6530]:     root : PWD=/ ; USER=root ; COMMAND=/usr/bin/ectool led power green
Oct 18 13:47:16 Framework-12 sudo[6530]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=0)
Oct 18 13:47:16 Framework-12 sudo[6530]: pam_unix(sudo:session): session closed for user root
Oct 18 13:48:26 Framework-12 sudo[7154]:     root : PWD=/ ; USER=root ; COMMAND=/usr/bin/ectool led power green
Oct 18 13:48:26 Framework-12 sudo[7154]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=0)
Oct 18 13:48:26 Framework-12 sudo[7154]: pam_unix(sudo:session): session closed for user root
Oct 18 13:49:35 Framework-12 sudo[8023]:     root : PWD=/ ; USER=root ; COMMAND=/usr/bin/ectool led power green
Oct 18 13:49:35 Framework-12 sudo[8023]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=0)
Oct 18 13:49:35 Framework-12 sudo[8023]: pam_unix(sudo:session): session closed for user root

and without the power-led-on service the light does not turn back on after the computer wakes up

Could it be a problem with the script?