[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.


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
#    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
# 2023-03-14         - added sound option

# check interval (seconds)

# example battery levels
# these levels are not based on scientific evidence
# you are required to adjust as appropriate for your device and research
MIN_BAT=10     # low water mark
MAX_BAT=60     # high water mark



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
if ! [[ "$(which acpi)" =~ (acpi) ]]; then
	echo "Please install acpi to use this script.\n"
	echo "   sudo pacman -S acpi"
	exit 1

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

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

    if [[ -n $1 ]]; then
        paplay ${1}

# 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"
            notify_sound $POWER_PLUG
	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"
            notify_sound $POWER_UNPLUG

	sleep ${INTERVAL} # Repeat every $INTERVAL seconds


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.

Run script as a user service

Create the folder ~/.config/systemd/user

mkdir ~/.config/systemd/user

Create a service unit using your favorite editor

xdg-open ~/.config/systemd/user/charge-notify.service

Paste below content and save the file

Description=Charger notify service

ExecStartPre=/bin/sleep 30


Enable and start the service

systemctl --user enable --now charge-notify.service

Enjoy :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

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.

The script now has a sound option.

usually “modern equipment” handle this life management as first class citizen. On IOS it is called “optimize battery charging” and on pinephone default is to only charge to 90% etc. (UI still shows 100%)

Your script causes problems in these cases, because if user thinks 80% of 80% is a sane “full conservative charge” they will be severely disappointed as in reality it’s only 64% of full charge.

Perhaps - but still there is a need for the equipment that doesn’t.

If you look at the defacto power management for linux tlp you will be surprised how few systems is actually supported.