Foscam camera : status & trigger motion detection

Hello,

You can easily change the status of motion detection of the camera with this

  - service: camera.enable_motion_detection or camera.disable_motion_detection
    target:
      entity_id:
        - camera.fi9826p_living

But is there a way to get the status ?

And is there a way to use the detection of the camera as trigger of a script ?

Since the foscam integration doesn’t create those sensors (as far as I know…) the only way I know to do it is to use the CGI commands for the various cameras to get the data.

here is what I use to get the motion detection status of one of my Foscam cameras:

sensor:
  - platform: command_line  
    command: curl -k --silent "http://192.168.1.58:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=<my_cam_user>&pwd=<my_cam_password>"
    name: "Kitchen Camera Motion Cam"
    value_template: >
      {% set status = value | regex_findall_index('Alarm>(\d+)</motion') %}
      {% set values = {'0':'Disabled', '1':'None', '2':'Detected'} %}
      {{ values[status] if status in values.keys() else 'Not Determined'}}
    scan_interval: 5

you can also get the motion detection status using the above for a switch you can use in the frontend to turn on or off the motion detection I use this for another camera that had a bad habit of turning it’s motion detection off periodically:

switch:
  - platform: command_line
    switches:
      foscam_computer_cam_motion:
        friendly_name: 'Computer Cam Motion'
        command_on: 'curl -k "http://192.168.1.53:8003/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=1&schedule0=281474976710655&schedule1=281474976710655&schedule2=281474976710655&schedule3=281474976710655&schedule4=281474976710655&schedule5=281474976710655&schedule6=281474976710655&usr=<my_cam_user>&pwd=<my_cam_password>"'
        command_off: 'curl -k "http://192.168.1.53:8003/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=0&usr=<my_cam_user>&pwd=<my_cam_password>"'
        command_state: 'curl -k --silent "http://192.168.1.53:8003/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr=<my_cam_user>&pwd=<my_cam_password>" '
        value_template: >
          {% if value | regex_findall(find='isEnable>(\d+)</isEnable') != [] %}
            {{ value | regex_findall_index('isEnable>(\d+)</isEnable') == '1'}}
          {% else %}
           {{ states('switch.foscam_computer_cam_motion') }}
          {% endif %}
2 Likes

Oh, super !

Must be doing something stupid, but what am I missing ?

First. as an FYI, if you don’t either tag me directly (by using @finity) or by clicking the reply button in my post then I don’t get notified that you have replied to me and then I have to notice that there are new posts in the thread. it’s easier for me to notice and reply faster the former way.

And it’s better to post actual code blocks instead of pictures of your code. it’s easier for us to edit your text than to rewrite it all by hand if there are changes needed.

but on to the issue…

you are trying to create a command line sensor as a template sensor. that won’t work.

you have to configure it as a simple “sensor:” with a platform of “command_line” not as a “template:” sensor.

just put the code I gave you (without the opening “sensor:” line and modified for your details) under your “sensor:” section in your configuration.yaml or wherever your sensors are if you have split your config.

Here is my configuration.yaml :

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
#camera: !include cameras.yaml
template: !include templates.yaml
input_number: !include input_number.yaml
input_select: !include input_select.yaml
#sensor: !include sensor.yaml
#binary_sensor: !include binary_sensor.yaml
rest_command: !include rest_command.yaml

sensor:
  - platform: command_line  
    command: curl -k --silent "http://192.168.1.101:2001/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=xxx&pwd=yyy"
    name: "Motion Camera Living"
    value_template: >
      {% set status = value | regex_findall_index('Alarm>(\d+)</motion') %}
      {% set values = {'0':'Disabled', '1':'None', '2':'Detected'} %}
      {{ values[status] if status in values.keys() else 'Not Determined'}}
    scan_interval: 5

This way it works, but how can I transfert it to a sensor.yaml file ?

Changing configuration.yaml : sensor: !include sensor.yaml
And in the sensor.yaml I tried this :

#sensor:
  - platform: command_line  
    command: curl -k --silent "http://192.168.1.101:2001/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=xxx&pwd=yyy"
    name: "Motion Camera Living"
    value_template: >
      {% set status = value | regex_findall_index('Alarm>(\d+)</motion') %}
      {% set values = {'0':'Disabled', '1':'None', '2':'Detected'} %}
      {{ values[status] if status in values.keys() else 'Not Determined'}}
    scan_interval: 5

But it gives me errors :

Back on topic : sensor works fine :grinning:

Schermafbeelding 2022-01-23 091343

How can I integrate my !secret camera_living_login and !secret camera_living_psw in the URL ?

There seems a problem in the state of the switches for my 2 last camera’s :

Schermafbeelding 2022-01-23 091726

Also switching on/off works for the first 2 camera’s, but when I toggle the switch of the last 2 camera’s the switch comes back to off. Maybe the type : first two are FI9826P and the last two are FI9900EP ?

On all the camera’s I removed the stuff of schedules in your code. I see on the app of foscam that my last 2 camera’s has “Full-time modus” as scheme and the first 2 camera’s have “Close”…even if I don’t want schemes…

  - platform: command_line
    switches:
      foscam_camera_voordeur_motion:
        friendly_name: 'Camera Voordeur Motion'
        command_on: 'curl -k "http://192.168.1.104:2004/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=1&usr=admin&pwd=FosVoord123."'
        command_off: 'curl -k "http://192.168.1.104:2004/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=0&usr=admin&pwd=FosVoord123."'
        command_state: 'curl -k --silent "http://192.168.1.104:2004/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr=xxx&pwd=yyy" '
        value_template: >
          {% if value | regex_findall(find='isEnable>(\d+)</isEnable') != [] %}
            {{ value | regex_findall_index('isEnable>(\d+)</isEnable') == '1'}}
          {% else %}
           {{ states('switch.foscam_camera_voordeur_motion') }}
          {% endif %}

the only way that is possible is to make the entire URL command with that info in plain text as a secret and then use that secret in the “command:” line:

command: !secret some_cammand_secret

I’m not sure why you would be getting those errors. it looks fine to me and is the way I split up my config.

maybe it’s the VSCode editor that is confused?

TBH, I’m not sure what the different CGI commands for the different cameras are.

I usually just download a CGI reference manual from Foscam for my camera of interest. Most cameras use the same set of CGI commands but yours may be different.

EDIT to add:

I entered the commands in the browser.

The first 2 camera’s give me this :

<CGI_Result>
    <result>0</result>
    <isEnable>0</isEnable>
    <linkage>136</linkage>
    <snapInterval>0</snapInterval>
    <sensitivity>0</sensitivity>
    <triggerInterval>0</triggerInterval>
    <isMovAlarmEnable>1</isMovAlarmEnable>
    <isPirAlarmEnable>0</isPirAlarmEnable>
    <schedule0>0</schedule0>
    <schedule1>0</schedule1>
    <schedule2>0</schedule2>
    <schedule3>0</schedule3>
    <schedule4>0</schedule4>
    <schedule5>0</schedule5>
    <schedule6>0</schedule6>
    <area0>0</area0>
    <area1>0</area1>
    <area2>0</area2>
    <area3>0</area3>
    <area4>0</area4>
    <area5>0</area5>
    <area6>0</area6>
    <area7>0</area7>
    <area8>0</area8>
    <area9>0</area9>
</CGI_Result>

The last 2 give me only this (same if enabled or disabled) :

<CGI_Result>
    <result>-3</result>
</CGI_Result>

Problem for Foscam helpdesk I suppose…

Transfering the to sensor.yaml is OK

Off topic : I have these binary_sensors in my template.yaml

- binary_sensor:
  - name: "airco_gang_is_on"
    icon: "mdi:fan"
    state: "{{ not is_state('climate.airco_gang', 'off') }}"
  - name: "airco_living_links_is_on"
    icon: "mdi:fan"
    state: "{{ not is_state('climate.airco_living_links', 'off') }}"

I tought to transfer these to a binary_sensor.yaml, but I get errors…

Schermafbeelding 2022-01-23 164903

Schermafbeelding 2022-01-23 164946

it’s hard to know why you are getting that error.

It seems to think it’s a regular binary_sensor that requires a “platform:” instead of a template binary_sensor.

you have something misconfigured somewhere.

Even if I put it in configuration.yaml I get an error :

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
template: !include templates.yaml
input_number: !include input_number.yaml
input_select: !include input_select.yaml
sensor: !include sensor.yaml
#binary_sensor: !include binary_sensor.yaml
rest_command: !include rest_command.yaml
switch: !include switch.yaml

binary_sensor:
  - name: "airco_gang_is_on"
    icon: "mdi:fan"
    state: "{{ not is_state('climate.airco_gang', 'off') }}"
  - name: "airco_living_links_is_on"
    icon: "mdi:fan"
    state: "{{ not is_state('climate.airco_living_links', 'off') }}"
  - name: "airco_living_rechts_is_on"
    icon: "mdi:fan"
    state: "{{ not is_state('climate.airco_living_rechts', 'off') }}"
  - name: "airco_living_is_on"
    icon: "mdi:fan"
    state: "{{ not is_state('climate.airco_living_links', 'off') or not is_state('climate.airco_living_rechts', 'off') }}"
  - name: "airco_fitness_is_on"
    icon: "mdi:fan"
    state: "{{ not is_state('climate.airco_fitness', 'off') }}"
  - name: "airco_slaapkamer_alex_is_on"
    icon: "mdi:fan"
    state: "{{ not is_state('climate.airco_slaapkamer_alex', 'off') }}"

#themes
frontend:
  themes: !include_dir_merge_named themes
...

Schermafbeelding 2022-01-23 174059

binary_sensor: is line 20

Right, that’s why I said this above:

you are trying to create a legacy template sensor in the binary_sensor section without providing a platform.

Or you are trying to create a new style template binary sensor without specifying it as such.

so this is likely correct:

template:
  - binary_sensor:
      - name: "airco_gang_is_on"
        icon: "mdi:fan"
        state: "{{ not is_state('climate.airco_gang', 'off') }}"
      - name: "airco_living_links_is_on"
        icon: "mdi:fan"
        state: "{{ not is_state('climate.airco_living_links', 'off') }}"
      - name: "airco_living_rechts_is_on"
        icon: "mdi:fan"
        state: "{{ not is_state('climate.airco_living_rechts', 'off') }}"

Ok‚ so I may leave it as it is.

Thank you for everything !

PS : Meanwhile found the problem for my 2 camera’s (I have to use getMotionDetectConfig1 instead of getMotionDetectConfig)

1 Like

Problem : when using the switch to activate the motion detection of the camera, it resets everything.

I explain : I set everything as it should be with the program of foscam :

When typing the URL of getMotionDetectConfig in my browser I get this :

<CGI_Result>
    <result>0</result>
    <isEnable>1</isEnable>
    <linkage>142</linkage>
    <snapInterval>1</snapInterval>
    <sensitivity>2</sensitivity>
    <triggerInterval>5</triggerInterval>
    <isMovAlarmEnable>1</isMovAlarmEnable>
    <isPirAlarmEnable>0</isPirAlarmEnable>
    <schedule0>281474976710655</schedule0>
    <schedule1>281474976710655</schedule1>
    <schedule2>281474976710655</schedule2>
    <schedule3>281474976710655</schedule3>
    <schedule4>281474976710655</schedule4>
    <schedule5>281474976710655</schedule5>
    <schedule6>281474976710655</schedule6>
    <area0>1023</area0>
    <area1>1023</area1>
    <area2>1023</area2>
    <area3>1023</area3>
    <area4>1023</area4>
    <area5>1023</area5>
    <area6>1023</area6>
    <area7>1023</area7>
    <area8>1023</area8>
    <area9>1023</area9>
</CGI_Result>

Now I switch off and on the motion detection of the camera with your switch and go back to my browser…

<CGI_Result>
    <result>0</result>
    <isEnable>1</isEnable>
    <linkage>0</linkage>
    <snapInterval>0</snapInterval>
    <sensitivity>0</sensitivity>
    <triggerInterval>0</triggerInterval>
    <isMovAlarmEnable>1</isMovAlarmEnable>
    <isPirAlarmEnable>0</isPirAlarmEnable>
    <schedule0>0</schedule0>
    <schedule1>0</schedule1>
    <schedule2>0</schedule2>
    <schedule3>0</schedule3>
    <schedule4>0</schedule4>
    <schedule5>0</schedule5>
    <schedule6>0</schedule6>
    <area0>0</area0>
    <area1>0</area1>
    <area2>0</area2>
    <area3>0</area3>
    <area4>0</area4>
    <area5>0</area5>
    <area6>0</area6>
    <area7>0</area7>
    <area8>0</area8>
    <area9>0</area9>
</CGI_Result>

So everything is gone :

I tried this, but it doesn’t work :

        command_on: 'curl -k "http://192.168.1.102:2002/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=1&linkage=142&snapInterval=1&sensitivity=2&triggerInterval=5&isMovAlarmEnable=1&isPirAlarmEnable=0&schedule0=281474976710655&schedule1=281474976710655&schedule2=281474976710655&schedule3=281474976710655&schedule4=281474976710655&schedule5=281474976710655&schedule6=281474976710655&usr=xxx&pwd=yyy"'

Any idea ?

Well, isn’t that…interesting…

I’ll start by telling you something pretty funny actually.

the camera I set that switch up for originally had started acting a bit flaky and I realized that the motion detection got disabled somehow. So I set that switch to be able to re-enable it easily from HA.

Then after a while I noticed that even tho the motion detection was enabled and the schedule was set correctly it still wasn’t triggering the motion detection events. I just chalked it up to being an old camera that was starting to fail. And since I use Blue Iris as an NVR and it has built-in motion detection I never dug into it any further.

But…

based on your post above I now realized that the camera wasn’t detecting events because the "area0’ thru “area9” settings were all set to 0 because I never set those back to the proper values when I turned motion detection back on. :man_facepalming:

TBH, I never even realized those needed set since they are buried another layer down in the camera settings.

It does seem like a poor design that all of the settings get wiped out completely when you just disable motion detection and then you have to set everything back up from scratch after you re-enable it. But it is what it is.

So, now that I got those set correctly my camera is now again sending motion events! Yay! Thanks for giving me that bit of data! :+1:

But now on to your question…

Here is my latest (very very long) command line switch to turn everything back on and reconfigure it properly:

command_on: 'curl -k "http://192.168.1.53:8003/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=1&snapInterval=1&sensitivity=1&triggerInterval=5&schedule0=281474976710655&schedule1=281474976710655&schedule2=281474976710655&schedule3=281474976710655&schedule4=281474976710655&schedule5=281474976710655&schedule6=281474976710655&area0=1023&area1=1023&area2=1023&area3=1023&area4=1023&area5=1023&area6=1023&area7=1023&area8=1023&area9=1023&usr=<user>&pwd=<pass>"'

I don’t see anything obviously wrong in yours tho but the above might help.

And I copied your last CGI command from above and pasted it directly into my browser (after changing to my details and adding back in all of the areaX info from above) and it worked fine for me.

So try adding in the area stuff and see how that goes.

1 Like

Glad I could help you…and glad to hear I’m not the only one experiencing this : everything is going well with these cameras and suddenly… :roll_eyes:

Totally agree… by the way, I just sent a message to Foscam about this. Curious what they will say…

I was actually just about to message you when your message came in, because I discovered something strange.

I will start with my first type of camera (FI9826P).

So I pasted your code (I added the linkage=142) into the switches and…it doesn’t work!

But…it works when I paste the same URL into the browser!?

At first I thought it was because the password ended with a period, but in the meantime that changed too and still the same.

I’ll put my code here again in case I’m missing something…

  - platform: command_line
    switches:
      foscam_camera_living_motion:
        friendly_name: 'Camera Living Motion'
        command_on: 'curl -k "http://192.168.1.101:2001/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=1&linkage=142&snapInterval=1&sensitivity=1&triggerInterval=5&schedule0=281474976710655&schedule1=281474976710655&schedule2=281474976710655&schedule3=281474976710655&schedule4=281474976710655&schedule5=281474976710655&schedule6=281474976710655&area0=1023&area1=1023&area2=1023&area3=1023&area4=1023&area5=1023&area6=1023&area7=1023&area8=1023&area9=1023&usr=xxx&pwd=xxx"'
        command_off: 'curl -k "http://192.168.1.101:2001/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=0&usr=xxx&pwd=xxx"'
        command_state: 'curl -k --silent "http://192.168.1.101:2001/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr=xxx&pwd=xxx"'
        value_template: >
          {% if value | regex_findall(find='isEnable>(\d+)</isEnable') != [] %}
            {{ value | regex_findall_index('isEnable>(\d+)</isEnable') == '1'}}
          {% else %}
           {{ states('switch.foscam_camera_living_motion') }}
          {% endif %}

And my other type of camera (FI9900EP), I cann’t even disabled it (neither via the switch nor via the browser)…

I also found that with my first password that contained a special character (a # symbol in it) I couldn’t get it to work either. there are ways to escape those characters but it was just easier at the time to change it so it contained no special characters.

I think it is something about all of the single and double quotes and special characters in the HA command-line that prevented it from working from the there but worked fine without the quotes in the chrome browser.

If you have any special characters even in the middle of the password try to remove those as well to see if it helps

Yes, everything works fine !

For the second type of camera I have to use setMotionDetectConfig1 and getMotionDetectConfig1 :face_with_raised_eyebrow:

But…if I may, I suggest you should use the same URL for switching on & off (except isEnable=0 or 1).

Why ? If you switch off the motion detection of your camera with your switch in HA and you then go to the app of Foscam to set it back on, all the settings are wiped out.

Based on the sensor I send myself an e-mail so I know when the camera is set on/off or is triggered. But it doesn’t work every time ? Do you know why ?

alias: Camera garage ingeschakeld
description: ''
trigger:
  - platform: state
    entity_id: sensor.motion_camera_garage
    from: Disabled
condition: []
action:
  - service: script.cb_e_mail
    data:
      message: Camera garage ingeschakeld
      title: Camera garage ingeschakeld
mode: single
alias: 'CB : E-Mail'
sequence:
  - service: notify.GMail
    data:
      message: '{{ message }}'
      title: '{{ title }}'
description: Send persistent notification
fields:
  message:
    description: De boodschap van de notificatie
    default: Boodschap
    example: Er is brand in de keuken
  title:
    description: De titel van de notificatie
    default: Titel
    example: Alarm, er is brand !
mode: single

I never go to the Foscam app to set any settings after the original set up. So the HA switch I (now…) have works fine for my needs.

I only set this switch up because I discovered the camera disabled the motion detection on it’s own or at least without me knowingly doing it.

I don’t see that my camera has those commands available. At least they aren’t in the CGI reference I have.

I have no idea.

I’ve got a Foscam too and trying to get this working. I have two zones active. But I don’t seem to understand what I need to do. So to validate my question.

When I set my preferred settings the getMotionDetectConfig1 shows the following config:

<CGI_Result>
    <result>0</result>
    <isEnable>1</isEnable>
    <linkage>0</linkage>
    <snapInterval>1</snapInterval>
    <triggerInterval>5</triggerInterval>
    <isMovAlarmEnable>1</isMovAlarmEnable>
    <isPirAlarmEnable>0</isPirAlarmEnable>
    <schedule0>281474976710655</schedule0>
    <schedule1>281474976710655</schedule1>
    <schedule2>281474976710655</schedule2>
    <schedule3>281474976710655</schedule3>
    <schedule4>281474976710655</schedule4>
    <schedule5>281474976710655</schedule5>
    <schedule6>281474976710655</schedule6>
    <x1>0</x1>
    <y1>0</y1>
    <width1>10000</width1>
    <height1>10000</height1>
    <threshold1>0</threshold1>
    <sensitivity1>3</sensitivity1>
    <valid1>1</valid1>
    <x2>4881</x2>
    <y2>2907</y2>
    <width2>2818</width2>
    <height2>5914</height2>
    <threshold2>0</threshold2>
    <sensitivity2>2</sensitivity2>
    <valid2>1</valid2>
    <x3>0</x3>
    <y3>0</y3>
    <width3>0</width3>
    <height3>0</height3>
    <threshold3>0</threshold3>
    <sensitivity3>0</sensitivity3>
    <valid3>1</valid3>
</CGI_Result>

The command on command should push all these settings if I’m not mistaken. Is it as simple to replace for example 1 by valid3=1 in the command?


Yup, seems to work, my working commandline to set motion detection to on:

http://192.168.x.x:88/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig1&isEnable=1&linkage=0&snapInterval=1&triggerInterval=5&isMovAlarmEnable=1&isPirAlarmEnable=0&schedule0=281474976710655&schedule1=281474976710655&schedule2=281474976710655&schedule3=281474976710655&schedule4=281474976710655&schedule5=281474976710655&schedule6=281474976710655&x1=0&y1=0&width1=10000&height1=10000&threshold1=0&sensitivity1=3&valid1=1&x2=4881&y2=2907&width2=2818&height2=5914&threshold2=0&sensitivity2=2&valid2=1&x3=0&y3=0&width3=0&height3=0&threshold3=0&sensitivity3=0&valid3=1&usr=<user>&pwd=<password>

The Foscam integration uses python-foscam

and unfortunately it doesn’t seem to support motion alarms :frowning:

I have made it into a rest sensor, slightly less hacky I think:

- platform: rest
  name: Motion Indgang
  resource: 'http://192.168.0.10:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=<username>&pwd=<password>'
  scan_interval: 5
  value_template: >
    {% set status = value.replace('</motionDetectAlarm>','<motionDetectAlarm>').split('<motionDetectAlarm>') %}
    {% set values = {'0':'Disabled', '1':'None', '2':'Detected'} %}
    {{ values[status] if status in values.keys() else 'Not Determined'}}

Home Assistant’s RESTsensor supports XML (it rewrites it to JSON), BUT it only works if the endpoint returns “Content-Type: text/xml”
unfortunately Foscam, incorrectly, returns “Content-Type: text/plain”

Funny enough I stumpled upon this few days ago when working with my underfloor heating system, so this seems to be a common problem with many XML endpoints.

There is this request open about adding a option to force XML:

1 Like