• Create a task based on battery level

    Home » Forums » AskWoody support » Windows » Windows 7 » Questions: Windows 7 » Create a task based on battery level

    Author
    Topic
    #488079

    I usually charge my laptop battery to 80% before disconnecting the charger so as to increase the battery life. The problem is that I often forget to check the battery level while charging and find out hours later that it’s been at 100%. I would like to create a task in Task Scheduler that displays a message when the battery level reaches 80%. But I don’t know how to add “battery level at x%” as a trigger for the task. I would appreciate your help very much.

    Viewing 13 reply threads
    Author
    Replies
    • #1377931

      There’s no such Task Scheduler ‘trigger’, so you probably need to run a utility which will pop up a message when your required battery threshold is reached. I found Battery Status Tool, which may or may not do what you want.

      The usual BatteryCare might perhaps assist, alternatively.

      BATcher

      Plethora means a lot to me.

    • #1377932

      If the battery is Lithium Ion there is no need to bother. These batteries have no discernible memory effect and full charge doesn’t reduce life.

      If you still want to play I suggest you use WMI – built in to Windows – to check the current level. Download Scriptomatic to play with WMI.

      cheers, Paul

    • #1378307

      I tried both of those tools but neither offer the required function.

      I downloaded Scriptomatic 2.0 but it doesn’t run properly for some reason. It’s an html application and while it launches properly, it gives a “script error on this page, do you want to continue” error akin to the old days when I used to use Internet Explorer on an incompatible site (screenshot). I am running Windows 7 Home Premium and have Internet Explorer 9 (though I use Firefox as my browser). Because of this problem, most of the buttons are not usable (the drop down lists for example).

      Is it because the Scriptomatic is outdated or that my Windows version (home premium) lacks some component?

      • #1378312

        I tried both of those tools but neither offer the required function.

        I downloaded Scriptomatic 2.0 but it doesn’t run properly for some reason. It’s an html application and while it launches properly, it gives a “script error on this page, do you want to continue” error akin to the old days when I used to use Internet Explorer on an incompatible site (screenshot). I am running Windows 7 Home Premium and have Internet Explorer 9 (though I use Firefox as my browser). Because of this problem, most of the buttons are not usable (the drop down lists for example).

        Is it because the Scriptomatic is outdated or that my Windows version (home premium) lacks some component?

        I suspect that you need win 7 Pro minimum to get it to work as Home premium does not allow access to some of the hidden system settings. (GU-edit i think it may be called, maybe some one can correct this if incorrect as very likely).

    • #1378325

      I think you’re referring to the General Policy Editor (gpedit) that is not available in the Home Premium version Clive.

      Jerry

    • #1378460

      Scriptomatic requires you to have admin credentials to run it. If you have UAC enabled you need to run it from an administrator Command Prompt.
      Right click on “Command Prompt” and select “Run as administrator”.
      Enter the full path to Scriptomatic, e.g. D:TempScriptomaticscriptomaticv2.hta

      cheers, Paul

    • #1378471

      asifbaig,

      Here’s a possible solution. It may not be pretty but it does work.
      Place the following PowerShell program in your default PowerShell Directory.

      Code:
      Param (
       [Parameter (Mandatory=$False)]
         $BaseCapacity
         )
      
      [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
      [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
      
      $MyBattery = Get-WmiObject -class BatteryStatus -ComputerName LocalHost -NameSpace  rootWMI
      $BatteryRemaining = $MyBattery.RemainingCapacity/$BaseCapacity
      
      if(($BatteryRemaining -lt 0.87) -and $MyBattery.Discharging) {
         $Message = "Battery Low...Please Charge Me!"
        [Windows.Forms.MessageBox]::Show($Message, "Battery Status", `
          [Windows.Forms.MessageBoxButtons]::OK , `
          [Windows.Forms.MessageBoxIcon]::Information , `
          [Windows.Forms.MessageBoxDefaultButton]::Button1, `
          [Windows.Forms.MessageBoxOptions]::ServiceNotification)
      }
      

      Note code also included in attachment.

      Next create a scheduled task that will fire every hour (or time interval of your choice ).
      33333-TSAction
      Note: You need to pass to the program {after the program name in the Action pane of the scheduled task } the mWh ( milla Watt Hours ) FULL capacity of your battery! I used the Battery Meter program to find this value. So the optional parameters would look like d:pathBatteryStatus.ps1 7730

      Done!

      When the task fires you will get the following message if the battery is below the threshold and you are NOT plugged in.
      33332-LowBatteryMsg
      See the messy part it shows a command window along with the message and that window will flash even if the message doesn’t display. When you click OK both windows close.

      The code as written checks for low battery since that was the easiest to check as I was writing the code. You can easily change the test from -le to -ge and also the percentage that follows to your desired threshold levels. You could also make the code check for both low & high charge conditions and print the appropriate message. BTW don’t forget to change the $Message value as appropriate.

      HTH :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1378944

      Hey Y’all,

      Here’s the latest version that kicks out messages for both Unplugged/Low Battery and Plugged in Charged Battery:

      Code:
      Param (
       [Parameter (Mandatory=$False)]
         $BaseCapacity
         )
      
      [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
      [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
      
      $MyBattery = `
       Get-WmiObject -class BatteryStatus -ComputerName LocalHost -NameSpace  rootWMI
      $BatteryRemaining = $MyBattery.RemainingCapacity/$BaseCapacity
      $Message = ""
      
      if(($BatteryRemaining -lt 0.30) -and $MyBattery.Discharging) {
         $Message = "Battery Low...Please Charge Me!"
      }
      Elseif(($BatteryRemaining -gt 0.95) -and $MyBattery.PowerOnline) {
         $Message = "Battery CHARGED above 95%...Please Unplug Me!"
      }
      
      if($Message -ne "") {
        [Windows.Forms.MessageBox]::Show($Message, "Battery Status", `
          [Windows.Forms.MessageBoxButtons]::OK , `
          [Windows.Forms.MessageBoxIcon]::Information , `
          [Windows.Forms.MessageBoxDefaultButton]::Button1, `
          [Windows.Forms.MessageBoxOptions]::ServiceNotification)
      }
      
      
      

      :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1379509

      Thank you very much, good sir. I will try this out and let you know how it goes. My exams are close so it may be a while before I get around to doing this but I will definitely report the results.

    • #1409662

      Aaaaaand I’m back. In less than half a year. As promised. 😉

      I tried the above mentioned script and I got this error:

      Cannot find the type for custom attribute ‘Parameter ‘. Make sure that the asse
      mbly that contains this type is loaded.
      At D:StuffUnsortedPowershellchargealert.ps1:10 char:13
      + [Parameter <<<< (Mandatory=$False)]
      + CategoryInfo : InvalidOperation: (Parameter :Token) [], Runtime
      Exception
      + FullyQualifiedErrorId : CustomAttributeTypeNotFound

      It appears that I need to install some module or enable some setting. Kindly let me know what I'm doing wrong.

    • #1409683

      Could you please post your version of the .ps1 file? :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1409814

      I copied all the text that you gave, saved it to a text file and named it chargealert.ps1.

      I’ll paste the text below. In case there’s some formatting error on this website, you can get the actual file from here: https://dl.dropboxusercontent.com/u/16631047/Test/chargealert.ps1

      Code:
      Param (
       [Parameter (Mandatory=$False)]
         $BaseCapacity
         )
      
      [void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
      [void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
      
      $MyBattery = `
       Get-WmiObject -class BatteryStatus -ComputerName LocalHost -NameSpace  rootWMI
      $BatteryRemaining = $MyBattery.RemainingCapacity/$BaseCapacity
      $Message = “”
      
      if(($BatteryRemaining -lt 0.40) -and $MyBattery.Discharging) {
         $Message = “Battery Low…Please Charge Me!”
      }
      Elseif(($BatteryRemaining -gt 0.85) -and $MyBattery.PowerOnline) {
         $Message = “Battery CHARGED above 95%…Please Unplug Me!”
      }
      
      if($Message -ne “”) {
        [Windows.Forms.MessageBox]::Show($Message, “Battery Status”, `
          [Windows.Forms.MessageBoxButtons]::OK , `
          [Windows.Forms.MessageBoxIcon]::Information , `
          [Windows.Forms.MessageBoxDefaultButton]::Button1, `
          [Windows.Forms.MessageBoxOptions]::ServiceNotification)
      }
      
    • #1409843

      Asifbaig,

      Ok, here goes with a “stupid” question. 😆 You are running this on a machine that has a battery?
      I tried it on my desktop and got errors although not the same as yours:
      [noparse]
      Get-WmiObject : Invalid class “BatteryStatus”
      At G:bekdocsscriptsBatteryStatusV3.ps1:19 char:2
      + Get-WmiObject -class BatteryStatus -ComputerName LocalHost -NameSpace rootWMI
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidType: (:) [Get-WmiObject], ManagementException
      + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
      [/noparse]
      But it ran just fine on my laptop.
      34840-BatteryStatus
      HTH :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1410147

      Yeah, I ran it on my laptop with the battery still attached to it (either that or my desktop became self-aware, disguised itself and is planning to kill me 😉 ).

      And surprisingly, I ran it now and it runs fine. I restarted the system in between and that must have evacuated the gremlins… 🙂 Thank you so much for your help.

    • #1410148

      Glad you got it sorted! :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    Viewing 13 reply threads
    Reply To: Create a task based on battery level

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: