Laptops should be a bit annoying when they are low on battery. Otherwise, I will not realise it until it’s too late, and my computer will turn off in the middle of whatever I was doing.
Proper linux Desktop Environments already do this, but since I am running the Awesome window manager with a custom config, I only get what I add myself.
My solution is as follows. First, the annoyance script:
~/.local/bin/checkbattery
#!/usr/bin/env python3
import subprocess
import math
import sys
with open('/sys/class/power_supply/BAT0/status') as f:
status = f.read()
if status in ["Charging\n", "Full\n"]:
sys.exit(0)
with open('/sys/class/power_supply/BAT0/energy_full') as f:
total = int(f.read())
with open('/sys/class/power_supply/BAT0/energy_now') as f:
now = int(f.read())
perc = math.floor(now / total * 100)
if perc < 10:
subprocess.run(["notify-send",
"-t", "0",
"-i", "/usr/share/icons/Numix/24/status/gpm-battery-020.svg",
"-u", "normal" if perc > 5 else "critical",
f"Low battery! {perc}%"])
Depending on the laptop, energy_now
and energy_full
are replaced
by charge_now
and charge_full
.
And notify-send
must be installed.
Then we could run it on login on a loop with a sleep, but I just have systemd call it every 2 minutes:
~/.config/systemd/user/battery.service
[Unit]
Description=Notify of low battery
[Service]
Type=oneshot
ExecStart=%h/.local/bin/checkbattery
~/.config/systemd/user/battery.timer
[Unit]
Description=Check battery status every 2 minutes
[Timer]
OnCalendar=*:0/2
[Install]
WantedBy=timers.target
$ systemctl --user enable --now battery.timer