Help with a template binary sensor

Tags: #<Tag:0x00007f73b94fe000>

Hi all,

I have a binary sensor and I want to create a template binary sensor that show opposite state.

This is what I built:

binary_sensor:
  - platform: template
    sensors:
      porta_corazzata:
        friendly_name: "Porta Corazzata"
        value_template: >-
          {% if is_state('binary_sensor.portacorazzatainvertito','on') %}
          off
          {% elif is_state('binary_sensor.portacorazzatainvertito','off') %}
          on
          {% else %}
          Sconosciuto
          {% endif %}

I’ve tested also other solution but both sensor have always same status.

Can someone please help me?

Binary sensors can only be on or off. How about this:

binary_sensor:
  - platform: template
    sensors:
      porta_corazzata:
        friendly_name: "Porta Corazzata"
        value_template: >-
          {% if is_state('binary_sensor.portacorazzatainvertito','on') %}
          off
          {% elif is_state('binary_sensor.portacorazzatainvertito','off') %}
          on
          {% endif %}

Hi,
a binary sensor has only two states: on and off. If you want to have three states (on, off & Sconosciuto), then you need to use a temporary sensor:

sensor:
  - platform: template
    sensors:
      porta_corazzata:
        friendly_name: "Porta Corazzata"
        value_template: >
           {% if  is_state('binary_sensor.portacorazzatainvertito', 'on') %}
             off
           {% elif is_state('binary_sensor.portacorazzatainvertito', 'off') %}
             on
           {% else %}
             Sconosciuto
           {% endif %}
        icon_template: mdi:clock

Well yeah, but there’s no way for a binary sensor to not be on or off. I don’t think it’s possible for a template sensor to return unknown and for it to be set to that.

binary_sensor:
  - platform: template
    sensors:
      porta_corazzata:
        friendly_name: "Porta Corazzata"
        value_template: >-
          {% not states('binary_sensor.portacorazzatainvertito') %}

This should work:

binary_sensor:
  - platform: template
    sensors:
      porta_corazzata:
        friendly_name: "Porta Corazzata"
        value_template: >-
          {{ is_state('binary_sensor.portacorazzatainvertito', 'off') }}