Binary_sensor unknown status

Hello everyone,
When I configured a sensor to detect motion it works perfect like this:

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

But since the blueprint I want to use uses a binary_sensor, I tried to set it up like this:

# Camera motion detection
command_line:
 - binary_sensor:
    name: "Motion Detection Foscam"
    unique_id: "Motion_Detection_Foscam"
    command: curl -k --silent "http://192.168.2.32:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=zuidervos&pwd=xxx"
    value_template: >
      {% set status = value | regex_findall_index('Alarm>(\d+)</motion') %}
      {% set values = {'0':'Disabled', '1':'off', '2':'on'} %}
      {{ values[status] if status in values.keys() else 'Not Determined'}}
    scan_interval: 5
    device_class: motion

This gives me a permanently "unknown’’ status, which should be the binary “off” . I am a bit new to this, so I am unsure if I used these values correctly or if there is another format I need to use. Can anyone help me with this?

You can’t have Disabled or Not Determined as the state of a binary sensor.

Set an availability template:

# Camera motion detection
command_line:
  - binary_sensor:
      name: "Motion Detection Foscam"
      unique_id: "Motion_Detection_Foscam"
      command: curl -k --silent "http://192.168.2.32:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=zuidervos&pwd=xxx"
      value_template: >
        {% set status = value | regex_findall_index('Alarm>(\d+)</motion') %}
        {{ status == '2' }}
      availability: >
        {% set status = value | regex_findall_index('Alarm>(\d+)</motion') %}
        {{ status in ('1','2') }}
      scan_interval: 5
      device_class: motion

Ah ok. Using these lines the motion detection is now listed as permanently unavailable.

Can you post an example of what the curl command returns? Just add:

 - sensor:
     name: "Motion Detection Foscam output"
     command: curl -k --silent "http://192.168.2.32:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=zuidervos&pwd=xxx"
     value_template: "{{ value[:254] }}"

The [:254] is just to truncate the output to within the maximum state size of a sensor.

You have put back the pwd parameter into the URL, haven’t you, assuming it’s not really xxx?

Thank you for helping. I did put the password back in. This is what the output looks like and the highlighted 1 does become a 2 when motion is triggered:
image

I do know that my previous sensor code definitely worked for a normal sensor and it set the correct “Clear” and “Detected” values when triggered. The problem I’m facing is that I do not know the syntax for assigning a “true” or “false” value to the binary sensor:

      {% set values = {'0':'Disabled', '1':'off', '2':'on'} %}

That’s exactly what my

{{ status == '2' }}

is meant to do. That’s a statement that is either true or false.

Take a look in your logs…

You could (should?) set this up as a RESTful binary sensor, with your number being in:

{{ value_json['CGI_Result']['motionDetectAlarm'] }}

I looked at the HA logs, but there is no log related to the binary sensor.
I solved it using a work around:


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

binary_sensor:
  - platform: template
    sensors:
      foscam_motion_dummy:
        friendly_name: "Foscam Motion Dummy"
        value_template: >
          {% if is_state('sensor.motion_detection_foscam', 'Detected') %}
            true
          {% elif is_state('sensor.motion_detection_foscam', 'Clear') %}
            false
          {% endif %}
        device_class: motion