Laptop Battery Management - Automated On/Off Cycle
-
Not sure if this is the right forum to post help requests, let me know if I'm at the right place! I have a new concept that has been on my mind for weeks now but I can't figure out how to effectively execute it. Hopefully someone here can help!
The Problem:
Work laptop has always been plugged in for the past 2 years working from home. It is undervolted and has an SSD, so my laptop is truly on 24/7. No performance or overheating issues, and having it always on is really useful for remote desktop access from my phone whenever I need it. The issue however is the battery degradation from being plugged in all the time. Fairly certain that because it was always topped off and rarely cycle charged, it has caused the laptop to abruptly shut off whenever unplugged for more than 3 minutes. Replaced the battery and all is good now, but I want to prevent this from happening again in the future.Proposed Idea:
I can plug the laptop charger into a Smart Plug (Alexa controlled). Using TriggerCMD, I would like to turn off the plug when the laptop battery is at 100%, and back on when it reaches 30%.Nice to Have:
Hoping I could have this configured to only execute outside my normal work hours since performance settings would change on battery mode. Also thinking that the battery only needs to be cycled at most once per week.Hardware/Software:
Laptop w/ Windows 10
TriggerCMD Desktop App
Alexa Assistant
Amazon Smart Plug
TriggerCMD Alexa Skill
TriggerCMD Alexa Smart Home SkillAppreciate any assistance or guidance on how to make this efficient. Thanks all!
-
@philip-nguyen, there's a similar question here.
These are the high level steps:
- Create 2 commands that just run echo (doesn't matter what), and name them PlugOn and PlugOff.
- Setup two Alexa routines - one that turns on your smart plug when the PlugOn command runs, and other that turns it off when the PlugOff command runs.
- Create a script. At the beginning of the script, do the following:
- Set variable called PLUG_IS_ON with a default value of true.
- To make sure the plug is on at first, run the tcmd utility like this: tcmd -t plugon -c (your TRIGGERcmd computer name)
- Create a loop that repeats every minute. Inside the loop, do the following:
- Check your battery level with BatteryInfoView.exe
- When the level is below 30 percent and PLUG_IS_ON = false, run tcmd utility like this: tcmd -t plugon -c (your TRIGGERcmd computer name)
- When the level is above 99 percent and PLUG_IS_ON = true, run tcmd utility like this: tcmd -t plugoff -c (your TRIGGERcmd computer name)
- Also set PLUG_IS_ON to true when you run plugon, and set it to false when you run plugoff
I think that should work. Do you know how to do that? Let me know if you get stuck, and where you get stuck. I can look at your script if you paste it here.
-
@russ Awesome thank you Russ! Reading through your post as well as the other thread now. I'll definitely post up my version if it works or if I get stuck. Thanks again!
-
@Philip-Nguyen, you could use a powershell script like this:
$low_battery_level = 20 $full_battery_level = 99 $charge = Get-CimInstance -ClassName Win32_Battery | Select-Object -ExpandProperty EstimatedChargeRemaining "Current Charge: $charge %." while($true) { if ($charge -ge $full_battery_level) { "Laptop battery is full. Turning the Smart Plug off." tcmd --computer laptop --trigger plugOff } if ($charge -le $low_battery_level) { "Laptop battery is low. Turning the Smart Plug on to recharge your laptop." tcmd --computer laptop --trigger plugOn } Start-Sleep -Seconds 600 }
That script assumes your laptop has the tcmd tool in a folder that's in your PATH, like c:\windows.
See my post above about setting up the 2 Alexa routines you'd associate with your plugOn and plugOff commands.
You can run that powershell script with a command like this:
powershell -file battery_monitor.ps1
-
@Russ Everything worked so fine to me but I was worried about the powershell window because it has to be always minimized on your taskbar so I found this article helping hiding it at startup just by adding the following code in the beginning of the script to run it in background:
# .Net methods for hiding/showing the console in the background Add-Type -Name Window -Namespace Console -MemberDefinition ' [DllImport("Kernel32.dll")] public static extern IntPtr GetConsoleWindow(); [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow); ' function Hide-Console { $consolePtr = [Console.Window]::GetConsoleWindow() #0 hide [Console.Window]::ShowWindow($consolePtr, 0) } Hide-Console
-
This post is deleted! -
Hey guys,
I created this process in Python and I don't need to use the BatteryInfoView.exe program.
I have Windows 11 and I'm using the "Task Scheduler" function to run in a loop that repeats itself every 5 minutes.Here is the script
import psutil, os, time bateria = psutil.sensors_battery() plugado = bateria.power_plugged if plugado: if bateria.percent == 100: os.startfile('D:\Downloads\Bateria\plugOff.bat') time.sleep(10) else: pass else: if bateria.percent <= 20: os.startfile('D:\Downloads\Bateria\plugON.bat') time.sleep(10) else: pass
Where
The file plugOff.bat has the code
tcmdWin.exe --computer your_name_computer --trigger plugOffThe file plugOn.bat has the code
tcmdWin.exe --computer your_name_computer --trigger plugOn -
@edupontocps, nice. This is simpler.