Template.yaml - Formatfrage / state oder value_template

Hallo,
zum Kombinieren von 2 Türkontakten habe ich mir eine Template-Beschreibung zusamengeschustert. Im Entwicklungswerkzeug/Template erfolgt auch die gewünschte Ausgabe. Nach dem Kopieren in die Template.yaml erhalte ich jedoch entweder eine Fehlermeldung (bei state):

`Invalid config for ‘template’ at template.yaml, line 17: invalid template (TemplateSyntaxError: unexpected ‘%’) for dictionary value ‘binary_sensor->0->state’, got “{{% if states(‘binary_sensor.o_c_bad_1og1_contact’) == ‘on’ %} offen {% elif states(‘binary_sensor.o_c_bad_1og2_contact’) == ‘on’ %} offen {% else %} zu {% endif %}}”

` oder er erscheint einfach nicht, ohne Fehler (value_template)

### template.yaml ####
  - binary_sensor:
    - name: O/C_Bad1_OG
      unique_id: "o_c_bad1_og"
      value_template: >-
        {{% if states('binary_sensor.o_c_bad_1og1_contact') == 'on' %}
        true
        {% elif states('binary_sensor.o_c_bad_1og2_contact') == 'on' %}
        true
        {% else %}
        false
        {% endif %}}

Welche Variante muss ich nehmen, um einen Sensor nach Bedingungen zu definieren und wo liegt der Formatierungsfehler bei der “state-Variante”?
Danke und Gruß

Moin,

value_template: ist in der alten templating version (Legacy)

Altes Template / Legacy

Legacy Sensor configuration format

This format still works but is no longer recommended. Use modern configuration.

This format is configured as a platform for the sensor integration and not directly under the template integration.

# Example configuration.yaml entry
sensor:
 - platform: template
   sensors:
     solar_angle:
       friendly_name: "Sun angle"
       unit_of_measurement: "degrees"
       value_template: "{{ state_attr('sun.sun', 'elevation') }}"

     sunrise:
       value_template: "{{ state_attr('sun.sun', 'next_rising') }}"

in der Neuen methode wie du es hast ist es state:

Der fehler bei dir ist aber auch noch ein anderer.

Da hast du vorne eie { zu viel

und hinten eine } zu viel

Edit:
Infos zu Jinja2

ist auch in dev tools von HA verlinkt:
image

LG
Tobi

Hallo Tobi,

danke, es hat funktioniert:

  - binary_sensor:
    - name: "O/C_Bad1_OG"
      unique_id: "o_c_bad1_og"
      state: >-
        {% if is_state('binary_sensor.o_c_bad_1og1_contact', 'on') %}
          true
        {% elif is_state('binary_sensor.o_c_bad_1og2_contact', 'on') %}
          true
        {% else %}
          false
        {% endif %}

Funktioniert…
Vielen Dank! Jetzt muss ich nur noch schauen wie ich den Typ von binary auf window mit den States open/closed ändere, aber das ist ein anderes Thema. danke!

Du brauchst noch die device_class:

- binary_sensor:
    - name: "O/C_Bad1_OG"
      unique_id: "o_c_bad1_og"
      device_class: window
      state: >-
        {% if is_state('binary_sensor.o_c_bad_1og1_contact', 'on') %}
          true
        {% elif is_state('binary_sensor.o_c_bad_1og2_contact', 'on') %}
          true
        {% else %}
          false
        {% endif %}

Edit:
du könntest über die Helfer auch eine Gruppe der Sensoren erstellen, dann brauchst du das nicht über templates machen.

LG
Tobi