Announcement

Collapse
No announcement yet.

Monitor a windows print spooler queue to start/stop a printer?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Monitor a windows print spooler queue to start/stop a printer?

    I've got an HP LaserJet 8100N printer and a Windows 10 Pro machine running a shared queue to it. This happens to be the same PC running my HS3 install.

    The printer is loud and consumes a fair amount of power. So leaving it up and running isn't ideal. I keep it around because a) it just won't die and b) it supports printing on 11x17 paper.

    I've got the 8100's power controlled via a Lutron appliance module. I've got the Ra2 module for HS3Pro.

    I've got it set up such that I can use "Alexa, turn on THE BEAST" to power up the printer. Which is kinda fun.

    But it occurs to me this ought to be something that could be automated via HS3.

    Anyone know what sort of scripting would be involved? I've only done a cursory search online for scripting of the print API.

    I'm wondering if there's a hook somewhere that could be triggered when a job is detected? That would probably be ideal. Just let the print spooler service handle issuing a command to HS3 to get the printer powered up. From that point I'd imagine HS3 would then have to monitor the queue "somehow" to determine when the queue has been idle "for a while" and power off the printer.

    Anyone wandered down this road using Homeseer?

    #2
    Not an expert here but you might be able to write a VBScript to monitor the print queue. You could schedule HomeSeer to run the script and if it sees jobs it could turn on the module.

    Comment


      #3
      Sure, constant querying of the spooler is one angle. But past experience tells me setting up something that's banging on a queue-type resource often causes other unintended side-effects. Least of which being wasted CPU cycles to check, checking, checking, endlessly. As opposed to having a trigger on a queue that only gets activated based on intended actions.

      Still, this does touch on what to do when the queue is empty. A queue resource isn't going to ever been "done", more than it'd be idle for a "long enough" period of time (or past a given timeframe) to make it a reasonable assumption that it's done.

      I'm looking at some windows forums to see if there's anything that'd help. I have to figure there's got to be some kinds of tools out there to monitor spooler queues. But I'd imagine most of them are centered more on monitoring for error conditions and/or print cost calculating.

      Windows does have an API for the Printer Spooler and there look to be some interesting ways to register for notifications with it.

      https://docs.microsoft.com/en-us/win...nt-spooler-api

      Comment


        #4
        You may be able to use my SNMP monitor if you can find the correct OID: http://www.oidview.com/mibs/11/HP-LA...OMMON-MIB.html
        Jon

        Comment


          #5
          I asked elsewhere and discovered the Print Management console in Windows has the ability to set up a Custom Filter. Which can run a script.

          What's the simplest way to tell HS3 to run something from a command line script? Assuming it's either powershell or plan old BAT file. This would be sent on the same machine that's running Homeseer.

          Comment


            #6
            Originally posted by wkearney99 View Post
            I asked elsewhere and discovered the Print Management console in Windows has the ability to set up a Custom Filter. Which can run a script.

            What's the simplest way to tell HS3 to run something from a command line script? Assuming it's either powershell or plan old BAT file. This would be sent on the same machine that's running Homeseer.
            An event can "Run a Script or Script command" These are usually VBScript but I guess they can be a plain old BAT file. Can you provide an example of what exactly you would like to run?

            Comment


              #7
              Originally posted by wkearney99 View Post
              What's the simplest way to tell HS3 to run something from a command line script? Assuming it's either powershell or plan old BAT file. This would be sent on the same machine that's running Homeseer.
              If you are wanting the spooler's filter to tell HS to run an event then using cURL to execute a homeseer json command that runs an HS event would do the job.

              see https://homeseer.com/support/homesee..._with_json.htm

              Refer to *request=runevent* section.
              Len


              HomeSeer Version: HS3 Pro Edition 3.0.0.435
              Linux version: Linux homeseer Ubuntu 16.04 x86_64
              Number of Devices: 633
              Number of Events: 773

              Enabled Plug-Ins
              2.0.54.0: BLBackup
              2.0.40.0: BLLAN
              3.0.0.48: EasyTrigger
              30.0.0.36: RFXCOM
              3.0.6.2: SDJ-Health
              3.0.0.87: weatherXML
              3.0.1.190: Z-Wave

              Comment


                #8
                Originally posted by wkearney99 View Post
                What's the simplest way to tell HS3 to run something from a command line script? Assuming it's either powershell or plan old BAT file.
                I think the event action you want is 'Run Another Program or Process'.

                Comment


                  #9
                  I'd definitely want to tell it to run something that could follow up. The simplest option being a delay timer to wait a predetermined period of time and then shut off the power module. But something a bit smarter might eventually be a better idea. To actually check that the queue was in fact empty, to avoid turning off the printer mid-job. That and if I'm reading the way the filtering is supposed to work any new incoming jobs could be used to reset the delay process.

                  Seems to me this is something akin to how you'd process repeated signals from a motion detector. No need to fire off the 'turn on' event again, as the printer's already on, just reset the turn off delay.

                  Comment


                    #10
                    Originally posted by wkearney99 View Post
                    I asked elsewhere and discovered the Print Management console in Windows has the ability to set up a Custom Filter. Which can run a script.
                    Interesting. I have been using Jon00 File Watcher plugin (C:\Windows\System32\spool\PRINTERS\\*.SHD) for a while to turn on a printer located 2 floors up. Lately it did not always work. This could be a great alternative and I could stop using that PI. Also, if you're using more than 1 printer you should also be able to filter which printer to turn on (Would't want printing to PDF to turn on the printer).

                    I have been playing with the Custom filters for the past hour, but I could not get it to work. I created a virtual device JobsInQueue which I turn on or off via JSON (http://homeseerserver/JSON?request=c...number&value=0 or value=100). I made PowerShell scripts which turned on/off the device. They worked, but nothing happened when I used the Test button. I guess it only takes BAT or VBS. So I made VBS, which works with the test button.

                    That's how far I got. I'm stuck now. I've tried several filters, but they haven't triggered the scripts yet.
                    Plugins I developed for HS4: Somfy Local, MiLight (LimitlessLED), Updates, Volvo (VoC), OpenTherm Gateway (OTGW)
                    Running HS4 Pro on Windows 10 Pro on a Synology VM, with Node-RED running as a container.

                    Comment


                      #11
                      on my pc I created a BAT file with a single line executing cURL to turn a virtual device (VD) on.

                      Code:
                      curl "http://homeseer-pc:80/JSON?request=controldevicebyvalue&ref=159&value=100"
                      I configured the default custom print filter "printers with jobs" to execute as follows:
                      Path: C:\Windows\System32\cmd.exe
                      Additional Arguments: /K c:\batch\test.bat

                      Printed to a pdf. A dos prompt appeared, with homeseer json output and the device in homeseer was indeed turned on.

                      /K keeps the dos prompt open after completion. /C will close it.

                      I then created a new filter where it checked for the number of job in queue is exactly 0 and executed a second BAT file turning the VD off (value=0).

                      My test print, turned the VD on, once the print was done it turned off the VD.
                      Len


                      HomeSeer Version: HS3 Pro Edition 3.0.0.435
                      Linux version: Linux homeseer Ubuntu 16.04 x86_64
                      Number of Devices: 633
                      Number of Events: 773

                      Enabled Plug-Ins
                      2.0.54.0: BLBackup
                      2.0.40.0: BLLAN
                      3.0.0.48: EasyTrigger
                      30.0.0.36: RFXCOM
                      3.0.6.2: SDJ-Health
                      3.0.0.87: weatherXML
                      3.0.1.190: Z-Wave

                      Comment


                        #12
                        I am using this for years now and it works just fine.

                        Put a remote switch between the printer and the powerline so you can swith the printer on and off with HomeSeer

                        Make a recurring event to run every 10 minutes or so.
                        Call this script

                        Code:
                        Imports System.IO
                        
                        Public Sub Main(ByVal Parms As String)
                        
                          Dim target As String = "C:\WINDOWS\system32\spool\PRINTERS"
                        
                          Dim fld As System.IO.DirectoryInfo
                          fld = New DirectoryInfo(target)
                        
                          'hs.writelog("Number of files", fld.GetFiles("*.*").Length)
                        
                          If(fld.GetFiles("*.*").Length > 0) Then hs.TriggerEvent ("Print document")
                        
                        End Sub
                        Make a second event called 'Print document' and let it turn on the remote switch for 10 minutes.
                        - Bram

                        Send from my Commodore VIC-20

                        Ashai_Rey____________________________________________________________ ________________
                        HS3 Pro 3.0.0.534
                        PIugins: ZMC audio | ZMC VR | ZMC IR | ZMC NDS | RFXcom | AZ scripts | Jon00 Scripts | BLBackup | FritzBox | Z-Wave | mcsMQTT | AK Ikea

                        Comment


                          #13
                          Originally posted by lveatch View Post
                          on my pc I created a BAT file with a single line executing cURL to turn a virtual device (VD) on.

                          Code:
                          curl "http://homeseer-pc:80/JSON?request=controldevicebyvalue&ref=159&value=100"
                          I configured the default custom print filter "printers with jobs" to execute as follows:
                          Path: C:\Windows\System32\cmd.exe
                          Additional Arguments: /K c:\batch\test.bat

                          Printed to a pdf. A dos prompt appeared, with homeseer json output and the device in homeseer was indeed turned on.

                          /K keeps the dos prompt open after completion. /C will close it.

                          I then created a new filter where it checked for the number of job in queue is exactly 0 and executed a second BAT file turning the VD off (value=0).

                          My test print, turned the VD on, once the print was done it turned off the VD.
                          This helped me figure it out. Thanks. So, for some reason, after creating the filter I have to first double click the filter before it works(!). I love how it works now. Costing close to 0 resources (no plugin or recurring script).

                          Here's my complete configuration:
                          - Obviously first set up a shared printer on the same pc.

                          - I set up a virtual device

                          - Made 2 batch files, one to turn the VD on, another for off

                          On
                          Code:
                          curl "http://homeseer-pc/JSON?request=controldevicebyvalue&ref=1234&value=100"
                          Off
                          Code:
                          curl "http://homeseer-pc/JSON?request=controldevicebyvalue&ref=1234&value=0"
                          - Instead of printmanagement.msc I used Microsoft Management Console (mmc) with the Print Management Snap-in. The reason for this is that I want it to work without being logged in. (I'm running Windows 10 Pro, on Server versions it will probably work when deployed as a role).

                          - Made two custom filters:
                          1. The first to turn the VD on: Jobs In Queue is greater than or equal to 1 and Printer Name is exactly NameOfPrinter, notification Path set to C:\Program Files\HomeSeer HS3\scripts\Printer1QueueJob.bat
                          2. The second to turn the VD off: Jobs in Queue is exactly 0 and Printer Name is exactly NameOfPrinter, notification Path set to C:\Program Files\HomeSeer HS3\scripts\Printer1QueueNoJob.bat

                          - Double clicked both custom filters to activate(!)

                          - Saved the console, added mmc "C:\Program Files\HomeSeer HS3\scripts\Console with custom printer filters.msc" to my scheduled task at startup.

                          - Made an event turn on power to the printer when the VD is turned on. I don't have one to turn it off yet - for now I do it manually when I collect the paper. It could be done by setting up an event that turns it off a few minutes after the queue is empty (if the queue is empty the job may still be in printer memory), but I would love to do it with Jon00's SNMP Monitor, which is already running. Only to find the correct OID...

                          --
                          Edit 2024: Note after modifications for my newest HomeSeer system.
                          The batch files are redundant. It can just be done by defining the actions as c:\windows\system32\curl.exe with arguments "http://homeseer-pc/JSON?request=controldevicebyvalue&ref=1234&value=100" or "http://homeseer-pc/JSON?request=controldevicebyvalue&ref=1234&value=0".
                          Plugins I developed for HS4: Somfy Local, MiLight (LimitlessLED), Updates, Volvo (VoC), OpenTherm Gateway (OTGW)
                          Running HS4 Pro on Windows 10 Pro on a Synology VM, with Node-RED running as a container.

                          Comment


                            #14
                            This thread speaks to the power of these forums. In about a week and a half of a member posting a problem a VERY good solution was formulated and implemented.

                            I have never investigate using the JSON control of HomeSeer so I learned something in the process.

                            Comment


                              #15
                              What's the right way to structure events to handle turning this off, but only if it's already been turned on for at least X minutes? I want to avoid power cycling the printer too quickly.

                              That and print jobs here at the house tend to be within 5-6 minutes of each other. So cycling the printer on and then off after one page, but then back on again just a minute or two later is probably not ideal. Perhaps using an EasyTrigger window of time or something. As in, during expected home office work intervals let it remain running for a few minutes more (or something like that).

                              Comment

                              Working...
                              X