Show notification and play alert sound on low battery

My laptop’s battery weared out to only 122mAh from 6000mAh, runs merely 2-3 minutes without AC power.

Sometimes either I forget to turn the switch on or a power cut happens, so the laptop abruptly shuts down itself with a nice click sound, sudden HDD head parking.

I decided to display alert message and play sound if the AC power is not present and battery voltage dropped below 11.1V, which is the lowest threshold voltage for Li-Ion batteries.

 

How this alert  works ?

I’ve decided to fist check if AC power is connected or not. If AC power is not available, then check if the battery voltage is below 11.1V (actually 11100000μV).

If the first condition is true, i.e. no AC power and second condition is also true, i.e. voltage is slightly below 11.1V then it checks if X session is running or not.

If X session is running, it sends visual alert message through notify-send and plays a beep sound.

notification_low_power1

The entire process runs again after 10 seconds through a systemd service. The beep sound is played nevertheless a GUI session is running or not.

The red battery icon I’m using is part of Antu icon theme.

 

The script which actually doing all of this

You could find some interesting information about your laptop’s battery and AC adapter under the /sys/class/power_supply/ directory, these are virtual directories crated and managed by kernel.

The notify-send command is part of libnotify-bin package in Debian or Ubuntu. I’m using notify-osd as notification daemon on LXQt desktop.

Have a look at the script I’m using,

#!/bin/bash
# Check AC adapter status and Battery Voltage
if [ $(cat /sys/class/power_supply/ACAD/online) = "0" ] && \
   [ $(cat /sys/class/power_supply/BAT0/voltage_now) \< "11100000" ]; then
       # Display Notification
       if [ $DISPLAY ]; then notify-send -u critical \
       -i /usr/share/icons/Antu/status/64/battery-low.svg \
       -t 2000 "Battery Critically Low" "Plug in to AC or Suspend immediately"; fi
       # Play Alert Sound
       for play_beep in {1..4}; do $(aplay /home/b00m/Downloads/beep-08b.wav > /dev/null 2>&1); done
fi

The script may look cumbersome, but it’s really simple and fairly self-explanatory, just don’t forget to edit the shell script according to requirements.

I’ve saved this scripts as battery_low_alert .

 

Creating a systemd service file to run the above script

I think it’s more convenient to run the script as a systemd service rather than running it as another cron job.

So I created a systemd service file, named low_battery.service under the /etc/systemd/system/ folder.

 sudo nano /etc/systemd/system/low_battery.service 

The systemd service file,

[Unit]
Description=Send alerts on low battery

[Service]
Type=simple
ExecStart=/bin/bash /home/b00m/battery_low_alert
Environment="DISPLAY=:0" "XAUTHORITY=/home/b00m/.Xauthority"
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Needless to say again, don’t forget to edit the above systemd service according your system.

Now reload systemd daemons and enable the low_battery.service systemd service.

sudo systemctl daemon-reload

sudo systemctl enable low_battery.service

You also could use User and Group parameters under the [Service] section to run the script as normal user. More about systemd here and here.

 

Conclusion

Hope you all enjoyed reading this story, there may be some sophisticated application to do exactly this, but prefer this way more.

Don’t forget to share your suggestions below through the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *