Is there an app or existing command to report the battery time since the laptop was last unplugged from AC but exlude the time spend during sleep on battery?
This can be inferred from pmset -g log
so I'm looking for a way to do this without using an app that stays resident to detect plug/unplug events.
Best Answer
Well, I'll provide an answer to my own question, tested on Yosemite but not extensively. This is bash code:
I won't vouch for its accuracy but it seems to work. Since I don't know Apple's internals (I'm new to Mac OS X) I reversed engineered it with a few trial and errors plugging and unplugging the adapter, both while asleep and also while awake, to see which log entries are reliable.
You will notice that I tried using
pmset -g log
to detect when the power was unplugged but it's unreliable. Upon unplugging, the log entry isSummary- [System: DeclUser kDisp] Using Batt
but unfortunately the same log entry occurs during sleep while unplugged so it's unreliable. I resorted tosyslog
instead.NOTE: This reports the time since the last unplugged event. This may not be when the battery was 100 %.
Potential issue: The above code takes the timestamp from syslog and matches against pmset. If pmset doesn't have an entry (any entry) at the same timestamp then it won't work. That shouldn't happen but if it does, then you could convert the timestamps to epoch seconds and detecting the closest timestamp in pmset that way.