Skip to content

Instantly share code, notes, and snippets.

@hypernova7
Created February 20, 2022 05:51
Show Gist options
  • Save hypernova7/85de79fb2c04964011ca9fda201d63fe to your computer and use it in GitHub Desktop.
Save hypernova7/85de79fb2c04964011ca9fda201d63fe to your computer and use it in GitHub Desktop.
Battery monitor with dunst
#!/bin/bash
# Original source: https://paste.pound-python.org/raw/QHM2XBftWwSti7Pc63JN/
# Batterry monitor with dunst
battery_level=$(acpi -b | sed -n 's/.*\ \([[:digit:]]\{1,3\}\)\%.*/\1/;p')
battery_state=$(acpi -b | awk '{print $3}' | tr -d ",")
battery_remaining=$(acpi -b | sed -n '/Discharging/{s/^.*\ \([[:digit:]]\{2\}\)\:\([[:digit:]]\{2\}\).*/\1h \2min/;p}')
backlight_cmd=$(which light)
_battery_threshold_level="35"
_battery_critical_level="30"
_battery_suspend_level="25"
if [ ! -f "/tmp/.battery" ]; then
echo "${battery_level}" > /tmp/.battery
echo "${battery_state}" >> /tmp/.battery
exit
fi
previous_battery_level=$(cat /tmp/.battery | head -n 1)
previous_battery_state=$(cat /tmp/.battery | tail -n 1)
echo "${battery_level}" > /tmp/.battery
echo "${battery_state}" >> /tmp/.battery
checkBatteryLevel() {
if [ ${battery_state} != "Discharging" ] || [ "${battery_level}" == "${previous_battery_level}" ]; then
exit
fi
if [ ${battery_level} -le ${_battery_suspend_level} ]; then
# I don't use sudo for
systemctl suspend
elif [ ${battery_level} -le ${_battery_critical_level} ]; then
dunstify "Low Battery" "Your computer will suspend soon unless plugged into a power outlet." -u critical
${backlight_cmd} -S 50
elif [ ${battery_level} -le ${_battery_threshold_level} ]; then
dunstify "Low Battery" "${battery_level}% (${battery_remaining}) of battery remaining." -u normal
${backlight_cmd} -S 75
fi
}
checkBatteryStateChange() {
if [ "${battery_state}" != "Discharging" ] && [ "${previous_battery_state}" == "Discharging" ]; then
dunstify "Charging" "Battery is now plugged in." -u low
${backlight_cmd} -S 100
fi
if [ "${battery_state}" == "Discharging" ] && [ "${previous_battery_state}" != "Discharging" ]; then
dunstify "Power Unplugged" "Your computer has been disconnected from power." -u low
fi
}
checkBatteryStateChange
checkBatteryLevel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment