# Monitors battery usage (rate of energy charge/discharge)

while cat energy_now; do sleep 1; done |awk -v F=\$(cat energy_full) -v C=60 'NR==1{P=B=\$1;p=100/F} {d=\$1-P; if(d!=0&&d*D<=0){D=d;n=1;A[0]=B=P}; if(n>0){r=g=(\$1-B)/n;if(n>C){r=(\$1-A[n%C])/C}}; A[n++%C]=P=\$1; printf "%3d %+09.5f %+09.5f\n", p*\$1, p*g, p*r}'
Needs to be run in a battery sysfs dir, eg. /sys/class/power_supply/BAT0 on my system. Displays the battery's current charge and the rate per-second at which energy is {dis,}charging. All values are displayed as percentages of "full" charge. The first column is the current charge. The second is the rate of change averaged over the entire lifetime of the command (or since the AC cable was {un,}plugged), and the third column is the rate of change averaged over the last minute (controlled by the C=60 variable passed to awk). The sample output captures a scenario where I ran 'yes' in another terminal to max out a CPU. My battery was at 76% charge and you can see the energy drain starts to rise above 0.01% per-second as the cpu starts working and the fan kicks in etc. While idle it was more like 0.005% per-second. I tried to use this to estimate the remaining battery life/time until fully charged, but found it to be pretty useless... As my battery gets more charged it starts to charge slower, which meant the estimate was always wrong. Not sure if that's common for batteries or not.
Sample Output
``` 76 -00.00570 -00.00688
76 -00.00567 -00.00626
76 -00.00585 -00.00751
76 -00.00582 -00.00751
76 -00.00589 -00.00813
76 -00.00585 -00.00751
76 -00.00602 -00.00876
76 -00.00599 -00.00876
76 -00.00606 -00.00939
76 -00.00603 -00.00876
76 -00.00619 -00.01001
```

-1
2015-09-19 15:45:40

### What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.