Controlling Printer Outlet

We normally keep our printer turned off. Residential printer standby can have a decent draw. It’s something you have to research specific to your printer — some have low single-digit standby draw and waste ink when powered on and off. Others, like ours, has a non-trivial standby draw that isn’t offset by ink savings. The problem is that you’ve got to turn the printer on, print your stuff, and then remember to turn it off. The tiny person remote power controller (i.e. Anya) works for this, but it’s not an elegant automated solution.

Scott set up a smart outlet for the printer – you can tell the Echo to turn the printer outlet on and off now. But you still have to remember to turn it off 🙂

So I set up a print queue on the server & all print jobs are submitted to the server-based queue. A scheduled task on the server checks the print queue for jobs and turns the printer on when jobs are found. When the printer is on but no jobs are in the queue, it waits ten minutes and checks again (otherwise you could turn the printer on & have the batch immediately turn it off. Or worse the job could be out of the queue but still printing!), then turns the printer off if there are still no jobs in the queue. Voila, now the printer turns itself on when you want to print something and it remembers to turn itself off later.

The tricky bit was figuring out how to post ‘ON’ and ‘OFF’ to the OpenHAB2 REST API. -Body with just the command:

Invoke-WebRequest -URI ‘http://openhabserver.domain.gTLD:8080/rest/items/Outlet1’ -ContentType “text/plain” -Method POST -Body ‘OFF’

 

# Get number of jobs in print queue
$iItemsInQueue = get-printjob -PrinterName "WF-7520 Series(Network)" | measure

# Get current state of outlet
try{
$strOutletState = Invoke-WebRequest -URI 'http://openhabserver.domain.gTLD:8080/rest/items/Outlet1/state' -ContentType "text/plain" -Method GET
}
catch{
Write-Host "Status Code:" $_.Exception.Response.StatusCode.value__
Write-Host "Status Description:" $_.Exception.Response.StatusDescription
Write-Host "Status Message:" $_.Exception.Message
Write-Host "Status Response:" $_.Exception.Response
Write-Host "Status Status:" $_.Exception.Status
}

if($iItemsInQueue.Count -eq 0 -And $strOutletState.Content-eq "ON"){ # No items are in print queue, but outlet is on. Turn it off after a ten minute pause
try{
Start-Sleep 600
$iItemsInQueue2 = get-printjob -PrinterName "WF-7520 Series(Network)" | measure # Check queue again to ensure we are not turning outlet off while someone is submitting print job
if($iItemsInQueue2 -eq 0){
Invoke-WebRequest -URI 'http://openhabserver.domain.gTLD:8080/rest/items/Outlet1' -ContentType "text/plain" -Method POST -Body 'OFF'
}
}
catch{
Write-Host "Status Code:" $_.Exception.Response.StatusCode.value__
Write-Host "Status Description:" $_.Exception.Response.StatusDescription
Write-Host "Status Message:" $_.Exception.Message
Write-Host "Status Response:" $_.Exception.Response
Write-Host "Status Status:" $_.Exception.Status
}
}
elseif ($iItemsInQueue.Count -gt 0 -And $strOutletState.Content-eq "OFF"){ # Items are in print queue and outlet is not presently on
try{
Invoke-WebRequest -URI 'http://openhabserver.domain.gTLD:8080/rest/items/Outlet1' -ContentType "text/plain" -Method POST -Body 'ON'
}
catch{
Write-Host "Status Code:" $_.Exception.Response.StatusCode.value__
Write-Host "Status Description:" $_.Exception.Response.StatusDescription
Write-Host "Status Message:" $_.Exception.Message
Write-Host "Status Response:" $_.Exception.Response
Write-Host "Status Status:" $_.Exception.Status
}
}
else{
Write-Host $iItemsInQueue.Count " items in the print queue. The outlet is currently " $strOutletState.Content
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *