FOSCAM motion/sound detection and IR Led status in HA


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:

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 %}'

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 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, and here you can find info about how to configure it with HA:

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 (

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

./ -N plantabaja -H -p 88 -u admin -w admin -M -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 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
$ chmod +x
$ ./ -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


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 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;

[email protected]:~/foscam2mqtt $ ./ -h Traceback (most recent call last): File "./", 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:

  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 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 script. These are the steps I’ve taken with the results;

Tried to install with user pi

[email protected]:~/foscam-python-lib $ python 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:


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

For information on other options, you may wish to consult the
documentation at:

Please make the appropriate changes for your system and try again.

Successful install with sudo

[email protected]:~/foscam-python-lib $ sudo python 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/ -> build/lib.linux-armv7l-2.7/foscam
copying foscam/ -> build/lib.linux-armv7l-2.7/foscam
creating build/lib.linux-armv7l-2.7/tests
copying tests/ -> build/lib.linux-armv7l-2.7/tests
copying tests/ -> 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/ -> build/bdist.linux-armv7l/egg/foscam
copying build/lib.linux-armv7l-2.7/foscam/ -> build/bdist.linux-armv7l/egg/foscam
creating build/bdist.linux-armv7l/egg/tests
copying build/lib.linux-armv7l-2.7/tests/ -> build/bdist.linux-armv7l/egg/tests
copying build/lib.linux-armv7l-2.7/tests/ -> build/bdist.linux-armv7l/egg/tests
byte-compiling build/bdist.linux-armv7l/egg/foscam/ to foscam.pyc
byte-compiling build/bdist.linux-armv7l/egg/foscam/ to __init__.pyc
byte-compiling build/bdist.linux-armv7l/egg/tests/ to camtest.pyc
byte-compiling build/bdist.linux-armv7l/egg/tests/ 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 script (with and without sudo)

[email protected]:~/foscam2mqtt $ ./
Traceback (most recent call last):
  File "./", line 8, in <module>
    from foscam import FoscamCamera
  File "/usr/local/lib/python2.7/dist-packages/foscam-1.0-py2.7.egg/foscam/", line 1, in <module>
    from foscam.foscam import FoscamCamera
ImportError: No module named foscam
[email protected]:~/foscam2mqtt $ sudo ./
Traceback (most recent call last):
  File "./", line 8, in <module>
    from foscam import FoscamCamera
  File "/usr/local/lib/python2.7/dist-packages/foscam-1.0-py2.7.egg/foscam/", 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;

git clone
cd foscam-pyton-lib
sudo python3 install
sudo pip3 install paho-mqtt
mkdir foscam2mqtt
cd foscam2mqtt
sudo chmod +x
nano # 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;

[email protected]:~/foscam2mqtt $ python3 -h
usage: [-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
                        mqtt camera username
                        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 ./ -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