Method of laptop conditional power management in Windows 11
Solution 1:
I ended up writing a python script to manage this. It requires only python, does not need admin rights, and no additional modules are required.
import subprocess
import time
import datetime
battery_watermark = 75
sleep_on_battery_mins = 20
def set_standby_timeout_dc(mins):
return subprocess.call("powercfg -change -standby-timeout-dc " +str(mins))
def battery_precentage():
return int(subprocess.run(["WMIC", "PATH", "Win32_Battery", "Get", "EstimatedChargeRemaining"], capture_output=True).stdout.decode().split("\n")[1][:3].strip())
def on_battery():
#BatteryStatus returns 2 when connected to AC.
charging_status = int(subprocess.run(["WMIC", "PATH", "Win32_Battery", "Get", "BatteryStatus"], capture_output=True).stdout.decode().split("\n")[1].strip())
if charging_status == 2:
return False
else:
return True
print("Windows Power settings changer")
starttime = time.time()
while True:
if (battery_precentage() > battery_watermark) and on_battery():
print(str(datetime.datetime.now()) + ": Setting on-battery standby timeout to 0, battery at " + str(battery_precentage()) + "%, on battery: " + str(on_battery()))
set_standby_timeout_dc(0)
else:
print(str(datetime.datetime.now()) + ": Setting on-battery standby timeout to " + str(sleep_on_battery_mins) + ", battery at " + str(battery_precentage()) + "%, on battery: " + str(on_battery()))
set_standby_timeout_dc(sleep_on_battery_mins)
time.sleep(((sleep_on_battery_mins/2)-1)*60 - ((time.time() - starttime) %60))