How do I tell my system to create a sound alert whenever my battery has charged at least X percent?

There are many other options too. For example in Dells you cap battery level to a certain limit.

There are many other options too. For example in Dells you cap battery level to a certain limit.

Well this is on a tuxedo built laptop with standard install of the XCFE variant on it, only installed the os and started using the laptop maybe a week ago so I don’t think I have that option, anyways I still want the sound alert for when the battery is close to full so I would need an app or script that does that, your script certainly looks like an easy solution but if I’m gonna have one I might as well put effort into finding a solution that takes the least amount of cpu time & consumes the least amount of power, that way peops who find this thread later will know what to look for if they need their solution to be more efficient than a simple shell script

btw, related question, how do I set a program or script to run periodically? And how to set that period with it

https://wiki.archlinux.org/title/Systemd/Timers

Thanks, for now I’m running that little script that was given however I’m also looking into the libraries needed to program it directly via this file I found while searching the net:

Made a comment in my code about it too just in case I lose the address, at the very least it serves as an example of how to use the api and what details to watch out for, for my library code I’ll have to exclude the mutex stuff since that should be sorted by the code using the wrapper, fortunately I managed to design a custom mutex & a custom wrapper for critical sections, neither can dead lock and efforts have been made to prevent them being destroyed while another thread is trying to lock them, I even went and made a custom solution to thread locals since it was almost impossible to do a one to one mapping between microsoft’s TlsAlloc etc and __thread_local etc, in the process of doing so I managed to make a quick linked list gc out of it, since I’m also making wrappers for the thread APIs in general I can just call the cleanup function before letting the thread die completely, the api is designed to be faster if __thread_local is available but silently fallback to a critical section if it’s not, something like this:

PAWTLS pawvu tls_ptr_or_index = 0;
...
pawtls_getloc( (void**)&local_data_ptr, &tls_ptr_or_index, sizeof(local_data) );
...
pawtls_remloc( &tls_ptr_or_index );
...
pawtls_remall();

Those are the only 3 functions and the 3rd one is meant as an exit call as everything that is linked will be deleted, I might take a PAWCLS pointer for the destruction callback before deletion though

Adapt this one

1 Like

Didn’t work out, I had my battery on charge for a while and just noticed it was at 87% after going back up from about 40ish percent, I’ll give the other one that was just posted a try until I get round to trying to construct an app that takes care of both reading the status and using the speakers to alert me to the battery state

May I ask, what your reacrion to the sound is?
Do you take the laptop from the power?

If you just want a notification to stop charging, you could also try tlp in case it supports your brand.
There one can set thresholds, when charging should start and to what capacity it be charged.

I do not know what brand of laptop you have, or even which kernel you use, but maybe this can mean something to you:

https://wiki.archlinux.org/title/Laptop/ASUS#Battery_charge_threshold

Edit:

And, as mentioned, take a look at TLP:

https://linrunner.de/tlp/

I think you’ll want the Battery Care section:

https://linrunner.de/tlp/settings/battery.html#battery-care

…and

https://linrunner.de/tlp/settings/bc-vendors.html

Only just saw your message, I just turn off the switch at the wall, can’t overcharge if there’s no power supply to begin with, however the sound needs to play for me to notice the message when I’m absorbed in something like a video or programming or reading a story

Thanks, will do

Edit: Having looked at it I can see that it doesn’t actually do what I want it to do, it automates battery care sure but not in a way I like, I only want an annoying sharp sound to be played from the speakers to alert me to the fact the battery has charged at least 80% and leave the decision of whether to cut the power or not to me, I find this sound to be acceptable:

/usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga

I just need to figure out how to read the battery state and work out what percentage it’s at and how to play the sound from C, I’m already familiar with the time() & clock() APIs so it’s only those 2 that I need to learn to setup my own app now

Then I’m guessing you’ll need to make a script that launches on boot, which you can create a Systemd unit for to accomplish and use bash’s watch to check for battery charge change.

Edit:

Combine this:

…with watch

Welp that didn’t work, set it to every 300s and no sound had come out despite having reached 83% by the time I looked

well, Ithink:

first see whether this line even works/reports something accurate

or rather, this command:
acpi -b | grep -P -o '[0-9]+(?=%)'

If it does, debug further.
If it doesn’t:
make it work :man_shrugging:

it doesn’t work for me, because the “acpi” command isn’t recognized …

Perhaps the script was just an example of the approach that can be taken?

Well it worked for me, checked the output against the reported battery percentage and it matched so I doubt that’s the issue at all, thanks anyways. I don’t usually use the shell since I can just programme what I want in C so long as I know what api to work with for it

if you are running the script every 300 seconds, perhaps the condition is never met
(it’s never equal - first it’s lower, on the next check it is already higher …)

if [[ $battery_level -eq $battery_limit ]]; then

use -gt instead, to catch the condition even though it is slightly overshot during the 5 minutes?

1 Like

Didn’t even notice that, I’ll try it then, but 1st is -ge (>=) supported at all?

yes, it is
… and the sound is a very subtle “ding”

Well I actually switched out the sound for the alarm, your tip did the trick though so I at least have a temporary solution until I get round to constructing a proper app that does it all by itself

Good!

I thought I mention the subtle sound, as it is so subtle that it may be missed …

But I do think this is already a proper solution - I’d spend no more time on it.
and credit to @Mirdarthos for it

… but you do like coding in C - a thing which I know next to nothing about :grimacing:

The eventual solution you will create will have been more work intensive but equally as functional, I’d wager.

anyway

Have fun! :nerd_face: