Trouble returning Foscam camera motion detect state

I followed the instructions posted below to setup a command line switch to turn on and off the motion detection for a foscam FI9831P V2 IP camera. I am able to use the switch to turn On the Motion detection, however it is not returning the state correctly, so the switch in HA turns back off even thou the motion detection is on. I tested both the command on and command off statements in a browser and they are both working as expected. It is just the command state that is giving me trouble.

https://www.home-assistant.io/cookbook/foscam_away_mode_PTZ/

I am using slightly modified version of the yaml script that I found here:

switches:
  foscam_motion:
    command_on: 'curl -k "http://yourip:88/cgi-bin/CGIProxy.fcgi?usr=username&pwd=password&cmd=setMotionDetectConfig&isEnable=1"'
    command_off: 'curl -k "http://yourip:88/cgi-bin/CGIProxy.fcgi?usr=username&pwd=password&cmd=setMotionDetectConfig&isEnable=0"'
    command_state: 'curl -k --silent "http://yourip:88/cgi-bin/CGIProxy.fcgi?usr=username&pwd=password&cmd=getMotionDetectConfig" | grep -oP "(?<=isEnable>).*?(?=</isEnable>)"'
    value_template: '{{ value == "1" }}'

When I just use the http command in the command state without the grep statement in my browser I get the following:

 <CGI_Result>
    <result>0</result>
    <isEnable>1</isEnable>
    <linkage>8</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 statement I want to find and evaluate is the isEnable statement on the second line. 1 is On and 0 is Off. However, the grep statement does appear to be correctly returning the value and therefore cannot set the state. I am not real familiar with the grep statement so not sure if the statement I am using is correct or if it has changed since the original instructions where written. Anyone have here have any suggestions on how to correctly evaluate the CGI Results to return the correct isEnable state?

Thanks for the help.

Andrew J

I was just rtying to help another user a few days ago that was trying to extract a valu from a command_line sensor and we could never get the “grep” command to work while it was embedded in the curl command line.

They were using hassio while i am using HA in Docker but non-hassio. So there may be something screwy in the way grep is used in hassio that doesn’t quite work correctly. But it works fine in my set up and I’m successfully using pretty much the exact command as you for my Foscam camera’s to check the status of motion enabled and mine is working perfectly fine.

I think the bottom line for that discussion was to use the “regex_findall_index” filter.

So I think based on that the following should get you what you want:

switches: 
  foscam_motion: 
    command_on: 'curl -k "http://yourip:88/cgi-bin/CGIProxy.fcgi?usr=username&pwd=password&cmd=setMotionDetectConfig&isEnable=1" ' 
    command_off: 'curl -k "http://yourip:88/cgi-bin/CGIProxy.fcgi?usr=username&pwd=password&cmd=setMotionDetectConfig&isEnable=0" '
    command_state: 'curl -k --silent "http://yourip:88/cgi-bin/CGIProxy.fcgi?usr=username&pwd=password&cmd=getMotionDetectConfig" '
    value_template: "{{ value | regex_findall_index('<isEnable>(.*)</isEnable>') == '1' }}"

finity. Thank you very much. That worked like a charm. I am now able to enable and disable the foscam motion sensor from HA without any problems.

I am using hassio running in a VirtualBox VM on a Windows PC. I just migrated to this new platform this weekend. I have been had several SD card corruption issues on my RPI 3B+, so hoping that this windows machine running VirtualBox will be a little more stable. I followed the instructions provided by TheHookUp below.

Thanks again for your help. I appreciate you sharing your expertise.

Andrew J

Finity Thanks :kiss: for your input

I have got it working too, but how do I get an entity with the state of detection?

There are a couple of CGI interfaces that you might use depending on the camera.

I think most of the new(er) cameras use the second style below. But look at the CGI reference for your cameras to very what the command is and modify these as needed:

  - platform: command_line
    name: "Kitchen Camera Motion"
    command: curl -k --silent "http://192.168.1.51:8001/get_status.cgi?user=<username>&pwd=<password>"
    value_template: >-
      {% set status = value | regex_findall_index('alarm_status=(\d+);') %}
      {%- if status == "0" -%}
        None
      {%- elif status == "1" -%}
        Detected
      {% else %}
         Not Determined
      {%- endif -%}
    scan_interval: 3 
    
  - platform: command_line  
    command: curl -k --silent "http://192.168.1.52:8002/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=<username>&pwd=<password>"
    name: "Garage Camera 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