[SOLVED] Add a switch Foscam

here is a command_line switch I use to turn on and off motion detection on one of my Foscam’s"

- platform: command_line
    switches:
      foscam_old_lr_motion:
        friendly_name: 'Old LR 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=xxxxx&pwd=yyyyyyyy"'
        command_off: 'curl -k "http://192.168.1.53:8003/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=0&usr=xxxx&pwd=yyyyyyyy"'
        command_state: 'curl -k --silent "http://192.168.1.53:8003/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr=xxxx&pwd=yyyyyyyyy" | grep -oP "(?<=isEnable>).*?(?=</isEnable>)"'
        value_template: '{{ value == "1" }}'

You’ll have to determine which cgi-command does what you need and substitute it in the curl command

Thank you but I want a switch to power on/off the camera
But it might not be possible so the alternative would be to power connect my camera to a connected wall socket.

I also need to have a sensor which detect the motion, so it is either possible with command_line but I don’t know how, or the alternative is to have a motioneyes/ZoneMinder setup connected to HA.

As far as I know there’s no way to do that in the camera.

Yes, you will need a automated power connector. Either a wall socket or extension cord that you can control from HA.

for motion detection try this:

- platform: command_line
  name: "Garage Camera Motion"
  command: 'curl -k --silent "http://192.168.1.XX:YYYY/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=xxxx&pwd=yyyyyy" | grep -oP "(?<=motionDetectAlarm>).*?(?=</motionDetectAlarm>)"'
  value_template: >-
    {%- if value == "0" -%}
      Disabled
    {%- elif value == "1" -%}
      None
    {%- elif value == "2" -%}
      Detected
    {%- endif -%}
  scan_interval: 3 
  #friendly_name: 'Garage'

Let me know if it works. I have heard that the grep command might not work in hassio. If it doesn’t work then I can give you another way to try.

I agree with @finity here - I have not seen a camera yet that allows to power off the camera itself by e.g. sending a command to it. How would you turn it on again in this case anyway?

A smart plug or power socket is probably the way to go - that’s what I do with my cameras as well.
I use cheap Etekcity plugs and contol them from my Broadlink RM Pro - used to use a cheapo RF thingie (forgot the name) connected to my RPi3 itself, but the range didn’t work out.

Because the Etekcity switch does not confirm the status change, the feedback loop (i.e. is the camera really on?) works by using the device_tracker for each camera.
If the camera is on the network, the power plug must be switched on.

1 Like

The Command failed, I also tried to change my username/password to set a simple one.

I was thinking of Xiaomi camera which you can turn off.
Of course it is still powered but the video stream and motion recording, detection are all off.

Did the command fail or did the state not detect that the motion is enabled?

> Command failed: curl -k --silent "http://192.168.1.16:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&amp;usr=myuser&amp;pwd=mypwd" | grep -oP "(?&lt;=motionDetectAlarm&gt;).*?(?=&lt;/motionDetectAlarm&gt;)"

run this command in the terminal and see what you get (adding in your credentials):

curl -k --silent "http://192.168.1.16:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=myuser&pwd=mypwd1"

Output result :

19

And the motionDetectAlarm is set to 2 when it detect something

try replacing these lines in the command line sensor from before (again with your credentials) with the following :

command: 'curl -k --silent "http://192.168.1.16:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&amp;usr=myuser&amp;pwd=mypwd1" '
value_template: >
  {% set status = value | regex_findall_index('<motionDetectAlarm>(.*)</motionDetectAlarm>') %}
  {% if status == '0' %}
    Disabled
  {%- elif status   == "1" -%}
    None
  {%- elif status == "2" -%}
    Detected
  {%- endif -%}

I now have “command_line : error on device update”

the curl -k line works fine in a terminal

I also tried to set up a switch with another code I found here and it told me the same error : “command failed”

I just put the following in my (non-hassio) config and it is working fine (credentials redacted):

    - platform: command_line
      name: "Garage Camera Motion Test"
      command: 'curl -k --silent "http://192.168.1.WW:ZZZZ/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=xxxx&pwd=yyyyy"'
      value_template: >-
        {% set status = value | regex_findall_index('<motionDetectAlarm>(.*)</motionDetectAlarm>') %}
        {% if status == "0" %}
          Disabled
        {%- elif status   == "1" -%}
          None
        {%- elif status == "2" -%}
          Detected
        {%- endif -%}
      scan_interval: 3 
      #friendly_name: 'Garage'

yes your code should work I just figured the problem is user access right related.
I’m running Hassio in Docker and Hassio don’t have the right to access to the command line

I thought the results you got in the screen shot above were from running the command from within hassio?

yes from my Mac terminal, but I tried in the Hassio terminal and it is working too.

Then I’m not understanding what you mean by

If it works in the hassio terminal then hassio has permissions to run that command.

you should be right, but after a restart I have many errors with the HA database and nothing is working now. I will start all over.

New HA setup and it works fine, thanks for the help

that’s good that it’s working.

If you could please mark the post with the working code that you used as “solution” so others can see what worked.

1 Like