Template binary sensor help

Hi,

I am trying to create a new template binary sensor but keep getting error messages.

binary_sensor:
  - platform: template
    sensors: Door Status
    state: "{{ is_state('input_boolean.door_status','on') }}"
     device_class: opening
     icon: >
       {% if is_state('input_boolean.door_status', "on") %}
         mdi:door-open
       {% else %}
         mdi:door-closed
       {% endif %}

Any ideas?

Thanks.

Indentation is off and you appear to use a mix of old and new template format.
Try this:

template:
  - binary_sensor
      - name: "Door Status"
        state: "{{ is_state('input_boolean.door_status','on') }}"
        device_class: opening
        icon: >
          {% if is_state('input_boolean.door_status', "on") %}
            mdi:door-open
          {% else %}
            mdi:door-closed
          {% endif %}
1 Like

Thanks, i want to insert it under this:

# Binary Sensors
binary_sensor:
  - platform: tod
    name: Night
    after: sunset
    after_offset: "-01:00"
    before: sunrise
    before_offset: "+01:00"
    
  - platform: tod
    name: Day
    after: sunrise
    after_offset: "+01:00"
    before: sunset
    before_offset: "-01:00"

However using a template sensor under binary sensor, doesnt work?

I am doing this for normal sensors, see below:

sensor:
  - platform: template
    sensors:
     ext_weather_wind_dir:
      friendly_name: 'Wind Direction'
      value_template: >
          {% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %}
          {% set degree = states('sensor.external_wind_direction')|float %}
            {{ direction[((degree+11.25)/22.5)|int] }}

Template is it’s own integration - like mqtt or something else.
It therefore needs to be OUTSIDE the sensor: and binary_sensor: part of your config.

# Binary Sensors
binary_sensor:
  - platform: tod
    name: Night
    after: sunset
    after_offset: "-01:00"
    before: sunrise
    before_offset: "+01:00"
    
  - platform: tod
    name: Day
    after: sunrise
    after_offset: "+01:00"
    before: sunset
    before_offset: "-01:00"

template:
  - binary_sensor:
      - name: "Door Status"
        state: "{{ is_state('input_boolean.door_status','on') }}"
        device_class: opening
        icon: >
          {% if is_state('input_boolean.door_status', "on") %}
            mdi:door-open
          {% else %}
            mdi:door-closed
          {% endif %}
1 Like

OK, however the previous sensor (weather meter) is working as expected?

If it needs to be seperated, why does the template aspect of this work?

Thanks.

Because the old format uses platform: template but that old format will not receive any more updates, it is left in place for backwards compatibility. The newer format is a 1st class integration which still receives updates, and already provides a lot more functionationality than the older format.

For example:

template:
  - binary_sensor:
      - name: "Door Status"
        state: "{{ is_state('input_boolean.door_status','on') }}"
        device_class: door
        unique_id: door-70db94c7-b297-4cbd-beeb-5b8e3f7941df

adding a unique_id means you can now full control this sensor in the UI.

EDIT:
By setting the device_class to door, you probably don’t need the icon template. That should just work.

Ah perfect, i wasnt aware. Thanks.

Hi,

Trying to change the existing method to the new method, but i am stuck:

Existing:

  - platform: template
    sensors:
     ext_weather_wind_dir:
      friendly_name: 'Wind Direction'
      value_template: >
          {% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %}
          {% set degree = states('sensor.external_wind_direction')|float %}
            {{ direction[((degree+11.25)/22.5)|int] }}

New:

  - sensor:
      - name: "Ext Weather Wind Dir"
        value_template: >
         {% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %}
         {% set degree = states('sensor.external_wind_direction')|float %}
         {{ direction[((degree+11.25)/22.5)|int] }} 

However it throws an error?

Replace value_template with state in the new format.

1 Like