Last active
February 7, 2024 19:01
-
-
Save osipxd/0e0d3372aeceaeb6f68e7364b704b5a7 to your computer and use it in GitHub Desktop.
Print current Mac wattage (with accuracy up to minute)
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 | |
set -euo pipefail | |
function extract_float_field() { | |
field=$1 | |
ioreg -rw0 -c AppleSmartBattery | | |
grep BatteryData | | |
sed -E "s/.*\"$field\"=([0-9]+).*/\1/" | | |
xargs -I % lldb --batch --source-quietly --one-line "print/f %" | # Convert IEEE-754 float | |
grep --only-matching --extended-regexp '[0-9]+(\.[0-9]+)?' | |
} | |
printf -- 'Charging (watt): ' | |
extract_float_field AdapterPower | |
printf -- 'Consumption (watt): ' | |
extract_float_field SystemPower |
This works fine on an Intel Mac.
On a M1, M2 or M3 machine the script needs some adjustments due toxargs
yielding slightly different output:
Thank you! Just revised the script to make it not relying on lldb output format. However, I've not tested it on an Intel Mac. Will you be able to test it there?
Thanks, the script outputs the wattage nicely on an Intel based MacBook Pro.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works fine on an Intel Mac.
On a M1, M2 or M3 machine the script needs some adjustments due to
xargs
yielding slightly different output:Hence the lines 11 and 12 need to be changed to: