Binary sensor state not updating Foscam infraLedState

Hello all!

I am quite new to the Hassio, but have been able to solve many problems already with existing resources. Now I am trying to get a binary sensor configured so that it would reflect the state of the infrared led on my Foscam camera. My configuration.yaml entry for this sensor is as follows:

binary_sensor:
  - platform: command_line
    name: IR Led State
    command: 'curl -k --silent "https://192.168.10.59/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=user&pwd=password" | grep -oE "<infraLedState>.*</infraLedState>" | sed "s/\(<infraLedState>\|<\/infraLedState>\)//g" | tr -d "\n"'
    value_template: '{{ value == "1" }}'

I used example here, but it didn’t work as such as Busybox does not have grep with option -P. So, to break the command down, it first gets XML state of the camera, then selects infraLedState with regex, then removes tags with sed and finally removes extra line ending sed produces with tr. The command works just fine when I ssh to my Hassio and run it from command line. But, no matter what I do, the state of “IR Led State” will always be off. I even went so far as to change template to:

  value_template: '{{ value != "1" }}'

And it still remains off. There must be something with the templates that I am getting same off state whether I test {{ value == "1" }} or {{ value != "1" }}

Any idea what I am missing here? I have no idea how I can debug the template part. Everything else was somehow testable on the system. I use the ‘States’ menu in ‘Developer tools’ part to see the state of the sensor.

I have Rasperry Pi 3B+ with 32bit with Home Assistant 0.92.2

Anyways, thanks in advance!

I got it working. Fixing it took two revelations.

First part of the fix was to use -q option on grep. It translates found character sequence to 1 and not found to 0. The thread which helped me there was this.

Second part was to look more closely examples here. The important part was to use payload text for states. So, the final solution which started working was this:

binary_sensor:
  - platform: command_line
    name: IR Led State
    command: 'curl -k --silent "https://192.168.10.59:443/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=user&pwd=password" | grep -q "<infraLedState>1</infraLedState>" && echo on || echo off'
    payload_on: "on"
    payload_off: "off"
    command_timeout: 2

If you test this solution and it does not seem to flip the state when infra led changes, do notice that it takes some time before hassio next checks states of sensors.

Hopefully this helps someone with similar issues!