FOSCAM motion/sound detection and IR Led status in HA

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

Was this ever fixed? I’m interested in doing automations in HASS based on motion form the Foscam.

I am also getting an error with the import (below). I followed the steps above that Rob did.
Any ideas how to get the import to work?

Traceback (most recent call last): 
File "./", line 8, in <module>
from foscam import FoscamCamera
File "/usr/local/lib/python3.4/dist-packages/pyfoscam-1.2-py3.4.egg/foscam/", line 1, in <module>
from foscam import FoscamCamera
ImportError: cannot import name 'FoscamCamera'

I had to update the pyfoscam python library (/usr/local/lib/python3.4/dist-packages/pyfoscam-1.2-py3.4.egg/foscam/ and this script to use the correct foscam.foscam namespace change:
from foscam import FoscamCamera
from foscam.foscam import FoscamCamera

Thank you! Works great now.

1 Like

Thanks for sharing the script.

1 Like

i change in file follow your guide but get erro.

Traceback (most recent call last):
File “./”, line 7, in
from foscam.foscam import FoscamCamera, FOSCAM_SUCCESS
ImportError: No module named foscam.foscam

just a question… script works properly but how can i use it as i cannot see any values on motion detection :frowning: i see that motion detection is 1 equal activated … but nothing more

ipcamera/Foscam {“infraLedState”: “1”, “soundAlarm”: “0”, “motionDetectAlarm”: “1”}
thats what i get… i never get payload 2. motion detection is well activated :frowning:

FIXED! I just created a mqtt binary sensor… Will see if it works in a few minutes . Til now i use appdaemon for this but motion detection is very slow there and i guess will mqtt it is on the fly

1 Like

Hello there,

I am new to running scripts with HASS. I currently have HASSIO and am looking to get a little more instruction. I have placed the file inside python_scripts folder inside of the configuration folder. Is this how I install it, or is there another way? Looking for some step by steps if possible. Thanks

I have a question as to the python script. How do you run it? Thanks in advance

So I have gotten the script placed and most working but I keep getting this error: Can anyone help?

    Traceback (most recent call last):
  File "/home/homeassistant/.homeassistant/", line 80, in <module>
  File "/home/homeassistant/.homeassistant/", line 61, in main
    client.connect(args.mqtthost, args.mqttport, 60)
  File "/home/pi/.local/lib/python3.5/site-packages/paho/mqtt/", line 760, in connect
    return self.reconnect()
  File "/home/pi/.local/lib/python3.5/site-packages/paho/mqtt/", line 887, in reconnect
    sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
  File "/usr/lib/python3.5/", line 712, in create_connection
    raise err
  File "/usr/lib/python3.5/", line 703, in create_connection
TimeoutError: [Errno 110] Connection timed out

how to add human detect sensor. Thanks

Good day, how do you continuesly run the script. I pressume it runs once reports the state and stops. How do you keep it active?
Thank you