(no title)
Nephx | 3 years ago
#!/usr/bin/python3
battery_directory = "/sys/class/power_supply/BAT1/"
with open(battery_directory + "status", "r") as f:
state = f.read().strip()
with open(battery_directory + "current_now", "r") as f:
current = int(f.read().strip())
with open(battery_directory + "voltage_now", "r") as f:
voltage = int(f.read().strip())
wattage = (voltage / 10**6) * (current / 10**6)
wattage_formatted = f"{'-' if state == 'Discharging' else ''}{wattage:.2f}W"
if state in ["Charging", "Discharging", "Not charging"]:
print(f"{state}: {wattage_formatted}")
Output:Charging: 32.15W
Discharging: -5.15W
chrismorgan|3 years ago
I have in my shell history which I occasionally use:
(With ^[ being actual escape, entered via Ctrl-V Escape, because I find writing the escape codes literally easier and more consistent than using echo -e or whatever else.)It’ll show lines like this every minute (with nice colouring):
My PinePhone has an axp20x-battery with current_now and voltage_now, like your various laptops except that while discharging it gets a negative current_now, which makes perfect sense to me but which doesn’t seem to match your laptops (since you add the negative sign manually in your script) or my laptop’s power_now (which is likewise still positive while discharging).