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.
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?
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:
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.
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