I would like to share my configuration for use the motion/sound and IR LED in FOSCAM cameras as sensors (in this setup i’m using a few FOSCAM C1 cameras).
The first method that I used was a command_line sensor that exec a script that connect to the Foscam Camera and get the DevStatus JSON.
This method was a bit slow because the command_line sensor component has at least 60s of wait between each call, too much time for my setup.
The foscam2mqtt.py script is a mini-script that constantly queries the HTTP FOSCAM API and checks if there is any motion or sound alert. If it was, then publishes the foscam camera states (motion alert, sound alert and ir status) in a topic inside a MQTT broker, so you also will need to setup a MQTT broker (i’m using mosquitto https://mosquitto.org), and here you can find info about how to configure it with HA: MQTT - Home Assistant
The reason i’m using constantly queries to the foscam cam is because I use the embedded motion sensor in C1 for turn on the lights and trigger the alarm, and foscam doesn’t provide a method for notify this kind of events to other systems.
you don’t need to edit the foscam2mqtt.py script, you can pass the camera config using the script arguments (you can see an example on my first reply).
I tried to use your script but I’m getting the following error;
pi@hass:~/foscam2mqtt $ ./foscam2mqtt.py -h
Traceback (most recent call last):
File "./foscam2mqtt.py", line 8, in <module>
from foscam import FoscamCamera
ImportError: No module named foscam
I’m also using HASS installed by the All in One installer. I’ve tried to run the script as user pi and also as the hass user (with and without the python virtual environment sourced) but with no success…
Hi @koen01, thanks for your reply. I’ve cloned and installed the library but still not able to run the foscam2mqtt.py script. These are the steps I’ve taken with the results;
Tried to install with user pi
pi@hass:~/foscam-python-lib $ python setup.py install
running install
error: can't create or remove files in install directory
The following error occurred while trying to add or remove files in the
installation directory:
[Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/test-easy-install-13161.write-test'
The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
/usr/local/lib/python2.7/dist-packages/
Perhaps your account does not have write access to this directory? If the
installation directory is a system-owned directory, you may need to sign in
as the administrator or "root" account. If you do not have administrative
access to this machine, you may wish to choose a different installation
directory, preferably one that is listed in your PYTHONPATH environment
variable.
For information on other options, you may wish to consult the
documentation at:
https://pythonhosted.org/setuptools/easy_install.html
Please make the appropriate changes for your system and try again.
Now running the foscam2mqtt.py script (with and without sudo)
pi@hass:~/foscam2mqtt $ ./foscam2mqtt.py
Traceback (most recent call last):
File "./foscam2mqtt.py", line 8, in <module>
from foscam import FoscamCamera
File "/usr/local/lib/python2.7/dist-packages/foscam-1.0-py2.7.egg/foscam/__init__.py", line 1, in <module>
from foscam.foscam import FoscamCamera
ImportError: No module named foscam
pi@hass:~/foscam2mqtt $ sudo ./foscam2mqtt.py
Traceback (most recent call last):
File "./foscam2mqtt.py", line 8, in <module>
from foscam import FoscamCamera
File "/usr/local/lib/python2.7/dist-packages/foscam-1.0-py2.7.egg/foscam/__init__.py", line 1, in <module>
from foscam.foscam import FoscamCamera
ImportError: No module named foscam
So still not working. Am I still missing something or am I using it wrong?
Install the foscam-python-lib with python3 and sudo;
Install the paho-mqtt client with pyton3;
Edit the script made by @jgon at line 38 (change print ‘Failed to get DEVInfo!’ to print (‘Failed to get DEVInfo!’)
Full installation steps;
cd
git clone https://github.com/quatanium/foscam-python-lib.git
cd foscam-pyton-lib
sudo python3 setup.py install
sudo pip3 install paho-mqtt
cd
mkdir foscam2mqtt
cd foscam2mqtt
wget https://raw.githubusercontent.com/gonzalezcalleja/foscam2mqtt/master/foscam2mqtt.py
sudo chmod +x foscam2mqtt.py
nano foscam2mqtt.py # Edit line 38, change print 'Failed to get DEVInfo!' to print ('Failed to get DEVInfo!')
Now I’m able to run the script with python3 under user pi;
pi@hass:~/foscam2mqtt $ python3 foscam2mqtt.py -h
usage: foscam2mqtt.py [-h] [-N NAME] [-H HOST] [-p PORT] [-u USERNAME]
[-w PASSWORD] [-M MQTTHOST] [-P MQTTPORT]
[-U MQTTUSERNAME] [-W MQTTPASSWORD] [-v]
optional arguments:
-h, --help show this help message and exit
-N NAME, --name NAME foscam camera name
-H HOST, --host HOST foscam camera host/ip
-p PORT, --port PORT foscam camera port
-u USERNAME, --username USERNAME
foscam camera username
-w PASSWORD, --password PASSWORD
foscam camera password
-M MQTTHOST, --mqtthost MQTTHOST
mqtt host
-P MQTTPORT, --mqttport MQTTPORT
mqtt port
-U MQTTUSERNAME, --mqttusername MQTTUSERNAME
mqtt camera username
-W MQTTPASSWORD, --mqttpassword MQTTPASSWORD
mqtt camera password
-v, --verbose increase output verbosity
Thanks atlast one step working but now i am not able to get camera details. Getting error - Failed to get DEVInfo! when i give command - python3 ./foscam2mqtt.py -N fcamout -H 192.168.0.xx -p 8091 -u admin -w camerapwd -M 192.168.0.mqttserver -P 1883 -U username -W mqttpwd to test if the script working