Skip to content

Instantly share code, notes, and snippets.

@mateosss
Created September 22, 2020 13:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mateosss/8ae97cca9be0e731b8aa2f84d8432533 to your computer and use it in GitHub Desktop.
Save mateosss/8ae97cca9be0e731b8aa2f84d8432533 to your computer and use it in GitHub Desktop.
Shows laptop power usage in realtime
#!/usr/bin/python3
from time import time, sleep
itime = time()
totcurrent = 0
totvoltage = 0
samples = 0
try:
while True:
current = int(open("/sys/class/power_supply/BAT0/current_now", "r").read()) / 1000000.0
voltage = int(open("/sys/class/power_supply/BAT0/voltage_now", "r").read()) / 1000000.0
samples += 1
totcurrent += current
avgcurrent = totcurrent / samples
totvoltage += voltage
avgvoltage = totvoltage / samples
print(f"current={current:.2f}A voltage={voltage:.2f}V watts={current*voltage:.2f}W{' ' * 20}")
print(f"[avg of {time() - itime:.2f}s] current={avgvoltage:.2f}A voltage={avgcurrent:.2f}V watts={avgcurrent*avgvoltage:.2f}W\r", end="")
sleep(1)
except KeyboardInterrupt:
print("\n")
@nrnw
Copy link

nrnw commented Oct 18, 2022

current_now not available for zorin os. Voltage can view. Nice work.

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