FOSCAM motion/sound detection and IR Led status in HA

Hi

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.

So I changed the method from command_line sensors to MQTT sensors. For get this I wrote this simple script for connect to Foscam Camera and publish the result in the broker: https://github.com/gonzalezcalleja/foscam2mqtt/blob/master/foscam2mqtt.py

And this is an example of my HA config:

   - platform: mqtt
     state_topic: "ipcamera/plantabaja"
     name: Cam PlantaBaja Motion
     qos: 0
     value_template: '{% if value_json.motionDetectAlarm == "0" %} disarm {% elif value_json.motionDetectAlarm == "1" %} off {% else %} on {% endif %}'

  - platform: mqtt
    state_topic: "ipcamera/plantabaja"
    name: Cam PlantaBaja Sound
    qos: 0
    value_template: '{% if value_json.soundAlarm == "0" %} disarm {% elif value_json.soundAlarm == "1" %} off {% else %} on {% endif %}'

  - platform: mqtt
    state_topic: "ipcamera/plantabaja"
    name: Cam PlantaBaja IR
    qos: 0
    value_template: '{% if value_json.infraLedState == "0" %} off {% else %} on {% endif %}'
5 Likes

Hello @jgon,
Sorry maybe it’s obvious but I am a newbee.

I have two short questions.

  1. What exactly is the script on the GitHub page and where do I have to put it.

  2. Will it work with other Cameras too? The Foscam C1 Lite for example.

Thanks a lot for sharing this project! I am really happy that I’ve found it!

Thanks in advance

Hi @Smarties,

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.

The HA config example is an example using HA MQTT sensors (MQTT Sensor - Home Assistant).

You can put this script wherever you want and run one script per camera, for example:

./foscam2mqtt.py -N plantabaja -H 192.168.1.213 -p 88 -u admin -w admin -M 192.168.1.100 -P 1883 -U homeassistant -W

I don’t have a C1 Lite but i think that should works because almost foscam cameras uses the same API (i’m using this script with C1).

1 Like

Hello @jgon

Thanks for the quick reply,

I installed HA with the all-in-one installer so i have a MQTT broker on my raspberry automatically. So this shouldn’t be the problem.:slight_smile:

But I have one more question about the foscam2mqqt.py file. When I copy the code into Notepad++ and save it as .py file it probably will not work.

Where do I have to replace a placeholder with my real informations to get it working?

Thanks in advance

what is the error?

Try downloading the file instead of copy&paste:

$ wget https://raw.githubusercontent.com/gonzalezcalleja/foscam2mqtt/master/foscam2mqtt.py
$ chmod +x foscam2mqtt.py
$ ./foscam2mqtt.py -h

Sorry for my slow response,

There is no error. I didn’t tried your solution yet because I got the camera only a few days ago.

I only had the question if there are placeholders somewhere in the file wich have to be replaced by my personal information

Example:

parser.add_argument("-H", “–host”, help=“foscam camera host/ip”)

is this “foscam camera host/ip” a placeholder to be replaced by something like this 192.168.178.x ?

or is “foscam camera username” a placeholder to be replaced by a username like “admin” ?

Hi @Smarties,

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).

Hi @jgon,

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

Am I missing something?

Try to install foscam module for python.

How do I install the foscam module? Sorry but i’m relatively new to python.

Thank you for the python script! i like it.

For the motion part a binary sensor config is better suited i think:

binary_sensor:
  platform: mqtt
  state_topic: "ipcamera/schuur"
  name: "Camera motion"
  qos: 0
  payload_on: "2"
  payload_off: "1"
  sensor_class: motion
  value_template: '{{ value_json.motionDetectAlarm }}'

Any one know how to install install foscam module for python in HA all in one.

Can you tell how you install the python module for 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…

Clone this repo


And then run
python setup.py install

Is the .py script just run on a schedule or how are you invoking it? May have missed those details.

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.

Successful install with sudo

pi@hass:~/foscam-python-lib $ sudo python setup.py install
running install
running bdist_egg
running egg_info
creating foscam.egg-info
writing foscam.egg-info/PKG-INFO
writing top-level names to foscam.egg-info/top_level.txt
writing dependency_links to foscam.egg-info/dependency_links.txt
writing manifest file 'foscam.egg-info/SOURCES.txt'
reading manifest file 'foscam.egg-info/SOURCES.txt'
writing manifest file 'foscam.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-armv7l/egg
running install_lib
running build_py
creating build
creating build/lib.linux-armv7l-2.7
creating build/lib.linux-armv7l-2.7/foscam
copying foscam/foscam.py -> build/lib.linux-armv7l-2.7/foscam
copying foscam/__init__.py -> build/lib.linux-armv7l-2.7/foscam
creating build/lib.linux-armv7l-2.7/tests
copying tests/camtest.py -> build/lib.linux-armv7l-2.7/tests
copying tests/__init__.py -> build/lib.linux-armv7l-2.7/tests
creating build/bdist.linux-armv7l
creating build/bdist.linux-armv7l/egg
creating build/bdist.linux-armv7l/egg/foscam
copying build/lib.linux-armv7l-2.7/foscam/foscam.py -> build/bdist.linux-armv7l/egg/foscam
copying build/lib.linux-armv7l-2.7/foscam/__init__.py -> build/bdist.linux-armv7l/egg/foscam
creating build/bdist.linux-armv7l/egg/tests
copying build/lib.linux-armv7l-2.7/tests/camtest.py -> build/bdist.linux-armv7l/egg/tests
copying build/lib.linux-armv7l-2.7/tests/__init__.py -> build/bdist.linux-armv7l/egg/tests
byte-compiling build/bdist.linux-armv7l/egg/foscam/foscam.py to foscam.pyc
byte-compiling build/bdist.linux-armv7l/egg/foscam/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-armv7l/egg/tests/camtest.py to camtest.pyc
byte-compiling build/bdist.linux-armv7l/egg/tests/__init__.py to __init__.pyc
creating build/bdist.linux-armv7l/egg/EGG-INFO
copying foscam.egg-info/PKG-INFO -> build/bdist.linux-armv7l/egg/EGG-INFO
copying foscam.egg-info/SOURCES.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
copying foscam.egg-info/dependency_links.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
copying foscam.egg-info/top_level.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
tests.camtest: module references __file__
creating dist
creating 'dist/foscam-1.0-py2.7.egg' and adding 'build/bdist.linux-armv7l/egg' to it
removing 'build/bdist.linux-armv7l/egg' (and everything under it)
Processing foscam-1.0-py2.7.egg
creating /usr/local/lib/python2.7/dist-packages/foscam-1.0-py2.7.egg
Extracting foscam-1.0-py2.7.egg to /usr/local/lib/python2.7/dist-packages
Adding foscam 1.0 to easy-install.pth file

Installed /usr/local/lib/python2.7/dist-packages/foscam-1.0-py2.7.egg
Processing dependencies for foscam==1.0
Finished processing dependencies for foscam==1.0

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?

1 Like

i’m not sure, as i’m not home at the moment, but try to install the module with python3 and run the script with python3 too.

Okay, I’ve got it working. Here’s what I did;

  1. Install the foscam-python-lib with python3 and sudo;
  2. Install the paho-mqtt client with pyton3;
  3. 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
2 Likes

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