TRIGGERcmd
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Laptop Battery Management - Automated On/Off Cycle

    Example Commands
    6
    9
    1.8k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Philip NguyenP
      Philip Nguyen
      last edited by

      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 Skill

      Appreciate any assistance or guidance on how to make this efficient. Thanks all!

      RussR 1 Reply Last reply Reply Quote 0
      • RussR
        Russ @Philip Nguyen
        last edited by Russ

        @philip-nguyen, there's a similar question here.

        These are the high level steps:

        1. Create 2 commands that just run echo (doesn't matter what), and name them PlugOn and PlugOff.
        2. 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.
        3. Create a script. At the beginning of the script, do the following:
        4. Set variable called PLUG_IS_ON with a default value of true.
        5. To make sure the plug is on at first, run the tcmd utility like this: tcmd -t plugon -c (your TRIGGERcmd computer name)
        6. Create a loop that repeats every minute. Inside the loop, do the following:
        7. Check your battery level with BatteryInfoView.exe
        8. 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)
        9. 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)
        10. 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.

        Russell VanderMey

        Philip NguyenP 1 Reply Last reply Reply Quote 3
        • Philip NguyenP
          Philip Nguyen @Russ
          last edited by

          @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!

          RussR 1 Reply Last reply Reply Quote 3
          • RussR
            Russ @Philip Nguyen
            last edited by Russ

            @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
            

            Russell VanderMey

            Cristiana FernandesC xlPatriciaX 2 Replies Last reply Reply Quote 0
            • Cristiana FernandesC
              Cristiana Fernandes @Russ
              last edited by

              @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
              
              1 Reply Last reply Reply Quote 1
              • Chambers DavidC
                Chambers David
                last edited by

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • edupontocpsE
                  edupontocps
                  last edited by

                  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 plugOff

                  The file plugOn.bat has the code
                  tcmdWin.exe --computer your_name_computer --trigger plugOn

                  RussR 1 Reply Last reply Reply Quote 1
                  • RussR
                    Russ @edupontocps
                    last edited by

                    @edupontocps, nice. This is simpler.

                    Russell VanderMey

                    1 Reply Last reply Reply Quote 0
                    • xlPatriciaX
                      xlPatricia @Russ
                      last edited by xlPatricia

                      Hello! I was using this script on my laptop and it was working normally. After formatting it this afternoon, I reinstalled everything again and got tcmd to work as it was before, but now it is not reading the battery %, it just keeps activating plugOn all the time regardless of how much battery is left.

                      I don't understand scripts, so I apologize in advance for not being able to provide more information or details. If anyone can help me, I would be grateful.

                      Just to let you know, I followed all the steps described here. I have tcmd utility in the C:\Windows folder, I downloaded BatteryInfoView.exe, I configured plugOn and plugOff in trigger_cmd and in Alexa and even so only plugOn is activated in a loop with Alexa's routines.

                      Oh, and I apologize for having revived the topic. I thought it best to leave everything in the same place.

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post