Created
March 15, 2026 14:16
-
-
Save e-minguez/c99f4224240658262edd13062521cc10 to your computer and use it in GitHub Desktop.
//github.com/equaeghe/batenergy fork with some fixes and a notify-send
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| FILE=/tmp/batenergy.dat | |
| ADP=(/sys/class/power_supply/A*) | |
| BAT=(/sys/class/power_supply/BAT*) | |
| USER="edu" | |
| USERID=$(id -u ${USER}) | |
| [[ -e $BAT ]] || exit | |
| state=$1 | |
| sleep_type=$2 | |
| if [[ $state == "post" ]]; then | |
| sleep 2 | |
| fi | |
| now=`date +'%s'` | |
| # Read an energy in mWh from /sys/class/power_supply. | |
| # Some firmware only reports charge (in µAh), in which case we convert using a stable voltage. | |
| read_energy() { | |
| local when=$1 | |
| local -n var=energy_$when | |
| if [[ -e $BAT/energy_$when ]]; then | |
| (( var = $(< "$BAT"/energy_$when) / 1000 )) # mWh | |
| else | |
| if [[ -z $voltage_ref ]]; then | |
| if [[ -e $BAT/voltage_min_design ]]; then | |
| voltage_ref=$(< "$BAT"/voltage_min_design) | |
| else | |
| voltage_ref=$(< "$BAT"/voltage_now) | |
| fi | |
| fi | |
| (( var = $(< "$BAT"/charge_$when) * voltage_ref / 1000000000 )) # mWh | |
| fi | |
| } | |
| read_energy now | |
| read_energy full | |
| if [[ -f $ADP/online ]]; then | |
| read online < "$ADP"/online | |
| if (( online )); then | |
| echo "Currently on mains." | |
| else | |
| echo "Currently on battery." | |
| fi | |
| fi | |
| case $state in | |
| "pre") | |
| echo "Saving time (${now}) and battery energy (${energy_now}) before sleeping ($sleep_type)." | |
| echo $now > $FILE | |
| echo $energy_now >> $FILE | |
| ;; | |
| "post") | |
| exec 3<>$FILE | |
| read prev <&3 | |
| read energy_prev <&3 | |
| rm $FILE | |
| time_diff=$(($now - $prev)) # seconds | |
| if (( time_diff <= 0 )); then time_diff=1; fi | |
| days=$(($time_diff / (3600*24))) | |
| hours=$(($time_diff % (3600*24) / 3600)) | |
| minutes=$(($time_diff % 3600 / 60)) | |
| (( energy_diff = energy_now - energy_prev )) # mWh | |
| (( avg_rate = energy_diff * 3600 / time_diff )) # mW | |
| energy_diff_pct=$(bc <<< "scale=1;$energy_diff * 100 / $energy_full") # % | |
| avg_rate_pct=$(bc <<< "scale=2;$avg_rate * 100 / $energy_full") # %/h | |
| MESSAGE="Duration of $days days $hours hours $minutes minutes sleeping ($sleep_type). | |
| Energy difference = ${energy_now} - ${energy_prev} | |
| Battery energy change of $energy_diff_pct % ($energy_diff mWh) at an average rate of $avg_rate_pct %/h ($avg_rate mW)." | |
| echo "$MESSAGE" | |
| # Wait for the user session and notification daemon to be fully ready after thaw | |
| sleep 5 | |
| sudo -u ${USER} DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/${USER}/bus notify-send "Battery resume stats" "${MESSAGE}" | |
| ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment