Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SENSOR] Webcam in use #71

Open
Jay2645 opened this issue Dec 29, 2023 · 4 comments
Open

[SENSOR] Webcam in use #71

Jay2645 opened this issue Dec 29, 2023 · 4 comments

Comments

@Jay2645
Copy link

Jay2645 commented Dec 29, 2023

This should be easy enough to do, I think - you should be able to look at fuser /dev/video0. If it reports something, then the webcam is on - otherwise it's off. (You might be able to get fancy and check video1 as well.)

Microphone might be handy, too, but I don't think you can get there from /dev/ - Google says you need to look at find /proc/asound -name status -exec grep -v closed {} + and see if it reports an output or not.

Even if it isn't natively built-in, it might be handy as an example script for people who want to write their own scripts?

@Jay2645
Copy link
Author

Jay2645 commented Dec 29, 2023

For completeness, here's the script I run:

#!/bin/bash

# Check if /dev/video0 is being used by any process
output=$(fuser /dev/video0)

# Initialize variables
sensor_state="false"

# Check the length of the output string
if [ ${#output} -gt 0 ]; then
  sensor_state="true"
fi

# Create the JSON output
json_output='{
  "schedule": "@every 5s",
  "sensors": [
    {
      "sensor_name": "Webcam",
      "sensor_type": "binary",
      "sensor_device_class": "power",
      "sensor_icon": "mdi:webcam",
      "sensor_state": '"$sensor_state"'
    }
  ]
}'

# Output the JSON
echo "$json_output"

Output:

{
  "schedule": "@every 5s",
  "sensors": [
    {
      "sensor_name": "Webcam",
      "sensor_type": "binary",
      "sensor_device_class": "power",
      "sensor_icon": "mdi:webcam",
      "sensor_state": false
    }
  ]
}

Note that it's important that "false" be the raw value and not put in quotes. Ask me how I know!

@joshuar
Copy link
Owner

joshuar commented Jan 1, 2024

Hey @Jay2645 I like this idea, I can easily see knowing when the microphone or webcam is in use as being useful to automate some action with Home Assistant.

I will have to think about whether it is possible to get this info without polling directly. I'll see if I can find it somewhere on D-Bus...

I also like the idea of collecting example scripts. I'm wondering where would be a good place to put them? In the go-hass-agent repo? In another separate repo?

I can also see a docs improvement I should make around binary sensors and their values 😉 I will add that in the next release.

@Jay2645
Copy link
Author

Jay2645 commented Jan 23, 2024

Sorry - for some reason Github has stopped sending me emails!

Somewhere like docs/examples would be good, in my opinion. People will be going into the docs anyway to learn how to make scripts, and having examples right next to them makes sense to me. If it were to be in another repo, I would expect the docs to also be in that repo (like the main Home Assistant project). But I think for the short-medium term, having it in here makes perfect sense. They're just text files, so they're small.

@joshuar joshuar changed the title Detect if webcam is in use by a process [SENSOR] Webcam in use Apr 10, 2024
@fhp
Copy link

fhp commented May 9, 2024

Thanks for the script, it works very well :)

I updated this line to detect if any camera is active:

From:

output=$(fuser /dev/video0)

To:

output=$(fuser /dev/video*)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants