Invert a attribute

hello,
how can i invert this attribute here?
image

this here doesnt works:

- platform: template
  sensors:
    huehner_klappe_inv:
      value_template: >-
         {% if is_state('binary_sensor.huehnerstall_klappe_contact.contact', 'true') %}
         false
         {%- else -%}
         true
         {%- endif %}
      friendly_name: Huehnerstall Klappe Inv
      device_class: door

image

your template is wrong.

try this:

value_template: >
  {% if is_state_attr('binary_sensor.huehnerstall_klappe_contact', 'contact', 'true') %}
    false
  {%- else -%}
    true
  {%- endif %}

just a question: since this binary probably is on/off based on that attribute, you could also create a new template sensor based on the state of the binary itself?

{% set inversed = 'binary_sensor.updater' %}
{% if is_state(inversed,'off') %} on
{%- else -%} off
{%- endif %}

which can be shortened to:

      inversed_short:
        value_template: >
          {% set inversed = 'input_boolean.test' %}
          {{is_state(inversed,'off')}}

seems, that is doesnt work

image

no contact attribute:

- platform: template
  sensors:
    huehner_klappe_inv:
      value_template: >
        {% if is_state_attr('binary_sensor.huehnerstall_klappe_contact', 'contact', 'true') %}
          false
        {%- else -%}
          true
        {%- endif %}
      friendly_name: Huehnerstall Klappe Inv
      device_class: door 

Right.

You didn’t create the contact attribute in the second binary sensor. You just created the binary sensor and controlled it’s state by referencing the attribute of the first sensor.

FYI, a simpler value template:

value_template: "{{ not is_state_attr('binary_sensor.huehnerstall_klappe_contact', 'contact', 'true') }}"

or:

{{is_state_attr('binary_sensor.huehnerstall_klappe_contact', 'contact', 'false') }}

which is essentially the same trick as I suggested above on the binary itself

1 Like

close, but it’s an attribute so True/False are booleans not strings, which has been the issue from the beginning.

value_template: "{{ not state_attr('binary_sensor.huehnerstall_klappe_contact', 'contact') }}"

@noxx The inversion will be the main state of the sensor, not a contact attribute. If you want it to keep it an attribute…

- platform: template
  sensors:
    huehner_klappe_inv:
      value_template: >
        {{ states('binary_sensor.huehnerstall_klappe_contact') }}
      attribute_templates:
        contact: >
         {{ not state_attr('binary_sensor.huehnerstall_klappe_contact', 'contact') }}
      friendly_name: Huehnerstall Klappe Inv
      device_class: door
2 Likes