Template sensor fails when using as binary_sensor

binary_sensor:
  - platform: command_line
    name: Status
    scan_interval: 60
    command: "/Users/server/scripts/status.sh"
    value_template: "{{ value }}"

The script returns True or False (tested with on and off). The status is alway unknown.

But it worked like this:

sensor:
  - platform: command_line
    name: Status
    scan_interval: 60
    command: "/Users/server/scripts/status.sh"
    value_template: "{{ value }}"

What is wrong here?

Paste the states of the sensor in here for the working configuration.

binary_sensor.status
Status
unknown

sensor.status
Status
False

I copied the code from binary_sensor: to sensor:

Bash:

if [ -f "$FILE" ]; then
    echo "True"
else
    echo "False"
fi

It should be resolving it. It’s possible there’s hidden special characters in the response due to the encoding level. Change your value_template to

"{{ 'True' in value }}"

Still unknown:
value_template: "{{ 'True' in value }}"

I don’t see how it could be. How are you checking the state of the binary sensor

In frontend and developer tools.

Not sure what to tell you. Without looking at the docs, it should be working after a restart.

I have four sensors in binary_sensor. One works the others don’t. They are all platform: command_line.

It does work in sensor:

Is there a disadvantage using it under sensor:?

Do you have 2 binary sensor sections? If yes, you can only have 1. Merge them together

I do have one in template where I use - binary_sensor: each time.

In the binary_sensor.yaml I only have this:

  - platform: ping
    host: 8.8.8.8
    count: 1
    scan_interval: 60
    name: Internet

  - platform: command_line
...

Just to be sure, I can use template binary sensor and binary sensor, right?

Yes.

My only other thought is that you’re looking at the wrong binary_sensor and a new one was created with a similar name.

According to the documentation, the default values for payload_on and payload_off are ON and OFF, respectively. In your case, the received values are True and False so modify the values of payload_on and payload_off.

binary_sensor:
  - platform: command_line
    name: Status
    scan_interval: 60
    command: "/Users/server/scripts/status.sh"
    value_template: "{{ value }}"
    payload_on: "True"
    payload_off: "False"
2 Likes

Wow, that was really the issue. Thanks a lot for that. I was about to give up.

1 Like