Skip to content

Instantly share code, notes, and snippets.

@mpiannucci
Created November 3, 2016 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpiannucci/45926772d9dd62f8226e805394889555 to your computer and use it in GitHub Desktop.
Save mpiannucci/45926772d9dd62f8226e805394889555 to your computer and use it in GitHub Desktop.
Get Battery Power Consumption on Linux
#!/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