Create a gist now

Instantly share code, notes, and snippets.

Embed
Linux Battery and Power Usage Monitoring Script
#!/usr/bin/env python2
import os
def readfile(path):
f = open(path, 'r')
if not f:
return false
val = f.read()
f.close()
return val
status = readfile('/sys/class/power_supply/BAT0/status').rstrip('\n');
if os.path.isfile('/sys/class/power_supply/BAT0/power_now'):
power = readfile('/sys/class/power_supply/BAT0/power_now')
power = float(power) / 1000000
elif os.path.isfile('/sys/class/power_supply/BAT0/current_now') and os.path.isfile('/sys/class/power_supply/BAT0/voltage_now'):
current = readfile('/sys/class/power_supply/BAT0/current_now')
current = float(current) / 1000000
voltage = readfile('/sys/class/power_supply/BAT0/voltage_now')
voltage = float(voltage) / 1000000
power = current * voltage
else:
print("Not Supported")
exit(1)
charge_now = float(readfile('/sys/class/power_supply/BAT0/charge_now'))
charge_full = float(readfile('/sys/class/power_supply/BAT0/charge_full'))
charge_full_design = float(readfile('/sys/class/power_supply/BAT0/charge_full_design'))
charge_percent = round((charge_now / charge_full) * 100)
charge_percent_design = round((charge_now / charge_full_design) * 100)
print status + ': ' + str(round(power, 2)) + " W (" + str(charge_percent) + "%) design(" + str(charge_percent_design) + "%) " + str(round(charge_now / 100000, 1)) + " Wh remaining"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment