Trouble with binary sensor

I am trying to create a binary sensor and have the following

  - platform: template
        friendly_name: 'Car in Garage'
        device_class: 'presence'
        value_template: {{ state('sensor.distance_to_car')|float < 2.0 }}

(The template works fine in the Template tester).

but it fails the config check with the following error

                      Error loading /config/configuration.yaml: invalid key: "OrderedDict([("state('sensor.distance_to_car')|float < 2.0", None)])"

in “/config/configuration.yaml”, line 63, column 0

The template needs to be in quotes.

value_template: "{{ states('sensor.distance_to_car')|float < 2.0 }}"

Thanks mate, so simple.

I was going from the first example here

that doesnt have it in quotes. When I look closer, the later examples do.

@Tediore, is this something that needs to be fixed in the docs?

Not in this case at least (although there might be some wrong examples elsewhere in the docs). The examples without quotes look like this, which is correct:

value_template: >-
  {{ state_attr('sun.sun', 'elevation')|float > 0 }}

@dazza25 note the >- after value_template: in the first example. That’s the syntax for creating multi-line templates (the hyphen also tells it to strip whitespace from the beginning and end, although that’s not strictly necessary). When you use the multi-line template syntax, even if the template itself is only one line, you don’t need to wrap the template in quotes. When it’s written like the one in your original post it needs to be wrapped in quotes.

If it isn’t broken I won’t fix it :slight_smile:

1 Like

A further question, when I add the unique_id parameter as in

  - platform: template
        unique_id: is_car_in_garage
        friendly_name: 'Car in Garage'
        device_class: 'presence'
        entity_id: 'sensor.distance_to_car'
        value_template: "{{ state('sensor.distance_to_car')|float < 1.8 }}"

I get the error

Invalid config for [binary_sensor.template]: [unique_id] is an invalid option for [binary_sensor.template]. Check: binary_sensor.template->sensors->car_in_garage->unique_id. (See ?, line ?).

Hmm, this looks like an issue. I see that the unique_id was added beginning of August for binary sensors. Could be that your are running an older version that doesn’t have this change included. Which HA version are you running?

Yes I am on 0.113, hadnt upgraded to 0.114 so that is probably it. So taken that out for now, no big deal.

Bigger issue is it never updates. Whether the distance is less or more than 1.8m, it is always showing false. Do I need to add more code to say if > 1.8 set to false and if less set to true?

it should be states(...) and not state(...).
See here in the template docs for more details.

@Tediore could you please correct your post to reflect this?

Ha, thanks. Updated…can’t believe I didn’t see that.

I have brought great shame to myself and my family.


Thanks guys, yes worked that out and was coming back here to say it. Working like a charm now.