I’ve been reading up on bash scripting, of which I am almost completely ignorant, unless you count being able to slavishly copy, paste, and cron
other people’s work. However, I’m a great fan of logic, so the concept is not all that alien to me.
So, here’s my attempt at stringing together a script that audibly informs when the Librem 5’s juice is low, and also when, after plug-in, it has reached a charge of 80%. I’ve tested it a couple of times and it seems to be working. (Critique, suggestions, or corrections are very welcome; I’m sure my script has room for improvement.)
This is only a notification script; it doesn’t limit or otherwise affect charging.
Prerequisites:
upower
mplayer
or another command-line music playerespeak
- a short sound file (create or copy from somewhere)
(My script’s name is batwarn.sh
, and my sound file is just a plink-plink-plink that I recorded from a music generator app.)
#!/bin/bash
# In order for cron to play sound files it needs to export an environment variable:
export XDG_RUNTIME_DIR="/run/user/1000"
state=$(upower -i /org/freedesktop/UPower/devices/battery_max170xx_battery | grep "state" | awk '{print $2}')
battery=$(upower -i /org/freedesktop/UPower/devices/battery_max170xx_battery | grep "percentage" | awk '{print $2}')
battery=${battery/\%/}
if [[ $state == discharging && $battery -le 40 ]]; then
mplayer /home/purism/batalarm.ogg && espeak "charge battery"
elif [[ $state == charging && $battery -eq 80 ]]; then
mplayer /home/purism/batalarm.ogg && espeak "80% charge"
elif [[ ( $state == charging || $state == fully-charged ) && $battery -ge 81 ]]; then
exit
fi
For the benefit of other novices, like me:
-First update the L5.
-Copy the above code into a text file and save it as a shell script, e.g. batwarn.sh
(home directory is fine)
-From the command line, make the file executable: chmod +x batwarn.sh
(Be in the correct directory.)
-Run crontab -e and add a cron job (i.e. scheduled task):
*/5 * * * * bash /home/purism/batwarn.sh
(but point it toward the actual location and name of your own file).
“Translating” the various elements of the script (from the top):
All bash (Bourne again shell) scripts start with a “shebang:” #!/bin/bash
Any line that starts with “#” is a comment, and is ignored by the script.
“Enable cron to play sound; this script runs for any user who is logged in.”
Short words (variables) are designated to substitute for long strings, mainly to ease typing the arguments. The long strings gather system information about the current battery charging/discharging state and the percentage of charge, and strip out the “%” sign; the variables are then used in the arguments to represent the gathered values.
The conditional statements and operators can be read as:
“If the current battery state is listed as ‘discharging,’ AND the charge percentage is less than or equal to 40, then launch mplayer
to play the file batalarm.ogg
AND then open espeak
to say “charge battery””
“Else, if the state is ‘charging,’ AND the charge percentage is equal to 80, then play the file AND then say “80% charge””
“Else, if the state is ‘charging,’ OR ‘fully-charged,’ AND the charge percentage is greater than or equal to 81, then exit and finish.”
The cron job will cause the sounds to play every 5 minutes until the device gets plugged in, shuts down, or the charge meets the required upper threshold.
Either the espeak
command or the mplayer
sound file can be eliminated from the script. The percentage thresholds can be changed.
You can run the following directly in the terminal to see where the charge data comes from and how it’s filtered:
upower -i /org/freedesktop/UPower/devices/battery_max170xx_battery
upower -i /org/freedesktop/UPower/devices/battery_max170xx_battery | grep "state"
upower -i /org/freedesktop/UPower/devices/battery_max170xx_battery | grep "state" | awk '{print $2}'
upower -i /org/freedesktop/UPower/devices/battery_max170xx_battery | grep "percentage"
upower -i /org/freedesktop/UPower/devices/battery_max170xx_battery | grep "percentage" | awk '{print $2}'
Fortunately there are a lot of bash tutorials on the internet.
See also: Why doesn't `upower -i` work?
EDIT:
mplayer /home/purism/batalarm.ogg && espeak "..."
I’ve noticed that unless I loop the sound file twice, it sometimes doesn’t play, only the espeak
ending. To fix this, play the sound file twice:
mplayer /home/purism/batalarm.ogg -loop 2 && espeak "..."