PC Idle script

Hi guys!

I was trying to improve my room presence detection and thought that a 100% bulletproof way to tell that I’m in my room is to see whether I’m using my PC or not. So quickly mocked this script up to check if my PC is idle or not, and decided to share it :slight_smile:

It’s a python script that will run continuously on your PC and check for time since last activity (mouse movements or keyboard usage), and based on the time since last activity it will turn an input_boolean on or off.

It currently only supports Windows PCs, but Linux support may come in the future. Or if someone is willing to contribute to this super small project - feel free to :slight_smile:

Here’s the link with instructions on how to install it: https://github.com/Nixellion/HASS-PC-Idle

I hope someone might find it helpful :slight_smile:

Thanks, have been thinking about doing the same and will give this a go later today. Meanwhile I use my PC as a media centre of sorts, mainly watching BBC iplayer, amazon prime and Netflix via the browser. This means there sometimes is no mouse/keyboard activity for the length of a movie but the pc shouldn’t be marked as away. I’ve not been able to find how to detect whether media is playing in a browser but my thought was to check network traffic and react to that. Would a python script be able to check network traffic?

I’m working on something similar in order to detect if I am watching youtube or something else via the browser.

I looked into the libraries “win32api” and “win32gui” and found a method for getting the name of the current window.

from win32gui import GetWindowText, GetForegroundWindow

The next step is to publish this to mqtt and create a template sensor in order to stop the video playback (e.g. when my phone rings).

Well, it’s kind of limited unfortunately, because again you need to hardcode all possible window titles like youtube, plex, vimeo and whatever other streaming services there are. And what if you watch a movie on your second screen while eating something, and the last place you clicked was not the video player? Like you typed something on main screen in discord or whatever, and got back to watching and eating. It will consider you idle =\

I have found iotlink (https://iotlink.gitlab.io/) which does a decent job of detecting if media is playing and also let’s you pause it via mqtt. Works well with all browser based media except for iplayer which doesnt recognise the windows play/pause command.
Apart from that it also has an idle time sensor but I have not tested this.

1 Like