IF-Blueprint Blog

Get low-battery push notification from your notebook

Imagine the situation while presenting your newest PowerPoint slide show in front of your customer, when your notebook’s screen suddenly goes black.
Reason: You missed the “Your battery is running critically low on power” message.

But you’re lucky: If you are an owner of a Microsoft Band (or any other push-compatible smart wear), here is a tiny proof-of-concept solution, preventing such a blame:

  1. Get yourself a free account at https://pushalot.com
  2. Remember the app token
  3. Get the appropriate app for your smartphone/smartwear at https://pushalot.com/apps and set it up
  4. Prepare your smartwear
    e.g. Microsoft Band needs the Notification hub enabled
  5. Copy the attached PowerShell file to a favorite location
  6. Update custom settings inside of the PowerShell script (at the very top):
    – $maxBatt: must be set to the value which is shown at full battery level.
    Run the script at 100% battery level to get your specific $maxBatt-Value. For example: 

    Status: Power on line and not loading.
    Remaining capacity: 38687 (100%) - sufficient.

    So your computer specific $maxBatt value is = 38687

    – $warnlevel: Level in percent at which a notification will be send (Recommendation: above 20%)
    – $pushalotAuthToken: Put your pushalot-App-token here

  7. Create a scheduled task with these settings:
    – Enable: Run whether user is logged on or not
    – Enable: Do not store password
    – Trigger: Run every day at 00:00 and repeat every 1 Minute for 1 day
    – Actions: Run program: 

    %SystemRoot%system32WindowsPowerShellv1.0powershell.exe

    Arguments: (Replace path to PowerShell script with your chosen path)

    -command "C:UsersusernameDocumentsPushalotBatteryWarn.ps1"

    – Conditions:
    — Disable: Start the task only if the computer is on AC
    — Enable: Start only if the following network connection is: Any connection
    – Settings:
    — Run task as soon as possible after a scheduled start is missed

 

Voila, done is your battery push notification!
From now on you will be reminded by your smartphone/smartwear, as soon as your notebook is at low battery. Minutes before any of your listeners will note, you’re aware of the raising situation and have plenty of time to plug your notebook in a power socket ūüôā

Remark 1:
Windows 10 is introducing the “Battery saving”-mode, which prevents scheduled tasks to run (default level: 20%). This is why you should choose a higher $warnlevel than 20%.

Remark 2:
This is proof-of-concept-code which is not fail safe. Use on your own risk.

PushalotBatteryWarn.ps1

#Custom options:
$maxBatt = 38687 #RemainingCapacity on 100% battery
$warnlevel = 25 #Warning level
$pushalotAuthToken = 'PutYourTokenHere'

###########################

$computer = "localhost"
$minBatt = $maxBatt / 100 * $warnlevel
$pushalotURI = "https://pushalot.com/api/sendmessage"
$regpath = "HKCU:SoftwarePushalotBatteryWatch"
$isOnline = [BOOL](Get-WmiObject -Class BatteryStatus -Namespace rootwmi -ComputerName $computer).PowerOnLine
$charging = [BOOL](Get-WmiObject -Class BatteryStatus -Namespace rootwmi -ComputerName $computer).Charging
$remaining = (Get-WmiObject -Class BatteryStatus -Namespace rootwmi -ComputerName $computer).RemainingCapacity 
$remainingPercents = $remaining / $maxBatt * 100
$remainingPercentsRounded = "{0:N0}" -f $remainingPercents

#Check registry, if already notified
if (!(Test-Path $regpath)) {
    New-Item -Path HKCU:Software -Name PushalotBatteryWatch
    New-ItemProperty -Path $regpath -name "Notified" -PropertyType String -Value "0"

}
$notified = Get-ItemProperty -Path $regpath -Name "Notified"
$notified = $notified.Notified -eq "1"

#Register event ##########################
#Create event log source, if it does not already exist. 
If ([System.Diagnostics.EventLog]::SourceExists("PowerShellPushAlot") -eq $false)  
    { 
        [System.Diagnostics.EventLog]::CreateEventSource("PowerShellPushAlot","Application") 
    } 
#Post event...
$logMsg = "Status: `r`n"
$logMsg += "Power plugged in: $isOnline `r`n"
$logMsg += "Is charging: $charging `r`n"
$logMsg += "Remaining capacity: $remaining ($remainingPercentsRounded%) `r`n"
$logMsg += "Warning level: $warnlevel% `r`n"
Write-EventLog -LogName "Application" -EntryType Information -EventId 0 -Source PowerShellPushAlot -Message $logMsg

#Remaining ##########################
Write-Host "Status: " -NoNewline
if (!$isOnline) {Write-Host "Discharging..."}
if ($isOnline -and $charging) {Write-Host "Power on line and charging..."}
if ($isOnline -and !$charging) {Write-Host "Power on line and not charging."}

Write-Host "Remaining capacity: $remaining ($remainingPercentsRounded%)" -NoNewline
if (!$isOnline -and ($remaining -le $minBatt)) 
{
    if ($notified) {
        Write-Host " - too low, but already notified."
    } else {

        Write-Host " - too low, sending notification..."

        Write-EventLog -LogName "Application" -EntryType Warning -EventId 1 -Source PowerShellPushAlot -Message "Battery too low, sending warning..."

        $json = @{
            AuthorizationToken = $pushalotAuthToken;
            Title = "$env:COMPUTERNAME Batteriewarnung";
            Body = "Battery is almost empty ($remainingPercentsRounded%)!";        
            IsImportant='TRUE'
        } | ConvertTo-Json    

        $response = Invoke-RestMethod -Method Post -Uri $pushalotURI -ContentType "application/json" -Body $json
        Set-ItemProperty -Path $regpath -name "Notified" -Value "1"
    }

} else {
    Write-Host " - sufficient."
    Set-ItemProperty -Path $regpath -name "Notified"  -Value "0"
}

Scheduled task propertiesPushalot Battery Watch Task Page 1

 

 

 

 

 

 

 

 

 

 

Pushalot Battery Watch Task Page 2Pushalot Battery Watch Task TriggerPushalot Battery Watch Task Page 3Pushalot Battery Watch Task ActionPushalot Battery Watch Task Page 4Pushalot Battery Watch Task Page 5