FOSCAM motion/sound detection and IR Led status in HA

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

[email protected]:~/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

[email protected]:~/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)

[email protected]:~/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
[email protected]:~/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;

[email protected]:~/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

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 "./foscam2mqtt.py", line 8, in <module>
from foscam import FoscamCamera
File "/usr/local/lib/python3.4/dist-packages/pyfoscam-1.2-py3.4.egg/foscam/__init__.py", 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/init.py) and this script to use the correct foscam.foscam namespace change:
OLD:
from foscam import FoscamCamera
NEW:
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 “./foscam2mqtt.py”, 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 foscam2mqtt.py 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/foscam2mqtt.py", line 80, in <module>
    main()
  File "/home/homeassistant/.homeassistant/foscam2mqtt.py", line 61, in main
    client.connect(args.mqtthost, args.mqttport, 60)
  File "/home/pi/.local/lib/python3.5/site-packages/paho/mqtt/client.py", line 760, in connect
    return self.reconnect()
  File "/home/pi/.local/lib/python3.5/site-packages/paho/mqtt/client.py", line 887, in reconnect
    sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
  File "/usr/lib/python3.5/socket.py", line 712, in create_connection
    raise err
  File "/usr/lib/python3.5/socket.py", line 703, in create_connection
    sock.connect(sa)
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