Template mit mehreren states anlegen

Grüße ins Board,

Ich bin nicht so bewandert mit dem yaml-Code und lerne jeden Tag etwas dazu.
Vielleicht kann mir jemand helfen.

Möchte ein Template anlegen, dass drei verschiedene states rück geben kann.
Ich habe praktisch im Fenster, jeweils unten und oben, einen Kontaktsensor versteckt. Je nach dem Ob das Fenster Schräg, offen oder geschlossen ist, möchte ich im Template das so auslesen wollen.

Anstatt immer beide States anzuschauen, dachte ich eben bau ich ein template.
Den Code unten habe ich mal in config.yaml eingegeben, aber nicht gespeichert, traue mich nicht :see_no_evil::face_with_hand_over_mouth:. Ein code-Fehler wird mal nicht angezeigt.

Wie ist die Meinung von euch, könnte man das so lösen, oder gibt es eine elegantere Art?

Sorry, hatte Probleme hier den Code einzubetten, copy&pasted ist nicht so das ware aus der yaml Datei.

Config.yaml
template:  
      - binary_sensor:
            - name: "Alex Fenster status"
              unique_id: "Alex_Fenster_status"
              state: "{% if is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening' , 'on') and is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_8' , 'off') -%}
                      schraeg
                      {%- else -%}
                         {% if is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening' , 'on') and is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_8' , 'on') -%}      
                         offen                    
                         {%- else -%}
                         geschlossen
                         {%- endif %}
                      {%- endif %}"

Danke für eure Hilfestellung

Cu Alex

Teste am besten zuerst deine if else Kombi unter Entwicklerwerkzeuge - Template. Dann siehst du ob es funktioniert. Aber wirklich nur die if else testen.

Teste am besten zuerst deine if else Kombi unter Entwicklerwerkzeuge - Template. Dann siehst du ob es funktioniert. Aber wirklich nur die if else testen

Da funktioniert es. Aber ich bin mir nicht sicher wie das in der config.yaml nach „State:“ genau aussehen soll.
Verschiedene Code wie „>“ oder „>-„ habe ich gesehen die nach dem States: kommen, habe aber noch nicht verstanden für was.


Config.yaml
template:  
      - binary_sensor:
            - name: "Alex Fenster status"
              unique_id: "Alex_Fenster_status"
              state: >- ## <- vielleicht so ohne Anführung?
                 {% if is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening' , 'on') and is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_8' , 'off') -%}
                      schraeg
                 {%- else -%}
                         {% if is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening' , 'on') and is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_8' , 'on') -%}      
                         offen                    
                         {%- else -%}
                         geschlossen
                         {%- endif %}
                 {%- endif %}

Ich kenne nur diese Schreibweise

{% if is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening' , 'on') and is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_8' , 'off')  %}
schraeg
{% elif is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening' , 'on') and is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_8' , 'on')  %}   
offen   
{% else %}
geschlossen
{% endif %}

Aber wenn deins problemlos beim testen funktioniert, kannst du es sicher auch so schreiben.
Speicher die configuration.yaml und schau dann unter Entwicklerwerkzeuge - Yaml - Konfiguration testen.

OK, du setzt das praktisch auch nicht in Hochzeichen?
:thinking:.
Ist dann „>“ ein Zeichen für Zeilenumbruch in yaml bzw. Das der Befehl in der nächsten Zeile steht?

Habe gerade mal eine test.yaml angelegt und den Code eingebettet.

State: >-
 „CODE„

Oder

state: 
   „code“

Oder

state: >-
  CODE
  

Egal was ich eingebe, wird immer grün angezeigt

Ich kann dir leider nicht sagen wofür > bzw. >- steht. Wenn unter Konfiguration testen kein Fehler angezeigt wird, dann speichere es und schau dir deinen Sensor an.
Vor der Änderung immer ein Full Backup machen.

OK, ich mache das mal :pleading_face::cold_sweat:

Teste es zuerst mit state: >

:partying_face: Die Erkenntnis war mal nicht schlecht.

Bei Code-Eingabe in configuration.yaml, egal ob mit „>“ od. „>-„ entsprechend den Code in Anführungszeichen oder nicht war dem völlig egal.

ABER:

Der Zustand unter Entwicklerwerkzeuge war immer „off“.
Folglich, ein binary_sensor kennt nur zwei Zustände, also habe ich das ganze nur als Template Sensor eingetragen.
Auch hier habe ich alle Varianten im state ausprobiert, egal welche, haben alle funktioniert.
Bin mir aber sicher, irgend eine Funktion muss schon dahinter stecken, nur welche.

Für mein kleines Projekt ist es scheinbar nicht relevant.
Von daher, vielen lieben Dank für deine Unterstützung @sirector

Problem ist damit gelöst
Cu Alex

Was ich gelernt habe:
‘>’ bedeutet wohl: Zeilenumbruch ignorieren
‘|’ bedeutet wohl: Zeilenumbruch berücksichtigen.
‘-’ keine Ahnung

Danke dir :+1:t2:. Mit Sicherheit werde ich diese Info irgend wann mal brauchen.

https://learnxinyminutes.com/docs/yaml/

# Multiple-line strings can be written either as a 'literal block' (using |), 
# or a 'folded block' (using '>').
# Literal block turn every newline within the string into a literal newline (\n).
# Folded block removes newlines within the string.
literal_block: |
  This entire block of text will be the value of the 'literal_block' key,
  with line breaks being preserved.

  The literal continues until de-dented, and the leading indentation is
  stripped.

      Any lines that are 'more-indented' keep the rest of their indentation -
      these lines will be indented by 4 spaces.
folded_style: >
  This entire block of text will be the value of 'folded_style', but this
  time, all newlines will be replaced with a single space.

  Blank lines, like above, are converted to a newline character.

      'More-indented' lines keep their newlines, too -
      this text will appear over two lines.

# |- and >- removes the trailing blank lines (also called literal/block "strip")
literal_strip: |-
  This entire block of text will be the value of the 'literal_block' key,
  with trailing blank line being stripped.
block_strip: >-
  This entire block of text will be the value of 'folded_style', but this
  time, all newlines will be replaced with a single space and 
  trailing blank line being stripped.

# |+ and >+ keeps trailing blank lines (also called literal/block "keep")
literal_keep: |+
  This entire block of text will be the value of the 'literal_block' key,
  with trailing blank line being kept.

block_keep: >+
  This entire block of text will be the value of 'folded_style', but this
  time, all newlines will be replaced with a single space and 
  trailing blank line being kept.

####################

Top :+1:t2:. Da werde ich mir mal die URL etwas genauer anschauen.