How can I play a custom sound when my battery is low?

I want my computer to play a custom sound file when my battery is low (say 10%). How can I do this?
Thanks

Answer

I just wanted to reply as I used the advice on this page for configuring my own crontab -e job.

I’m running Lubuntu (trusty) and the above didn’t work for me, but with a little tweaking it did. My level of (in)experience is that I’m relatively capable of reading basic scripts and commands but less able to write them from scratch in linux disto’s. I mention that because if I can cobble together a working battery alarm, most of you reading this should be able to as well!

SO…. the script and a basic explanation….

As per the posts above by Glutanimate and Scott Goodgame (thanks guys!), I basically did the following:

To get mpg123 and be able to play audio:

sudo apt-get install acpi mpg123

File to open to write the command:

crontab -e

Command to enter at end of the file:

*/1 * * * * if [ `acpi -b | awk ' { print ( $(NF-2)-0)}'`  -lt "15" ] ; then mpg123 /home/andy/Alarm.mp3 ; fi

Explanation of the above

Asterisks at the start

The five asterisks represent the frequency at which to run the commands.

  • First star = Minutes: 0-59
  • Second star = Hours: 0-23
  • Third star = Day of Month: 0 – 31
  • Fourth star = Month: 0 – 12
  • Fifth star = Day of Week: 0 – 6 (0 means Sunday)

*/1 * * * *

means ‘run this every minute’.

IF/THEN/FI:

IF [ stuff inside these brackets ] ; THEN do something ; FI

Most of you, I imagine, are very familiar with IF/THEN statements.
The FI at the end concludes the IF statement

Playing the audio:

mpg123 /home/andy/Alarm.mp3

this simply uses mpg123 to play a file called Alarm.mp3 located in my home directory /home/andy/.

Output battery info:

acpi -b

this simply outputs the battery status. On my laptop it yields results such as:

Battery 0: Discharging, 74%, 02:35:18 remaining

Pipe to awk command

 | awk ' { print ( $(NF-2)-0)}'`  -lt "15"

Okay, so we’re piping the output of the battery status above and printing the second-from-the-end value (NF-2) which in the above example is ‘74%’. However, we need to remove the ‘percentage’ -0. We want to compare the current value with the value 15 -lt “15”. So to sum it up in plain English,

IF the value of the battery is LESS THAN 15% THEN play an audio file

I appreciate I’ve dumbed it down somewhat and perhaps my explanation could be improved on, but hopefully it’ll help someone else understand how to add an alarm for the battery and avoid their laptops from just powering off all of a sudden!

Good luck!


EDIT

Okay… so it turns out that when my laptop is charging, the output of

acpi -b

is:

Battery 0: Charging, 35%, 01:04:08 until charged

whereas on battery is

Battery 0: Discharging, 36%, 01:22:36 remaining

so using the parameter mentioned initially of

print ( $(NF-2)-0)

outputs different results, because remember, the NF-value bit specifies how many items backwards from the end of the output to display, so with the output on battery of

Battery 0: Discharging, 36%, 01:22:36 remaining

$(NF-2) outputs ‘36%’ whereas with the laptop on charge,

Battery 0: Charging, 35%, 01:04:08 until charged

$(NF-2) outputs ’00:50:12′, i.e. the amount of time remaining until charged.

The -0 after $(NF-2) minuses zero from the value, so when the value is a percent -0 changes it from something like ‘36%’ to ’36’. When the output value is perceived as non numerical, such as the time output of ’01:04:08′ as above, the value -0 returns is ‘0’. So you can see my issue – when my laptop is on charge, the output value is always ‘0’. Since I want to play an alarm when the output value is less than 15, it translates to a battery alarm that goes off every one minute when on charge!

I read up a little more on the output of the awk command and it turns out I didn’t need to make it read the second value from the end, I could start from the beginning of the output and skip forward. This is much better as my battery status begins the same whether it is on charge or on battery in terms of number of parameters, i.e. the fourth parameter from the beginning is always the value of charge in percent.

SO… to make it read the fourth value, the command changes from

awk ' { print ($(NF-2)-0)}'

to

awk ' { print ($4)-0}'

or in its full glory:

*/1 * * * * if [ `acpi -b | awk ' { print ($4)-0}'`  -lt "15" ] ; then mpg123 /home/andy/Alarm.mp3 ; fi

Again I hope that helps someone! Good luck and thanks for reading!


BONUS EDIT

Okay, so a little additional poking around and I also worked out how to set the volume to 100% and unmute before playing the alarm – because let’s face it, what’s the point of a battery alarm if you can’t hear it because you’ve set the volume to low or muted it?!

Anyway, the command you need is:

*/1 * * * * if [ `acpi -b | awk ' { print ($4)-0}'`  -lt "15" ] ; then pactl set-sink-volume 0 100% && pactl set-sink-mute 0 0 && mpg123 /home/andy/Alarm.mp3 ; fi

as you can see, all I’ve done is inserted the command string

pactl set-sink-volume 0 100% && pactl set-sink-mute 0 0 &&

(the &&‘s just bind multiple commands together)

Again, good luck all!

LAST EDIT

Okay, So… one final (hopefully!) edit… apparently, the percentage sign is a special sign when running cron tasks. Therefore, you have to ‘escape’ the percentage sign by putting a backslash (\) in front of each % so it isn’t (mis)interpreted by the shell.

So basically replace

100%

with

100\%

Therefore, the command you really REALLY need is:

*/1 * * * * if [ `acpi -b | awk ' { print ($4)-0}'`  -lt "15" ] ; then pactl set-sink-volume 0 100\% && pactl set-sink-mute 0 0 && mpg123 /home/andy/Alarm.mp3 ; fi

phew hope that sorts it for you as it has for me!! (eventually!)

Attribution
Source : Link , Question Author : vyb , Answer Author : Eric Carvalho

Leave a Comment