noxx
(Ralf B)
July 6, 2021, 7:07am
1
hello,
how can i invert this attribute here?
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
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')}}
noxx
(Ralf B)
July 6, 2021, 7:52am
4
seems, that is doesnt work
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
noxx:
no contact attribute:
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.
tom_l
July 6, 2021, 9:22am
6
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
petro
(Petro)
July 6, 2021, 1:23pm
8
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