Skip to content

Instantly share code, notes, and snippets.

@pazworld
Last active November 11, 2019 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pazworld/166a0fbebde13780ff9b4566821373fe to your computer and use it in GitHub Desktop.
Save pazworld/166a0fbebde13780ff9b4566821373fe to your computer and use it in GitHub Desktop.
Battery overcharge notification script for my ThinkPad X220 under Arch Linux
#!/bin/sh -eu
# Battery overcharge notification script for my ThinkPad X220 under Arch Linux
# most ideas and codes are from
# https://linoxide.com/linux-how-to/remind-unplug-charging-laptop-arch-linux/
# thanx
MAX_BAT=70
MIN_BAT=40
BAT_DEV="/org/freedesktop/UPower/devices/battery_BAT0"
SHOW_POWER_INFO_CMD="upower --show-info ${BAT_DEV}"
GET_PERCENTAGE='s/^ *percentage: *\([0-9]*\)%/\1/p'
GET_CHARGE_OR_DISCHARGE='s/^ *state: *\([^ ]*charging\)/\1/p'
LAST_STATE=""
INITIAL_PERCENTAGE=""
LAST_PERCENTAGE=""
PROGRESS_BAR=""
while true; do
BAT_PERCENTAGE=`${SHOW_POWER_INFO_CMD}|sed -n "${GET_PERCENTAGE}"`
if [ "$LAST_PERCENTAGE" = "$BAT_PERCENTAGE" ]; then
PROGRESS_BAR=${PROGRESS_BAR}"#"
else
echo -ne "\n"
LAST_PERCENTAGE=$BAT_PERCENTAGE
PROGRESS_BAR=""
fi
BAT_STATE=`${SHOW_POWER_INFO_CMD}|sed -n "${GET_CHARGE_OR_DISCHARGE}"`
if [ "$LAST_STATE" != "$BAT_STATE" ]; then
echo -ne "\n"
LAST_STATE=$BAT_STATE
INITIAL_PERCENTAGE=$BAT_PERCENTAGE
LAST_PERCENTAGE=$BAT_PERCENTAGE
PROGRESS_BAR=""
fi
if [ "$BAT_STATE" = "discharging" ]; then
echo -ne "\rBattery discharging: "
if [ $BAT_PERCENTAGE -le $MIN_BAT ]; then
notify-send "Battery under $MIN_BAT. Plaese plug in the adapter"
fi
else
echo -ne "\rBattery charging: "
if [ $BAT_PERCENTAGE -ge $MAX_BAT ]; then
notify-send "Battery above $MAX_BAT. Please remove the adapter"
fi
fi
echo -n "${INITIAL_PERCENTAGE}% -> ${BAT_PERCENTAGE}% ${PROGRESS_BAR}"
LAST_PERCENTAGE=$BAT_PERCENTAGE
sleep 10 #Repeat every 10 seconds
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment