vt-battery-checker is a Bash script which uses desktop notifications to inform the user about the laptop battery state and whether the power cord is plugged in or not.
The script comes in handy when your desktop environment does not provide the functionality already or it does but in a somewhat inconvenient manner (in my case this was LXDE).
The script requires vt-bash-utils 0.9.0 or later. In particular it calls the
vtbu_single_instance_running. Please download the library
from the project page and install it following the instructions provided.
Moreover it is required that your environment features reasonably new versions of:
- the GNU core utilities
- an implementation of AWK
It should be noted that the script has been tested using UPower 0.99.10, grep 3.1, coreutils 8.30, Gawk 4.2.1 and Bash 4.4.23 on Fedora 29 running on a Lenovo ThinkPad T420 laptop.
It is currently required to perform a simple manual installation. To do so follow these steps:
Download a ZIP file with the required version of the script from this page. Most likely you want the latest version.
Unpack the downloaded file.
unzip vt-battery-checker-0.8.2.zip && cd vt-battery-checker-0.8.2
If the vt-bash-utils library was installed to a directory other than the
/usr/local/sharedirectory modify the
vt-battery-checker.shscript on the line where the variable
bash_utils_pathis assigned a value to specifying the correct directory.
Copy the script to a directory which is in your
PATHenvironment variable and which is your preferred location to install third-party software to. This might be the
/usr/local/bindirectory for example. Make sure this is done as a user account allowed to write to the destination directory.
cp vt-battery-checker.sh /usr/local/bin
Add the execute mode bit to the script file mode bits. Again, make sure that this is done as the appropriate user account.
chmod +x /usr/local/bin/vt-battery-checker.sh
A common way to enable an application to autostart upon login to a desktop environment is to create a desktop entry for it in one of the directories
cat > ~/.config/autostart/vt-battery-checker.sh.desktop <<'EOF' [Desktop Entry] Type=Application Exec=vt-battery-checker.sh Hidden=false Name=vt-battery-checker.sh Comment= EOF
The intention is to configure the desktop environment to do this upon login (see above).
The script currently does not understand any command line options or other arguments.
The script listens for UPower events which signal that a power source has been added, removed or changed. When
any such event occurs the script retrieves the value of the
Online property of the
/org/freedesktop/UPower/devices/line_power_AC object meaning that it checks whether power is currently being
provided through line power. If this has changed since the last time it has been checked the script sends a
desktop notification signalling the same. If the power is not currently being provided through line power the
script retrieves the value of the
TimeToEmpty property of the
object meaning that it checks how much longer until the battery is empty. The script evaluates the state of
the battery based on the estimated time left until it is empty. The state is an integer and it is
0 if the
time left is at most 2 minutes,
1 if the time left is at most 6 minutes,
2 if the time left is at most 11
3 if the time left is at most 31 minutes and finally
4 if the time left is more than 31 minutes.
If the battery state is lower than the last time this has been checked or the power has just stopped being
provided through line power the script retrieves the value of the
Percentage property of the
/org/freedesktop/UPower/devices/battery_BAT0 object meaning that it checks what the amount of energy left in
the battery is, expressed as a percentage, and sends a desktop notification stating the estimated time left
until the battery is empty and the amount of energy left in the battery.
Currently the script prevents running multiple instances of itself at the same time. Therefore it does not fully support a system shared among multiple users.
Suggestions and bug reports are more than welcome at email@example.com.