Jump to content

Reliable Idle detection (or detect when the monitor has been told to turn off)

Recommended Posts

I want to detect when a PC is effectively idle so that I can turn off a wifi controlled socket (Audio Amp). Control of the socket isn't a problem.

Detecting Idle is a bit tricky, if I'm watching a video then potentially I won't move the mouse or type.

I did look into potentially reading the status of VLC, which is the only media player I use. Unfortunately this is also tricky as I may have multiple instances (max 1 playing while others paused), but in conjunction with other idle detection methods this would give me the greatest flexibility to conjure up a good control strategy for the wifi socket.
The viable option I have come up with is to detect that the monitor is off (or has been told to  turn off, I believe there may be a difference), but it doesn't seem to be easy. I can get around the monitor switching off while playing audio by using time as a factor, I wouldn't be doing that at night, for example.

If anyone has some ideas I'd appreciate it :)

Link to post
Share on other sites

you could rely on Task Scheduler for that, instead of detecting idle time yourself. this is because Task Scheduler respects not only user inactivity as a criteria for idle time, but also resource usage, and most importantly - any programs that set their execution level to any value mentioned here, which prevents Windows from entering sleep mode, activating screen saver, etc.

i expect you could call some Win32 API to query Windows if any app execution level has been set as mentioned. however i am unfamiliar with such method, hence my suggestion to use Task Scheduler.

recommended read at MSDN here.

Link to post
Share on other sites

Thanks for the ideas!


I started with task scheduler. I have run into a few problems. The first is that I can't work out how to schedule a task when I come out of idle to turn the amp back on again. Other issues revolve around time. I could potentially use task scheduler to signal a script that idle has been detected, if I can detect coming out of idle as well. 


no mouse + no KB + no sound should work, tho I haven't found a way to detect if audio is playing. Correction, there's this:


Using the screensaver activity might work, I would need to enable one for that tho :D


Don't laugh, but I've only just discovered _Timer_GetIdleTime(). That, coupled with the audio detection, might solve the problem. Happy days :)


Link to post
Share on other sites

no, that function sets the execution state. i was interested in a method to query the OS if any other program has already set its execution state in a way that requires the system to stay awake. if that was the case, one can assume the system is not idle even if _Timer_GetIdleTime() does return a value that indicates so.

Link to post
Share on other sites


And I would say just like a tv if "on" for n hours just a messagebox 1 minute before tv is turned off press ok to stay awake.

You first have to make your definition of "idle". When I am idle on my system and check what taskmanager is doing with CPU, Memory, ..... I am allways surprised that its 3-5% CPU using because there are still browsers open, virusscanners active, dozens of windows OS processes etc.

Link to post
Share on other sites

If you are using windows, there is a trigger in event viewer task scheduler that might help. Open event viewer and create a basic task.

Begin the Task: On an event
Setting Basic
Log:  System
Source: Kernel-Power
Event ID: 42
The system is entering sleep.

There is also a trigger for when the computer returns from sleep mode, so you can switch the socket back on.

Begin the Task: On an event
Setting Basic
Log:  System
Source: Power-Troubleshooter
Event ID: 1
The system has resumed from sleep.

Source: https://superuser.com/questions/321151/how-to-execute-a-script-on-sleep-hibernate-resume-and-shutdown

Hope this helps 😃


Edit: It is task scheduler, not event viewer

Edited by Leendert-Jan
Link to post
Share on other sites

Someone more familiar with DLL calls/callbacks should look here: https://docs.microsoft.com/en-us/windows/win32/api/powerbase/nf-powerbase-powerregistersuspendresumenotification I think this might notify you when your computer is being suspended/resumed

Edit: Or maybe periodically querying this: https://docs.microsoft.com/en-us/windows/win32/api/powerbase/nf-powerbase-callntpowerinformation

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.


My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...