[root tip] [Utility Script] Charger Notifier - battery state

A recent topic caused me to add the following script to my toolbox.

The script is configurable and uses an interval and lower/upper threshold for battery.

The script utilizes libnotify to send a system message when outside the limits - thus ensuring you don’t forget to plug or unplug your laptop charger.

Script

Create the local bin folder

mkdir ~/.local/bin

Create a new file and make it executable

touch ~/.local/bin/charge-notify.sh && chmod +x ~/.local/bin/charge-notify.sh

Edit the file with kate or another editor

xdg-open ~/.local/bin/charge-notify.sh

Paste below content and save

#! /bin/bash
#
# Script to notify when battery is outside levels - time to plug charger.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.#
#
# Source: https://linoxide.com/remind-unplug-charging-laptop-arch-linux
#
# https://forum.manjaro.org/t/set-up-an-alarm-notification-when-battery-reaches-full-charge/92331
#
# @linux-aarhus - root.nix.dk
#
# 2021-11-27
# 2021-11-28 revised - checks not updating
#                    - fix variable check on all levels
#

set -eu

# dependency check
if ! [[ "$(which notify-send)" =~ (notify-send) ]]; then
	echo "Please install libnotify to use this script.\n"
	echo "   sudo pacman -S libnotify"
	exit 1
fi
if ! [[ "$(which acpi)" =~ (acpi) ]]; then
	echo "Please install acpi to use this script.\n"
	echo "   sudo pacman -S acpi"
	exit 1
fi

# check interval (seconds)
INTERVAL=30

# example battery levels
# these leves are not based on scientific evidence
# you are required to adjust as appropriate for your device
MIN_BAT=40     # low water mark
MAX_BAT=80     # high water mark

get_plugged_state(){
	echo $(cat /sys/bus/acpi/drivers/battery/*/power_supply/BAT?/status)
}

get_bat_percent(){
    echo $(acpi|grep -Po "[0-9]+(?=%)")
}

# primary loop
while true ; do

	if [ $(get_bat_percent) -le ${MIN_BAT} ]; then # Battery under low limit
 		if [[ $(get_plugged_state) = "Discharging" ]]; then # plugged
 		    notify-send "Battery below ${MIN_BAT}. Time to plug adapter"
 		fi
        fi
	if [ $(get_bat_percent) -ge ${MAX_BAT} ]; then # Battery over high limit
 		if [[ $(get_plugged_state) = "Charging" ]]; then # plugged
 			notify-send "Battery above ${MAX_BAT}. Time to unplug adapter"
 		fi
	fi
	sleep ${INTERVAL} # Repeat every $INTERVAL seconds
done

Usage

The battery levels are example levels. Edit the battery levels according to your system and preference.

If in doubt please see below comments and do your own research.

Launch the script using the systems autorun feature - each system does it differently :slight_smile:

Sample screenshots

I have put in a couple of messages in my debug version to see if the notification is trigger at the right point and the script is configured with a 60s interval.

Click to reveal screenshots

When the adapter gets plugged

Reaching high water

4 Likes
Semi-off-topic note about those values in terms of longer life of battery

Perhaps notebooks uses near the same technology batteries as smartphones does: Li-Ion and Li-Pol.

While main customer difference in them is that Li-Pol able to provide higher current values than Li-Ion, other specs are near the same.
All batteries uses 2 stages to get charge: high current, constant voltage up to about 50-55% and seconds stage of higher voltage in order to save current values closer to maximum as it can possible be.
First stage is OK for battery wearing, second stage is not OK and increasing wearing significantly.

There is the Accu​Battery app (https://play.google.com/store/apps/details?id=com.digibites.accubattery ) for Android phones, which aims on proper charging in terms of battery\s long life usage. The app based on scientific publications.
The app notes that if to charge battery from 20 to 80% the battery life will be longer in 3 times (as the app states in in it’s description) comparing with the 0-100% full cycle charging.

Battery wearing counted in full cycles and after certain count battery notably looses it’s capacity and charge movement speed while charging and discharging.
I will try to show on the experiment data based of smartphone charging and the app’s results of it to show what values are more optimal than 40-80% in the quote:

If we do 0-100% charging we wearing the battery for 1.00 cycle and get 100% of charge, so charge-wearing"exchange" rate is 100% / 1.00 cycle = 100 %/cycle.

Experiments with phone's battery shows this

40% / 0.17 = 235 % of charge capacity / wearing cycle = in 2.4 times


60% / 0.21 = 285 % / cycle = in 2.9 times


Most perfect (900%):

Possibly more optimal as having more charge to use but for the price of very low increase of battery wearing (800%):


So for longest battery life purpose I found that lower limit is almost absent (5% is still good), the higher limit is about 50%. Make it higher if you want to pay battery wearing for more autonomous work period.

I am sure that lower threshold of 40% is very far from optimal.

Possibly optimal charging from 5 to 55…60% and to have 50-55% of charge of full battery capacity with the goal of 8 times less wearing comparing to full charge (0-100%).

Thank you!

1 Like

It is common knowledge that batteries for moderne equipment lasts longer if you don’t deep decharge and don’t load to full capacity.

I have noted that batteries tend to last longer when you don’t wear them complete down on every discharge.

I have not scientific proof - nor have spent time investigating - please share your results :slight_smile:

As you can read in the post number 2, the results of two months of experimenting with the mentioned app on my phone with different change range are listed there. Use click on spoiler named “Experiments with phone’s battery shows this” to expand these results for my smartphone.

The app represent results based on scientific research, most common noted of them are

-) Study of life evaluation methods for Li-ion batteries for backup applications - ScienceDirect
-) Factors that affect cycle-life and possible degradation mechanisms of a Li-ion cell based on LiCoO2 - ScienceDirect

Would be even more interesting if someone will find and post experiment results of such app for notebooks, as I only assume that batteries on smartphones and notebooks behaves near the same.

The original script contains no commands to play sounds.

I guess you can enable sound on notification for your system - and that is beyond the script.