Last active
October 27, 2024 17:44
-
-
Save mateosss/8ae97cca9be0e731b8aa2f84d8432533 to your computer and use it in GitHub Desktop.
Shows laptop power usage in realtime
This file contains 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/python3 | |
from time import time, sleep | |
from pathlib import Path | |
itime = time() | |
totcurrent = 0 | |
totvoltage = 0 | |
totpower = 0 | |
samples = 0 | |
voltage_path = Path("/sys/class/power_supply/BAT0/voltage_now") | |
current_path = Path("/sys/class/power_supply/BAT0/current_now") | |
power_path = Path("/sys/class/power_supply/BAT0/power_now") | |
energy_path = Path("/sys/class/power_supply/BAT0/energy_now") | |
use_current = current_path.exists() | |
try: | |
while True: | |
voltage = int(open(voltage_path, "r").read()) / 1e6 # [µV] | |
if use_current: # Some computers have one some have the other | |
current = int(open(current_path, "r").read()) / 1e6 # [µA] | |
power = current / voltage # [µW] | |
else: | |
power = int(open(power_path, "r").read()) / 1e6 | |
current = power / voltage | |
remaining_joules = int(open(energy_path, "r").read()) / 1e6 # [µJ / h] | |
samples += 1 | |
totcurrent += current | |
avgcurrent = totcurrent / samples | |
totvoltage += voltage | |
avgvoltage = totvoltage / samples | |
totpower += power | |
avgpower = totpower / samples | |
remaining_hs = remaining_joules / power | |
print(f"{current:.2f}A {voltage:.2f}V {power:.2f}W {' ' * 15}") | |
print(f"[AVG over {time() - itime:.2f}s / {remaining_hs:.2f}hs left] {avgcurrent:.2f}A {avgvoltage:.2f}V {avgpower:.2f}W\r", end="") | |
sleep(1) | |
except KeyboardInterrupt: | |
print("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
current_now not available for zorin os. Voltage can view. Nice work.