Skip to content

Instantly share code, notes, and snippets.

@earlonrails
Created March 5, 2014 18:57
Show Gist options
  • Save earlonrails/9374070 to your computer and use it in GitHub Desktop.
Save earlonrails/9374070 to your computer and use it in GitHub Desktop.
battery_temperature for mac osx 10.9.2 using ioreg
#!/bin/bash
# battery_temperature for mac osx 10.9.2
if [ `uname` == "Darwin" ]; then
if [ "$1" == "-h" ]; then
echo "Usage: $(basename $0 ".sh") [option]"
echo " -l also show Model Name and Processor Name"
echo " -h display this help message"
exit
fi
# -l display Model Name and Processor Name
if [ "$1" == "-l" ]; then
system_profiler SPHardwareDataType | awk '/Model Name/ || /Processor Name/' | sed 's/[^:]*: //' | awk '{ printf $0 " " }'
echo
fi
# sensor data
temperature=`ioreg -r -n AppleSmartBattery | grep Temperature | cut -c23-`
temperature_celsius=`echo $temperature / 100.0 | bc`
echo "AppleSmartBattery Temperature: "
echo $temperature_celsius °C
temperature_fahrenheit=`echo "($temperature_celsius * (9 / 5)) + 32.0" | bc`
echo $temperature_fahrenheit °F
# error message if unsupported machine
else
echo -e "\nThis appears not to be an OS X machine\n"
fi
@bkovitz
Copy link

bkovitz commented Jun 5, 2014

Thanks for this script!

One correction: The lines sent to bc need to start with scale = 2\; or you lose all fractions. Currently, the result of (9 / 5) is getting truncated to 1, so 30ºC is getting displayed as 62ºF, that is, 30 * 1 + 32.

@jondthompson
Copy link

bkovitz, could you either post a fixed gist or write the line(s) in question in the comments so that we can see what you mean?

@jondthompson
Copy link

Nevermind. Fixed in my fork.

@akofink
Copy link

akofink commented Nov 10, 2014

Thank you so much! 👍

@miguelPerezOntiveros
Copy link

what about cpu temp?

@redoacs
Copy link

redoacs commented Nov 5, 2018

Using iStats, you get a different reading for the battery temperature than using this. So i collected ˜10 days of by-minute data from both, ioreg and istats. Compared them, ran a linear regression and now i have the theory that the value from ioreg -r -n AppleSmartBattery is in Kelvin tenths. Does anyone have more info on these units ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment