Coder Social home page Coder Social logo

hass-pc-usage-detection's Introduction

Home Assistant PC usage detection

What is this?

This is a super simple script that monitors if you are using your PC (either touching keyboard or mouse), and reports this to Home Assistant

It uses HTTP binary sensor

How to use it?

  1. Download it: git clone https://github.com/TheLastGimbus/hass-pc-usage-detection/ - or just press "download zip" button above
  2. Get your Home Assistant long-lived access token
  3. (Optional, but very recomended) Set up SSH tunnel to your Home Assistant
  4. pip3 install -r requirements.txt
  5. python3 main.py --url "http://<home_assistant_ip>:<port>/api/states/binary_sensor.<binary_sensor_name>" --token <HASS_LONG_LIVE_TOKEN> --keyboard --mouse

You need to specify --keyboard and --mouse flags to detect keeb/mouse activity

Default time of no-activity after PC is unused is 3 minutes. You can specify this with --time <seconds>

Example setup:

If you are actually going to rely on this, I recommend you few things from my setup:

binary_sensor:
 - platform: ping
   name: Ping PC
   host: 192.168.1.101  # You need to set yourself static IP address
 - platform: template
   sensors:
     pc_active:
       friendly_name: "PC active"
       value_template: >-
         {{ is_state('binary_sensor.pc_is_touched_script', 'on')
            and is_state('binary_sensor.ping_pc', 'on') }}

Now, you can safely use binary_sensor.pc_active in your automations!

  • make startup script and add it to your autostart: This is what I use on Ubuntu - replace [email protected] with your server user@address:
#!/bin/bash

# I run SSH tunnel and script separatly in screen, so I can manage and restart them whenever I want

# Set up SSH tunnel
# This loops it, so it always tries to re-connect after 10 seconds if something fails
screen -S "hass-ssh-tunnel" -dm bash -c '\
  while true; do
  ssh -L 9876:192.168.1.242:8123 -N -T [email protected] ;
  sleep 10 ;
  echo "Disconnected, trying to re-connect..." ;
  done;
'
screen -S "hass-python-presence-detector" -dm bash -c '\
  MY_SCRIPT_PATH="/PATH/TO/SCRIPT/FOLDER" ; \
  # This is to activate Python virtual enviroment, very recommended, but you can skip it \
  source "$MY_SCRIPT_PATH/venv/bin/activate" ; \
  python3 "$MY_SCRIPT_PATH/main.py" \
      --url "http://localhost:9876/api/states/binary_sensor.pc_is_touched_script" \
      --time 60 \
      --token "HOME_ASSISTANT_LONG_LIVED_TOKEN" \
      --keyboard \
      --mouse \
'

If you don't know what screen and venv is, here is simplified version:

#!/bin/bash
# Set up SSH tunnel
# The '&' makes it run in the background
ssh -L 9876:192.168.1.242:8123 -N -T [email protected] &
MY_SCRIPT_PATH="/PATH/TO/SCRIPT/FOLDER"
python3 "$MY_SCRIPT_PATH/main.py" \
    --url "http://localhost:9876/api/states/binary_sensor.pc_is_touched_script" \
    --time 60 \
    --token "HOME_ASSISTANT_LONG_LIVED_TOKEN" \
    --keyboard \
    --mouse

Of course, this will look a little different on Windows ๐Ÿ™…

Now, place this in your autostart and you should be good to go

SSH tunnel?

If you are running Home Assistant on your local network, and connecting to your local address (something like 192.168.x.x) with standard HTTP (without S) - anyone sniffing your WiFi could get your access token - that gives almost full access to all your home!

To protect against it, you can set up SSH tunnel that encrypts all traffic and ensures that you are connected to your server, and not the potential attacker

  1. Set up SSH on your server and SSH keys on your computer (it's easy, Google how to do that if you don't have it already)
  2. Type into terminal/cmd: ssh -L 9876:<home_assistant_ip>:8123 -N -T <user>@<home_assistant_ip>

9876 (you can change this) is port that will open on your client (your pc), and 8123 (default HASS port) is port that will be directed to on your server

You can open the browser and type in localhost:9876 to see for yourself!

hass-pc-usage-detection's People

Contributors

thelastgimbus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hass-pc-usage-detection's Issues

Max retries exceeded

Hi,

I'm trying to use this script on my Manjaro desktop, my HA is behind a ngnix and uses HTTPS, but I've tried connecting with both the domain name and the local URL. But I still get this:
I can ping the local URL and the domain name just fine from my machine.

Reporting off
Can't connect: HTTPSConnectionPool(host='<COMPUTERNAME>', port=8123): Max retries exceeded with url: /api/states/binary_sensor.pc_active_check (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f06d812f160>: Failed to establish a new connection: [Errno 113] No route to host'))
Reporting on

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.