[SOLVED] Possible to modify lock screen to show battery percentage?
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
-
- Level 3
- Posts: 151
- Joined: Thu Mar 12, 2020 3:50 pm
[SOLVED] Possible to modify lock screen to show battery percentage?
I've looked around but couldn't find anything to this -- could very well be I didn't look in the right places.
Whenever I have my Mac plugged in to charge the battery, the lock screen is visible whenever I don't actively use it. I appreciate that there is an icon in the upper right corner of the lock screen indication that the battery is being charged -- but I am looking for a way to get the percentage showing. That way I could make a guess how much longer it needs to charge without having to unlock the system.
Can anyone point me in the right direction or the solution?
Thanks!!
Whenever I have my Mac plugged in to charge the battery, the lock screen is visible whenever I don't actively use it. I appreciate that there is an icon in the upper right corner of the lock screen indication that the battery is being charged -- but I am looking for a way to get the percentage showing. That way I could make a guess how much longer it needs to charge without having to unlock the system.
Can anyone point me in the right direction or the solution?
Thanks!!
Last edited by SilenceIsG0lden on Thu Feb 01, 2024 7:36 pm, edited 1 time in total.
LM 21.1 cinnamon on PC built by myself in 2008
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
Re: Possible to modify lock screen to show battery percentage?
I did a search and the only thing I can find is an applet for Cinnamon that was started, apparently didn't work right, and was abandoned. This was from 2016. Not encouraging.
For every complex problem there is an answer that is clear, simple, and wrong - H. L. Mencken
Re: Possible to modify lock screen to show battery percentage?
Yes, that's possible.
Place this script in
This script runs in a loop, checks if screensaver is enabled and if so gets capacity_level from BAT0 and shows it under your clock. The screensaver updates the text only every minute when the clock is updated! First start can take a few seconds before the battery text apears.
Place this script in
~/bin
and make an autostart application.
Code: Select all
#!/bin/bash
##########################################################
# #
# Shows battery status in screensaver #
# #
##########################################################
# Path to hwmon for BAT0
device=$(ls -1 /sys/class/power_supply/BAT0 | grep hwmon)
hwmonpath="/sys/class/hwmon/$device"
if [ "$device" = "" ]; then
echo -e "\e[31;1m* Can not find any BAT0 hardware monitor in /sys/class/hwmon, exiting script.\e[0m"
exit 1
fi
# Start loop
while :
do
# Checks if screensaver is active
ss=$(xdg-screensaver status)
if [ "$ss" = "enabled" ]; then
capacity=$(cat $hwmonpath/device/capacity)
dconf write /org/cinnamon/desktop/screensaver/default-message "'Battery $capacity%'"
fi
# Sleep 10 seconds and check again
sleep 10
done
This script runs in a loop, checks if screensaver is enabled and if so gets capacity_level from BAT0 and shows it under your clock. The screensaver updates the text only every minute when the clock is updated! First start can take a few seconds before the battery text apears.
-
- Level 3
- Posts: 151
- Joined: Thu Mar 12, 2020 3:50 pm
Re: Possible to modify lock screen to show battery percentage?
Thank you, Koentje, for trying to help me!
I followed your instructions: created a file, copied the contents of the script into it and saved it, opened /bin as root and copied it into it. Then opened Startup Applications and added the script, simply as a link to the file, picked a 30 second delay after bootup.
I then rebooted. Started the lock screen (via power button, which I set in power management to start the lock screen). Sadly, the battery percentage is not showing up; I waited a few minutes.
I then looked at the script in more detail; however, my knowledge is rather limited. What I discovered, though, is that the folder /sys/class/hwmon/ has no '$device' items -- instead there are 6 links to folders, all numbered, starting with 0: /sys/class/hwmon/hwmon0/, /sys/class/hwmon/hwmon1/, etc.
All of those folders have a modified time stamp from when I last booted up. Their contents are not identical either, one has just 4 items, and one has as many as 20 - though some of those are again folders with more items. Am I understanding your script correctly that in this case the script exits, since it doesn't find the hardware monitor? Or did I get that wrong? I'm not getting any error message to that effect, though. Would it appear in the screensaver on the lock screen? (Am still confused if it's lock screen or screensaver - since the only screensaver that's not just black/blank seems to be the lock screen... )
If I go into any of those folder, e.g.: hwmon1, I seem to end up in an endless loop if I follow the first folder each time:
/sys/class/hwmon/hwmon1/device/hwmon1/device/hwmon1/device/hwmon1/device/hwmon1/device/hwmon1/device/hwmon1/device/hwmon1 -- it just keeps going like this; I stopped clicking.
Anything I could provide from my system so you can help me further? Or did you already spot a mistake I made? Maybe in setting up the script in the autostart app?
It seems like it should work, since I do have the hardware monitor. I don't know why that should matter, but to be on the safe side: I also use the Slimbook Battery software, with 3 different profiles, to extend my battery life (or "turbo charge" it for starting Google docs...). Could that interfere?
Thanks again!
LM 21.1 cinnamon on PC built by myself in 2008
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
Re: Possible to modify lock screen to show battery percentage?
Place the script in
~/bin
not /bin
! In other words, place it in your home's bin folder and try again. And see if it can be executed and is owned by you! Let me know if still nothing happens..-
- Level 3
- Posts: 151
- Joined: Thu Mar 12, 2020 3:50 pm
Re: Possible to modify lock screen to show battery percentage?
Oh! That would make a difference. I don't seem to have one in my home folder. So, shall I make one? If so, would it be as a ".bin" or "bin"?
LM 21.1 cinnamon on PC built by myself in 2008
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
Re: Possible to modify lock screen to show battery percentage?
SilenceIsG0lden wrote: ⤴Wed Jan 31, 2024 4:48 pmOh! That would make a difference. I don't seem to have one in my home folder. So, shall I make one? If so, would it be as a ".bin" or "bin"?
bin
And see if you have these lines in your
~/.bashrc
file
Code: Select all
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
-
- Level 3
- Posts: 151
- Joined: Thu Mar 12, 2020 3:50 pm
Re: Possible to modify lock screen to show battery percentage?
Thank you for your continued help! I've done both of those things -- and took the file with the script out of the other place + deleted the autostart entry for it + created a new one with the new link.
But still no luck: Rebooted, started screensaver, moved the mouse to active the login screen, waited a few minutes, nothing. The battery icon just sits there as usual.
Please confirm: every time you refer to the home folder, it is in fact 'home\{username}' - correct?
What about the fact that I don't have a device in '/sys/class/hwmon' and only those numbered virtual (as in they are all links) hwmon0 - hwmon5 folders?
LM 21.1 cinnamon on PC built by myself in 2008
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
Re: Possible to modify lock screen to show battery percentage?
Yes what else could be 'home'? Of cource it is your home directory... /home/$USER.
And yes again, /sys/class/hwmon only contains links.. because the real hwmon folders are in the devices folder, but linked to /sys/class/hwmon.
What does the script say when you start it from the terminal... error message or does it go into the loop?
And yes again, /sys/class/hwmon only contains links.. because the real hwmon folders are in the devices folder, but linked to /sys/class/hwmon.
What does the script say when you start it from the terminal... error message or does it go into the loop?
-
- Level 3
- Posts: 151
- Joined: Thu Mar 12, 2020 3:50 pm
Re: Possible to modify lock screen to show battery percentage?
Well, if I go one level up I have '/home/{username}' and '/home/.ecryptfs' -- I just wanted to be sure. (Sometimes the smallest misunderstandings can lead to extensive troubleshooting.)
[...]
I'm sorry; I don't want to annoy you. I was just trying to understand the script. I followed the links, and only the hwmon2 (i.e. /sys/class/hwmon/hwmon2/device) has a 'capacity' file; hwmon5/device has 'capacity_level'.And yes again, /sys/class/hwmon only contains links.. because the real hwmon folders are in the devices folder, but linked to /sys/class/hwmon.
Seemingly nothing happened. But when I wanted to close the terminal window, it said there was still something running. So I pressed the power button to call up the screensaver -- and voila! OK. This is weird. Literally nothing else changed except for me running the script once in the terminal. Could that have kicked off something? It is persistent; I just rebooted.What does the script say when you start it from the terminal... error message or does it go into the loop?
THANK YOU, Koentje!!
LM 21.1 cinnamon on PC built by myself in 2008
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
Re: Possible to modify lock screen to show battery percentage?
What did the name file say in both hwmon folders?SilenceIsG0lden wrote: ⤴Thu Feb 01, 2024 7:36 pm I followed the links, and only the hwmon2 (i.e. /sys/class/hwmon/hwmon2/device) has a 'capacity' file; hwmon5/device has 'capacity_level'.
So the loop was activated.Seemingly nothing happened. But when I wanted to close the terminal window, it said there was still something running.
It did not klick off anything, you prabably did not wait long enough the first time for the text to come up.. as i said, could take a while the first time.So I pressed the power button to call up the screensaver -- and voila! OK. This is weird. Literally nothing else changed except for me running the script once in the terminal. Could that have kicked off something? It is persistent; I just rebooted.
You're welcome!THANK YOU, Koentje!!
-
- Level 3
- Posts: 151
- Joined: Thu Mar 12, 2020 3:50 pm
Re: Possible to modify lock screen to show battery percentage?
hwmon2's is the battery, BAT0; hwmon5's is hidpp_battery_0. No idea what that could be. You?
Btw, I was too premature in my celebration -- I had plugged the Macbook in when it was at 34% charge. It's fully charged now, but the % in the lockscreen is not changing, no matter how often I go in there or how long I wait. I ran the script in a terminal, and now it updated to 100% - even though by now I'm down to 89%.
Any idea what could be going on?
LM 21.1 cinnamon on PC built by myself in 2008
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
Re: Possible to modify lock screen to show battery percentage?
I guess you have a battery powered mouse or keyboard?SilenceIsG0lden wrote: ⤴Thu Feb 01, 2024 10:35 pm hwmon2's is the battery, BAT0; hwmon5's is hidpp_battery_0. No idea what that could be. You?
I don't know, maybe it's a mac thing? On my Dell Latitude i get an update every minute. If i set a custom time in screensaver settings and add seconds to it, i get an update every second!Btw, I was too premature in my celebration -- I had plugged the Macbook in when it was at 34% charge. It's fully charged now, but the % in the lockscreen is not changing, no matter how often I go in there or how long I wait. I ran the script in a terminal, and now it updated to 100% - even though by now I'm down to 89%.
Any idea what could be going on?
%H:%M:%S
.I changed the script a little so now you see if AC is pluggen in.
Code: Select all
#!/bin/bash
##########################################################
# #
# Shows battery status in screensaver #
# #
##########################################################
# Path to hwmon for BAT0
hwmonpath="/sys/class/hwmon/$(ls -1 /sys/class/power_supply/BAT0 | grep hwmon)"
while :
do
ss=$(xdg-screensaver status)
if [ "$ss" = "enabled" ]; then
capacity=$(cat $hwmonpath/device/capacity)
ac=$(upower -d | grep 'online: ' | awk '{print $2}')
if [ "$ac" = "yes" ]; then
ac="[AC]"
else
ac=""
fi
# Uncomment below line to test on terminal
#echo "$ac Battery $capacity%"
dconf write /org/cinnamon/desktop/screensaver/default-message "'$ac Battery $capacity%'"
fi
sleep 10
done
-
- Level 3
- Posts: 151
- Joined: Thu Mar 12, 2020 3:50 pm
Re: Possible to modify lock screen to show battery percentage?
Insert facepalm. Yes, a wireless mouse.Koentje wrote: ⤴Fri Feb 02, 2024 5:34 amI guess you have a battery powered mouse or keyboard??SilenceIsG0lden wrote: ⤴Thu Feb 01, 2024 10:35 pm hwmon2's is the battery, BAT0; hwmon5's is hidpp_battery_0. No idea what that could be. You?
This is so odd. I tested with the uncommented line, and everything went as it should; it even updated the battery percentage in the screensaver, with the added [AC]. I changed the file in the BIN folder with your new script. Rebooted -- and the percentage shown in the screensaver was stuck at the number it was before I rebooted. I ran the script again in the terminal, went into the screensaver, came back in to look at the terminal window -- and it showed 108%, the 109%, but the battery was actually at 89%. When I started the screensaver again, it also showed 109% there. I haven't closed the terminal window, and it's steadily counting up there. My battery is at 100% now, but still plugged in. The script output is at 112%.You can also test it first on the terminal and uncomment the #echo "$ac Battery $capacity%" line. Start the script, start screensaver, plug your ac adapter, wait 10 seconds, then unplug ac adapter, wait 10 seconds, then exit screensaver. See if there is some battery output on the terminal.
Thank you very much for your efforts, but I think there's something particular about the MacBook that prevents your script from working correctly, as you have already suggested. Wouldn't be too surprising, I suppose. I won't change the solved status, though, as it may well work for others, and I'm now moving on without a solution. (First world problems...)
LM 21.1 cinnamon on PC built by myself in 2008
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
Re: [SOLVED] Possible to modify lock screen to show battery percentage?
I have used this script the past 2 weeks on my own laptop and it works great for me. So this is something mac indeed.
But can you test something for me please. Maybe i can change something in the script for it to work.
When the laptop percentage goes beyond 100%, can you run
But can you test something for me please. Maybe i can change something in the script for it to work.
When the laptop percentage goes beyond 100%, can you run
upower -d | grep 'percentage'
for me and see if this tells the correct or also incorrect numbers?Re: [SOLVED] Possible to modify lock screen to show battery percentage?
Anyway, in case upower does show the correct value, then below script should work!
Code: Select all
#!/bin/bash
##########################################################
# #
# Shows battery status in screensaver #
# V2 #
##########################################################
up=$(which upower)
while :
do
ss=$(xdg-screensaver status)
if [ "$ss" = "enabled" ]; then
capacity=$($up -d | grep 'percentage' | awk '{print $NF}' | head -n1)
ac=$($up -d | grep 'online: ' | awk '{print $2}')
if [ "$ac" = "yes" ]; then
ac="[AC]"
else
ac=""
fi
# Uncomment below line to test on terminal
#echo "$ac Battery $capacity%"
dconf write /org/cinnamon/desktop/screensaver/default-message "'$ac Battery $capacity'"
fi
sleep 10
done
-
- Level 3
- Posts: 151
- Joined: Thu Mar 12, 2020 3:50 pm
Re: [SOLVED] Possible to modify lock screen to show battery percentage?
Yes, upower does show the correct value. Sadly, though, the script still doesn't work when saved in the Bin folder. As in the first version of your script, after changing the script in the Bin folder and after rebooting, it permanently shows the percentage of the last time I ran the script in the terminal. (With the only difference being that it now shows 4 digits after the comma.)
Could the problem lie elsewhere, not in the script itself, but how its handled by the system? Pure speculation, as I have no clue, just based on the fact that it works when running it in the terminal. It just seems like it or a part of it is not being "picked up" by Cinnamon. If I go to the entry in Startup Applications and click the run now arrow, it doesn't change anything either. Still stuck at that percentage.
I'm willing to troubleshoot some more, since I know how this kind of challenge can be churning away in the back of one's mind. And who knows, maybe it'll work out in the end!
LM 21.1 cinnamon on PC built by myself in 2008
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
Re: [SOLVED] Possible to modify lock screen to show battery percentage?
Maybe upower for your device gives other layout. Can you post output of
upower -d
-
- Level 3
- Posts: 151
- Joined: Thu Mar 12, 2020 3:50 pm
Re: [SOLVED] Possible to modify lock screen to show battery percentage?
Code: Select all
Device: /org/freedesktop/UPower/devices/line_power_ADP1
native-path: ADP1
power supply: yes
updated: Wed 14 Feb 2024 14:25:59 (2607 seconds ago)
has history: no
has statistics: no
line-power
warning-level: none
online: no
icon-name: 'ac-adapter-symbolic'
Device: /org/freedesktop/UPower/devices/battery_BAT0
native-path: BAT0
vendor: Newer Tech
model: A1495
power supply: yes
updated: Wed 14 Feb 2024 15:09:07 (19 seconds ago)
has history: yes
has statistics: yes
battery
present: yes
rechargeable: yes
state: discharging
warning-level: none
energy: 14.1488 Wh
energy-empty: 0 Wh
energy-full: 43.364 Wh
energy-full-design: 38.48 Wh
energy-rate: 12.7576 W
voltage: 7.228 V
charge-cycles: 687
time to empty: 1.1 hours
percentage: 32.628%
temperature: 27.6 degrees C
capacity: 100%
technology: lithium-ion
icon-name: 'battery-good-symbolic'
History (charge):
1707944946 32.628 discharging
1707944944 32.662 discharging
1707944942 32.696 discharging
1707944940 32.747 discharging
1707944938 32.782 discharging
History (rate):
1707944946 12.758 discharging
1707944944 10.012 discharging
1707944942 11.537 discharging
1707944940 13.068 discharging
1707944938 12.432 discharging
Device: /org/freedesktop/UPower/devices/mouse_hidpp_battery_0
native-path: hidpp_battery_0
model: Wireless Mouse
serial: 1c-e9-ee-76
power supply: no
updated: Wed 14 Feb 2024 15:08:44 (42 seconds ago)
has history: yes
has statistics: yes
mouse
present: yes
rechargeable: yes
state: discharging
warning-level: none
battery-level: normal
percentage: 55% (should be ignored)
icon-name: 'battery-low-symbolic'
Device: /org/freedesktop/UPower/devices/DisplayDevice
power supply: yes
updated: Wed 14 Feb 2024 15:09:06 (20 seconds ago)
has history: no
has statistics: no
battery
present: yes
state: discharging
warning-level: none
energy: 14.1488 Wh
energy-full: 43.364 Wh
energy-rate: 12.7576 W
charge-cycles: N/A
time to empty: 1.1 hours
percentage: 32.628%
icon-name: 'battery-good-symbolic'
Daemon:
daemon-version: 0.99.17
on-battery: yes
lid-is-closed: no
lid-is-present: yes
critical-action: HybridSleep
LM 21.1 cinnamon on PC built by myself in 2008
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
LM 21.3 cinnamon on Macbook Air
Living in Linux Mint land since 2020, after decades on Microsoft (from DOS to Win7).
Re: [SOLVED] Possible to modify lock screen to show battery percentage?
Ok, thanks! That made some things more clear.. you have another path to your AC device then my laptop. And your battery power has decimals indeed.. you can uncomment a line to get rid of the decimals.
The script now checks for the right path to BAT0 and AC. And it logs more stuff, your environment, when started, some upower things and when it goes into a loop waiting for the screensaver.. etc.
Call it
The script now checks for the right path to BAT0 and AC. And it logs more stuff, your environment, when started, some upower things and when it goes into a loop waiting for the screensaver.. etc.
Call it
ss-battstat3.sh
and place it in your ~/bin
folder. Then change the autostart application to point to the correct script. Logout and login again and it now has created a ~/bin/ss-battstat.log.Code: Select all
#!/bin/bash
##########################################################
# #
# Shows battery status in screensaver #
# V3 #
##########################################################
x=1
scrpath=$(readlink -f $0)
workdir=$(dirname $scrpath)
log="$workdir/ss-battstat.log"
datum=$(date +'%d-%m-%Y %H:%M')
# GET PATH TO UPOWER
up="$(which upower)"
# GET PATH TO BATTERY DEVICE
bat0=$($up -e | grep 'BAT0')
# GET PATH TO POWER SUPPLY
ps=$($up -e | grep 'line_power_')
echo -e "\n*** Starting battery status in screensaver on $datum ***\n" > "$log"
echo -e "------------------------------------------------------------------\n" >> "$log"
echo -e "$(env)\n" >> "$log"
echo -e "------------------------------------------------------------------\n" >> "$log"
echo -e "upower path = $up" >> "$log"
echo -e "BAT0 path = $bat0" >> "$log"
echo -e "AC path = $ps\n" >> "$log"
echo -e "$(upower -d)\n" >> "$log"
echo -e "------------------------------------------------------------------\n" >> "$log"
echo -e "*** Going into loop ***" >> "$log"
while :
do
# GET SCREENSAVER STATUS
ss=$(xdg-screensaver status)
if [ "$ss" = "enabled" ]; then
if [ "$x" = "0" ]; then echo "* Screensaver active!" >> "$log"; x=1; fi
# GET BATTERY PERCENTAGE
capacity=$($up -i "$bat0" | grep 'percentage' | awk '{print $NF}')
# UNCOMMENT BELOW FOR BATTERY PERCENTAGE WITHOUT DECIMALS
#capacity=$(echo "$capacity" | awk -F'.' '{print $1"%"}')
# IS AC ADAPTER CONNECTED
ac=$($up -i "$ps" | grep 'online:' | awk '{print $NF}')
if [ "$ac" = "yes" ]; then
ac="[AC]"
else
ac=""
fi
# UNCOMMENT BELOW LINE TO TEST ON TERMINAL (or see ./ss-battstat.log)
#echo "$ac Battery $capacity"
# SET DCONF SCREENSAVER MESSAGE
dconf write /org/cinnamon/desktop/screensaver/default-message "'$ac Battery $capacity'"
echo "Dconf set to: $(dconf read /org/cinnamon/desktop/screensaver/default-message)" >> "$log"
else
if [ "$x" = "1" ]; then echo "* Screensaver inactive, waiting to get active.." >> "$log"; x=0; fi
fi
# SLEEP 10 SECONDS BEFORE WE CHECK AGAIN
sleep 10
done