Created
November 3, 2016 20:48
-
-
Save mpiannucci/45926772d9dd62f8226e805394889555 to your computer and use it in GitHub Desktop.
Get Battery Power Consumption on Linux
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/python | |
with open('/sys/class/power_supply/BAT0/status') as f: | |
if 'Charging' in f.read(): | |
print('The battery is currently charging') | |
exit(0) | |
scale = 1000000000000.000 | |
voltage = 0 | |
current = 0 | |
with open('/sys/class/power_supply/BAT0/voltage_now') as f: | |
voltage = int(f.read()) | |
with open('/sys/class/power_supply/BAT0/current_now') as f: | |
current = int(f.read()) | |
if current < 1 or voltage < 1: | |
print('Failed to read necessary battery information') | |
wattage = voltage * current / scale | |
print('Power Draw: ' + str(wattage) + ' Watts') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment