Skip to main content

Laptop Low Battery Smartphone Push Notification

This post is part of the Into The Past series. A list of posts written long after they happened as part of an attempt to look back at some earlier creations and thoughts I had along the way. This post was written April 2024.

Preface #

Laptop Power Push Notification preview

This is a small gem that I had created which ended up serving me for several years before I moved on to use Mac as my primary OS. I'm sure a number of people would agree that the default power options in Windows can feel very aggressive, and putting a laptop into sleep mode after a few minutes of inactivity can often lead to annoying situations if you are running some long-winded process. I had often run into issues with this. So please allow me to lead this post with the same quote that the original Instructable is served with:

When your laptop is sitting on a table and you're doing some other stuff in the meanwhile, it might quietly be running out of battery. In that case, you likely want to be nudged to find it a power outlet before it has to shut down or hibernate. Wouldn't it be nice to get a push message on your phone (or even smartwatch, because hey, it's 2015)? Quote from Dammit.nl

The full, original step by step guide on setting this up can be found at Instructables.com

AutoIt Scripting Language #

This small utility is written in the AutoIt Scripting Language which, although old, at the time was a very neat language for automating tasks on Windows. Highlighting the below quote from from their own site might give an immediate indication of the utilities that this automation language can serve:

AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages.

The script itself was released on Pastebin along with the Instructables guideline, and can be found in the section below. Pushbullet is used as a mediator to receive push notification, and can be installed on any smart device.

https://pastebin.com/NnEAe8HE

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.12.0
 Author:         Michael Cavaleri
			     michael@cavaleri.dk

 Script Function:
    This script will run once every 5 minute. You can easily change this to
	run more frequent or have it delayed some. I thought 5 minutes seemed
	like a decent amount of time to balance resource usage and effectivity.

	You will receive a notification every 5 minutes when your laptop is
	running low on battery.

	This script will check if your computer is connected to the internet.
	If this is the case, then it will check your current battery status.
	If the power level is 20% or less (this amount being easy to change),
	then it will fire a notification to	your smartphone or tablet or whatever
	device you are hooking it up to	on PushBullet.

	The script will only fire a notification, if your laptop is not hooked
	up to a power source. By default, it will fire a notification to all of
	your devices on PushBullet. However, you can change it to a single device
	by changing $vSendToAll to False and entering your Device ID in $vDeviceID

	Instructions:
    1. Press CTRL+F and search for "change these variables".
	2. Change each variable to fit your needs.
	3. Press F7 to compile this script into an executable.
	4. Run the executable to receive notifications.

#ce ----------------------------------------------------------------------------

; Include the necessary libraries:
#include <WinAPISys.au3>


; Change these variables
$vAccessToken = "ACCESS TOKEN"	; You can find your access token in http://www.pushbullet.com/account
$vTimer = 300000 				; This is the amount of time it waits between checking battery status. 1000 = 1 second.
$vPowerPercentageAlert = 20 	; This is the power level percentage where you want to receive the notification.
$vSendToAll = True				; Change this to False if you only want notifications on a single device.
$vDeviceID = ""					; Change this to the ID of the device if you only want notifications on one devices.

#region ### CREATE TRAY MENU ###
Opt("TrayMenuMode", 3)
Opt("TrayOnEventMode", 1)
$iExit = TrayCreateItem("Exit")
#endregion ### CREATE TRAY MENU ###

TrayItemSetOnEvent($iExit, "Close")


While 1
   If _IsInternetConnected() Then
	  $vPowerStatus = _WinAPI_GetSystemPowerStatus()
	  If $vPowerStatus[0] = 0 Then
		 if $vPowerStatus[2] <= $vPowerPercentageAlert Then
			if $vSendToAll = True Then
			   $sPD = '{"type": "note", "title": "Laptop Battery Low", "body": "Your laptop only has ' & $vPowerStatus[2] &'% power left."}'
			ElseIf $vSendToAll = False Then
			   $sPD = '{"device_iden": "' & $vDeviceID & '", "type": "note", "title": "Laptop Battery Low", "body": "Your laptop only has ' & $vPowerStatus[2] &'% power left."}'
			EndIf
			$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
			$oHTTP.Open("POST", "https://api.pushbullet.com/v2/pushes", False)
			$oHTTP.setRequestHeader("Authorization", "Bearer " & $vAccessToken)
			$oHTTP.SetRequestHeader("Content-Type", "application/json")
			$oHTTP.Send($sPD)
		 EndIf
	  EndIf
   EndIf
   Sleep($vTimer)
WEnd


#Region ### The Functions ###
Func Close()
   Exit
EndFunc

Func _IsInternetConnected()
    Local $aReturn = DllCall('connect.dll', 'long', 'IsInternetConnected')
    If @error Then
        Return SetError(1, 0, False)
    EndIf
    Return $aReturn[0] = 0
EndFunc   ;==>_IsInternetConnected
#EndRegion