Skip to content

Instantly share code, notes, and snippets.

@ahmedbaig
Created September 3, 2022 05:00
Show Gist options
  • Save ahmedbaig/ae93a0c1289a2e924849bd73656e114c to your computer and use it in GitHub Desktop.
Save ahmedbaig/ae93a0c1289a2e924849bd73656e114c to your computer and use it in GitHub Desktop.
Batter Charge Notification Maker
#!/bin/bash
# Description: Display notification if battery is about to charge or discharge fully.
# upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "state|to\ full|percentage" ; --- There are different ways we get battery status - this is no 1
# upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "state|to\ full|percentage" | awk '/perc/{print $2}' --- There are different ways we get battery status - this is no 2
# $ find /sys/class/power_supply/BAT0/ -type f | xargs -tn1 cat --- There are different ways we get battery status - this is no 3
# $ cat /sys/class/power_supply/BAT0/status --- There are different ways we get battery status - this is no 4
# $ cat /sys/class/power_supply/BAT0/capacity --- There are different ways we get battery status - this is no 5
# we will try to use 4th & 5th ; as only these gives one letter / number as output. Rest gives long string and also baterry % in long string , cannot be used to compare arithmatically
# Set state variable =
cat /sys/class/power_supply/BAT0/status
if [ $? -eq 0 ]; then state=$(cat /sys/class/power_supply/BAT0/status) ;
else state=$(cat /sys/class/power_supply/BAT1/status) ;
fi ;
# Set capacity variable =
cat /sys/class/power_supply/BAT0/capacity
if [ $? -eq 0 ]; then percent=$(cat /sys/class/power_supply/BAT0/capacity) ;
else percent=$(cat /sys/class/power_supply/BAT1/capacity) ;
fi ;
state_1="Charging" ;
percent_1="98" ;
state_2="Discharging" ;
percent_2="15" ;
rm $percent_1 # I do not know why, but running this script creates a file named of "percent_1" field value. rm $percent_1 removes that file.
rm $percent_2 # I do not know why, but running this script creates a file named of "percent_2" field value. rm $percent_2 removes that file.
#### play_beep = $(play -q -n synth 0.2 sin 880 >& /dev/null)
#### play -q -n synth 0.1 sin 880 ---> Changing the 0.1 will change the length of the sound, Changing sin 880 will adjust the pitch ... lots of possibilities!
# ------- Overcharging Protection -------
if [ $state = $state_1 ];
then
if [ $percent -gt $percent_1 ];
then
(notify-send -i /usr/share/icons/gnome/22x22/devices/battery.png "Battery is almost CHARGED = $percent %" "Disconnect power supply to improve battery life" ; $(play -q -n synth 0.2 sin 880 >& /dev/null) ; sleep 1 ; spd-say "Disconnect Charger" || speak "Disconnect Charger" ; rm $percent_1)
else rm $percent_1 ; exit 0
fi
# ------- Discharging Protection -------
else
if [ $state = $state_2 ];
then
if [ $percent -lt $percent_2 ];
then
(notify-send -i /usr/share/icons/gnome/22x22/devices/ac-adapter.png "Battery is about to DISCHARGE = $percent % remaining" "Connect power supply to continue" ; $(play -q -n synth 0.2 sin 880 >& /dev/null) ; sleep 1 ; spd-say "Connect Charger" || speak "Connect Charger" ; rm $percent_2)
else rm $percent_2 ; exit 0
fi
else rm $percent_2 ; exit 0
fi
fi
exit 0
# Further instructions -
# ------- Save the script in /home/USERNAME & Add the script in crontab to run-----
# In the terminal type command :- chmod 777 /home/Replace_by_USERNAME/Battery_Charge_Notification.sh # replace actual username
# in the terminal type command :- crontab -e
# add following parameters at the end of file (uncomment to enable)
#### Min Hour Day Month DayOfWeek Command
# DISPLAY=":0.0"
# XAUTHORITY="/home/ahmedbaig/.Xauthority"
#### In above Line, replace actual username in place of ahmedbaig. Delete THIS line after that.
# XDG_RUNTIME_DIR="/run/user/1000"
# MAILTO=''
# add following cron schedule below the parameters (uncomment to enable) to run the script in every 10 min.
#*/10 * * * * bash /home/ahmedbaig/.Battery_Charge_Notification.sh >/dev/null 2>&1 # replace actual username
# ------- End ---------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment