Foscam Cam - How to create sensor and switches?

Hi all,

I just bought a Foscam cam and added to HA using Foscam Integrations. I can control PTZ but there’s no evidence of the switch and sensor to activate-deactivate motion or sound detection.

I’ve found older post (this and this) and tried to make a collage but without full success. Instead of necroposting I preferred open a new one to collect all the info.

Till now I accomplished to have a motion switch using this code:

switch:
  - platform: command_line
    switches:
      foscam1_motion:   
        command_on: 'curl -k "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?usr=Foscam1&pwd=xxxxx&cmd=setMotionDetectConfig&isEnable=1&linkage=15&snapInterval=1&sensitivity=1&triggerInterval=0&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=783&area6=527&area7=775&area8=771&area9=768"'
        command_off: 'curl -k "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?usr=Foscam1&pwd=xxxxx&cmd=setMotionDetectConfig&isEnable=0"'
        command_state: 'curl -k --silent "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?usr=Foscam1&pwd=xxxxx&cmd=getMotionDetectConfig" | grep -oP "(?<=isEnable>).*?(?=</isEnable>)"'
        value_template: '{{ value == "1" }}'

I tried to use same logic for the motion sensor but it stays always on none or disabled (never change to detected even if I receive notification via Foscam App:

sensor:
  - platform: command_line  
    command: curl -k --silent "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=Foscam1&pwd=xxxxxxxx"
    name: "Foscam 1 Motion"
    value_template: >
      {% set status = value | regex_findall_index('Alarm>(\d+)</motion') %}
      {% if status == "0" %}
        Disabled
      {%- elif status == "1" -%}
        None
      {%- elif status == "2" -%}
        Detected
      {% else %}
        Not Determined
      {%- endif -%}
    scan_interval: 3

Just to add all the info this is what I see when using the following link in a web browser:

http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=Foscam1&pwd=xxxxxxx

<CGI_Result>
    <result>0</result>
    <IOAlarm>0</IOAlarm>
    <motionDetectAlarm>1</motionDetectAlarm>
    <soundAlarm>1</soundAlarm>
    <record>0</record>
    <sdState>0</sdState>
    <sdFreeSpace>0k</sdFreeSpace>
    <sdTotalSpace>0k</sdTotalSpace>
    <ntpState>1</ntpState>
    <ddnsState>0</ddnsState>
    <url></url>
    <upnpState>0</upnpState>
    <isWifiConnected>1</isWifiConnected>
    <wifiConnectedAP>Casa+Concordia</wifiConnectedAP>
    <infraLedState>0</infraLedState>
    <humanDetectAlarmState>1</humanDetectAlarmState>
    <sdFormatError>0</sdFormatError>
</CGI_Result>

And this is the result of this other link:

http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr=Foscam1&pwd=xxxxxxxxx

<CGI_Result>
    <result>0</result>
    <isEnable>1</isEnable>
    <linkage>128</linkage>
    <snapInterval>1</snapInterval>
    <sensitivity>1</sensitivity>
    <triggerInterval>0</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>783</area5>
    <area6>527</area6>
    <area7>775</area7>
    <area8>771</area8>
    <area9>768</area9>
</CGI_Result>

My goals are:

  • Have motion sensor working
  • Have sound switch and sound sensor
  • Have custom script to load presaved PTZ position
  • Have the stream in frontend in real time (at the moment I have to wait 5 sec to see stream)

Why?

Because I want a reliable baby monitor to monitor 3 babies and a commercial products costs 3 times more that a IP cam 1080p.

Thank you in advance

Does the "<motionDetectAlarm>" value at the first link above ever change to “2” when the camera detects motion?

it should stay on “2” for at least several seconds after motion stops.

Hi,

Yes. Both “motion” and “sound” change to 2 when triggered.
So it’s definitely the sensor that don’t catch the change

I just tried your motion sensor code with one of my Foscam cameras (substituting my credentials) and it’s working as expected.

1 Like

Damn!

I added the specific sensor to the history stats and I see now the change state.

Sorry…

For the record I made to have also sound and switch for audio. Below the code for future users:

sensor:
  - platform: command_line  
    command: curl -k --silent "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=Foscam1&pwd=xxxxxxx"
    name: "Foscam 1 Motion"
    value_template: >
      {% set status = value | regex_findall_index('Alarm>(\d+)</motion') %}
      {% if status == "0" %}
        Disabled
      {%- elif status == "1" -%}
        None
      {%- elif status == "2" -%}
        Detected
      {% else %}
        Not Determined
      {%- endif -%}
    scan_interval: 3

  - platform: command_line  
    command: curl -k --silent "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=Foscam1&pwd=xxxxxx"
    name: "Foscam 1 Sound"
    value_template: >
      {% set status = value | regex_findall_index('Alarm>(\d+)</sound') %}
      {% if status == "0" %}
        Disabled
      {%- elif status == "1" -%}
        None
      {%- elif status == "2" -%}
        Detected
      {% else %}
        Not Determined
      {%- endif -%}
    scan_interval: 3
switch:
  - platform: command_line
    switches:
      foscam1_motion:
        friendly_name : "Foscam 1 Motion"
        # command_on: 'curl -k "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?usr=Foscam1&pwd=xxxxxxxx&cmd=setMotionDetectConfig&isEnable=1&linkage=15&snapInterval=1&sensitivity=1&triggerInterval=0&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=783&area6=527&area7=775&area8=771&area9=768"'
        command_on: 'curl -k "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?usr=Foscam1&pwd=xxxxxx&cmd=setMotionDetectConfig&isEnable=1"'
        command_off: 'curl -k "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?usr=Foscam1&pwd=xxxx&cmd=setMotionDetectConfig&isEnable=0"'
        command_state: 'curl -k --silent "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?usr=Foscam1&pwd=xxxxxx&cmd=getMotionDetectConfig" | grep -oP "(?<=isEnable>).*?(?=</isEnable>)"'
        value_template: '{{ value == "1" }}'
  - platform: command_line
    switches:
      foscam1_sound:
        friendly_name : "Foscam 1 Sound"
        command_on: 'curl -k "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?usr=Foscam1&pwd=xxxxxxxxxx&cmd=setAudioAlarmConfig&isEnable=1"'
        command_off: 'curl -k "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?usr=Foscam1&pwd=xxxxxxxxx&cmd=setAudioAlarmConfig&isEnable=0"'
        command_state: 'curl -k --silent "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?usr=Foscam1&pwd=xxxxxx&cmd=getAudioAlarmConfig" | grep -oP "(?<=isEnable>).*?(?=</isEnable>)"'
        value_template: '{{ value == "1" }}'

I take advantage of your kindness to ask you support for the other point.

I’m trying to add a switch to load 2 different preset 1) AtHome 2) AtWatch. At this moment I have command for on and off but how can I see the current preset (if any)?

- platform: command_line
    switches:
      foscam1_preset:
        friendly_name : "Foscam 1 Preset"
        command_on: 'curl -k "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?usr=Foscam1&pwd=xxxxxxx&cmd=ptzGotoPresetPoint&name=AtWatch"'
        command_off: 'curl -k "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?usr=Foscam1&pwd=xxxxx&cmd=ptzGotoPresetPoint&name=AtHome"'
        # command_state: TBD
        # value_template: TBD

Moreover do you have a suggestion to make the stream load faster when I open the frontend?

Thank you!

I didn’t find that command in the CGI reference that I have so I don’t know if that’s possible.

Not really. I doubt it’s a camera limitation but likely a limitation of the HA machine you are viewing the stream in.

1 Like

I did a research in the “Foscam IPCamera CGI User Guide” and found nothing. That’s a pity.

I will try to add camera via Frigate (that I use for general videosurveillance) and see if results change.

Thank you for your support!

1 Like

Maybe this code might be helpfull:

~ binary_sensor:

1 Like

thanks! Now it is far more usable. I would be great to add those sensor in default integration

Hi, it’s still me. I noticed other sensors that can be useful and I tried to replicate the syntax without success. Any help?

  - sensor:
      command: curl -k --silent "http://192.168.2.114:88/cgi-bin/CGIProxy.fcgi?cmd=getNetworkAutoAdaptability&usr=xxxx&pwd=xxxx"
      name: "Foscam 1 NAA"
      value_template: >
        {% set status = enable %}
        {% if status == "0" %}
          Disabled
        {%- elif status == "1" -%}
          Enabled
        {% else %}
          Not Determined
        {%- endif -%}
      scan_interval: 3

Sensor should display Enabled:

<CGI_Result>
    <result>0</result>
    <enable>1</enable>
</CGI_Result>

but returns Not Detemined.

Thanks in advance!

you can try this:

value_template: >
  {% set status = value | regex_findall_index('enable>(\d+)</enable') %}
  {% if status == "0" %}
    Disabled
  {%- elif status == "1" -%}
    Enabled
  {% else %}
    Not Determined
  {%- endif -%}

or if that doesn’t work it should if you replace the above with:

value_template: >
  {% set status = value | regex_findall_index('enable>(\d+)</enable') %}
  {% set values = {'0':'Disabled', '1':'Enabled'} %}
  {{ values[status] if status in values.keys() else 'Not Determined'}}
1 Like

it worked (second version). Thanks!

1 Like