Two window sensors one output | Zwei Fenstersensoren eine Ausgabe

Hi there,

I have installed two Aqara window sensors in each of my windows.
Example kitchen:

Sensor 1 for position tilted:
binary_sensor.lumi_lumi_sensor_magnet_aq2_b460d606_on_off
Sensor 2 for position open:
binary_sensor.lumi_lumi_sensor_magnet_aq2_b095e406_on_off
It is my goal that in Lovelace only one line appears for each pair of senors, according to the following scheme:

  • Sensor 1 and 2 is closed = output: “Kitchen window closed”
  • Sensor 1 is open and sensor 2 is closed = output: “Kitchen window tilted”
  • Sensor 2 is open (sensor 1 doesn’t matter) = output: “Kitchen window open”
  • If one of the sensors is not available = output: “Kitchen window not available”

Can you do that?

As always, I am grateful for any help.

Greetings Werner

Hallo,

ich habe in meine Fenster je zwei Aqara Fenstersensoren eingebaut.
Beispiel Küche:

Sensor 1 für Stellung gekippt:

binary_sensor.lumi_lumi_sensor_magnet_aq2_b460d606_on_off

Sensor 2 für Stellung geöffnet:

binary_sensor.lumi_lumi_sensor_magnet_aq2_b095e406_on_off

Mein Ziel ist es, dass in Lovelace je Senor-Paar nur eine Zeile erscheint und zwar nach folgendem Schema:

  • Sensor 1 und 2 ist geschlossen = Ausgabe: “Fenster Küche geschlossen”
  • Sensor 1 ist offen und Sensor 2 ist geschlossen = Ausgabe: “Fenster Küche gekippt”
  • Sensor 2 ist geöffnet (Sensor 1 egal) = Ausgabe: “Fenster Küche offen”
  • Wenn einer der Senoren nicht verfügbar ist = Ausgabe: “Fenster Küche nicht verfügbar”

Kann man das realisieren?

Bin wie immer für jede Hilfe dankbar.

Gruß Werner

Do I understands you right and you are using 2 sensors for 1 window?
Tilted = „gekippt“. Sorry for that, my English is not the best.

Hi there,

Yes. One reacts when tilted, the other when opened. Whereby sensors 1+2 actually react when open.

Greetings Werner

Anyway, have a look at the generic lovelace cards „Bedingte Elemente“ („conditional „) and „Entität Filter („ entity-filter“).

Hi there,

I already have that, but I can’t get any further. Unfortunately, I am also a beginner.

Greetings Werner

Window closed:


type: conditional
conditions:
  - entity: binary_sensor.1
    state: 'off'
  - entity: binary_sensor.2
    state: 'off'
card:
  type: glance
  entities:
    - entity: binary_sensor.1
      name: Fenster geschlossen
      icon: mdi:window-closed-variant
  state_color: true
  show_name: true
  show_state: false


Window tilted:


type: conditional
conditions:
  - entity: binary_sensor.1
    state: 'on'
  - entity: binary_sensor.2
    state: 'off'
card:
  type: glance
  entities:
    - entity: binary_sensor.1
      name: Fenster gekippt
      icon: mdi:exclamation
  state_color: true
  show_name: true
  show_state: false


Window opened:


type: conditional
conditions:
  - entity: binary_sensor.1
    state: 'on'
  - entity: binary_sensor.2
    state: 'on'
card:
  type: glance
  entities:
    - entity: binary_sensor.1
      name: Fenster offen
      icon: mdi:window-open-variant
  state_color: true
  show_name: true
  show_state: false


unavailable No. 1:


type: conditional
conditions:
  - entity: binary_sensor.1
    state: 'unavailable'

card:
  type: glance
  entities:
    - entity: binary_sensor.1
      name: Sensor nicht erreichbar
      icon: mdi:exclamation
  state_color: true
  show_name: true
  show_state: false


unavailable No. 2:


type: conditional
conditions:
  - entity: binary_sensor.2
    state: 'unavailable'

card:
  type: glance
  entities:
    - entity: binary_sensor.2
      name: Sensor nicht erreichbar
      icon: mdi:exclamation
  state_color: true
  show_name: true
  show_state: false


1 Like

Sprechen Sie deutsch?

Dankeschön. Aber wo muss ich das hin kopieren?
Thanks very much. But where do I have to copy this to?

Add a new card to your frontend and choose „Manuell“, delete the displayed „type: ''“, copy the particular code above.

Yes, I’m native German speaker.

Ach super !
Wie kann ich denn die Zustände kombinieren - also in einer Zeile sollte dann stehen.
Je nach Zustand

  • Fenster Küche offen
  • Fenster Küche gekippt
  • Fenster Küche geschlossen
  • Fenster Küche nicht erreichbar

Ich versthe gar nicht wie man diese Programmierung begreifen kann. Haben Sie sowas gelernt?

Gruß Werner

PS: Habe oben meine Anfrage nochmal auf Deutsch einkopiert…

Please note, that the official forum language is English so others can follow the conversation :slightly_smiling_face:

The conditional card only shows up if the condition is met.
Or do you want to see all 4 sensor states all the time?

1 Like

Hallo,

ich habe es mal mit diesem Code getestet:

type: conditional
conditions:
  - entity: binary_sensor.lumi_lumi_sensor_magnet_aq2_8147f906_on_off
    state: 'on'
  - entity: binary_sensor.lumi_lumi_sensor_magnet_aq2_19330507_on_off
    state: 'on'
card:
  type: glance
  entities:
    - entity: binary_sensor.lumi_lumi_sensor_magnet_aq2_8147f906_on_off
      name: Arbeitszimmer Fenster offen
      icon: mdi:window-open-variant
  state_color: true
  show_name: true
  show_state: false

Die Karte zeit bei einem offenem Fenster das Symbol gelb an und bei einem geschlossenen Fenster, das Symbol in blau. Verschwinden tut die Karte aber nicht.

Insgesamt würde diese Variante sehr aufwendig sein, für jedes Fenster 5 Karten.
Würde es auch pro Fenster mit einem Code möglich sein?
Vorteile: ich könnte mir auch einer Seite alle Fenstertzustände anzeigen lassen und bei den bedingten nur die offenen, gekippten und nicht erreichbaren Fenster. Auch würde mir je Fenster eine Zeile genügen, das große Symbol nimmt doch ganz schön Platz weg.

Gruß Werner

Hi there,

I tested it with this code:

type: conditional
conditions:
  - entity: binary_sensor.lumi_lumi_sensor_magnet_aq2_8147f906_on_off
    state: 'on'
  - entity: binary_sensor.lumi_lumi_sensor_magnet_aq2_19330507_on_off
    state: 'on'
card:
  type: glance
  entities:
    - entity: binary_sensor.lumi_lumi_sensor_magnet_aq2_8147f906_on_off
      name: Arbeitszimmer Fenster offen
      icon: mdi:window-open-variant
  state_color: true
  show_name: true
  show_state: false

When the window is open, the card displays the symbol in yellow and when the window is closed, the symbol in blue. But the card doesn’t go away.

Overall, this variant would be very complex, 5 cards for each window.

Would it also be possible with one code per window?

Advantages: I could also display all window states on one side and only open, tilted and inaccessible windows for the conditional ones. One line per window would also suffice for me, the large symbol takes up a lot of space.

Greetings Werner

That’s strange, sure, that you closed the editing modus? When I simulate the card with my binary sensors I get:

(editing modus)

(frontend)

If you don’t want a pure frontend solution by using conditional card, you can create a template sensor.

Try this template sensor:


template:
  - sensor:
      - unique_id: fensterstatus_kueche
      - name: Status Küchenfenster
        state: |
          {% set b1 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_b460d606_on_off') %}
          {% set b2 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_b095e406_on_off') %}
          {% if b1 == 'off' and b2 == 'off' %} Fenster Küche geschlossen
          {% elif b1 == 'on' and b2 == 'off' %} Fenster Küche gekippt
          {% elif b2 == 'on' %} Fenster Küche offen
          {% elif b1 == 'unavailable' or b2 == 'unavailable' %} Fenster Küche nicht verfügbar
          {% else %} ?
          {% endif %}
        icon: |

          {% set b1 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_b460d606_on_off') %}
          {% set b2 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_b095e406_on_off') %}
          {% if b1 == 'off' and b2 == 'off' %} mdi:window-closed-variant
          {% elif b1 == 'on' and b2 == 'off' %} mdi:angle-acute
          {% elif b2 == 'on' %} mdi:window-open-variant
          {% elif b1 == 'unavailable' or b2 == 'unavailable' %} mdi:exclamation
          {% else %} ?
          {% endif %}



2 Likes

Hallo,

vielen lieben Dank für die Mühe und sorry für die späte Antwort.

Leider weiß ich nicht, wo ich diesen Code einfügen muss. Habe es schon in verschiedenen Yaml-Datein versucht, konnte aber keinen Erfolg verzeichen.

Bitte um Hilfe.

LG Werner

Hi there,

Thank you very much for the trouble and sorry for the late reply.

Unfortunately, I don’t know where to paste this code. Have tried various Yaml files but found no success.

Please help.

Werner

All good. You can copy it in your configuration.yaml.

Hallo,

wenn ich das einfüge bekomme ich nach einem Neustart folgende Fehlermeldungen:

 Logger: homeassistant.components.sensor
Source: helpers/entity_platform.py:692
Integration: Sensor (documentation, issues)
First occurred: 16:16:16 (3 occurrences)
Last logged: 17:10:22
Updating octoprint sensor took longer than the scheduled update interval 0:00:30


 Logger: homeassistant.config
Source: config.py:464
First occurred: 16:11:17 (8 occurrences)
Last logged: 17:10:11

    Invalid config for [template]: required key not provided @ data['sensor'][0]['state']. Got None. (See /config/configuration.yaml, line 139).
    Invalid config for [automation]: [template] is an invalid option for [automation]. Check: automation->template. (See /config/configuration.yaml, line 12).
    Invalid config for [template]: required key not provided @ data['sensor'][0]['state']. Got None. (See /config/configuration.yaml, line 138).

Gruß Werner

Hi there,

when I insert this I get the following error messages after a restart:

 Logger: homeassistant.components.sensor
Source: helpers/entity_platform.py:692
Integration: Sensor (documentation, issues)
First occurred: 16:16:16 (3 occurrences)
Last logged: 17:10:22
Updating octoprint sensor took longer than the scheduled update interval 0:00:30


 Logger: homeassistant.config
Source: config.py:464
First occurred: 16:11:17 (8 occurrences)
Last logged: 17:10:11

    Invalid config for [template]: required key not provided @ data['sensor'][0]['state']. Got None. (See /config/configuration.yaml, line 139).
    Invalid config for [automation]: [template] is an invalid option for [automation]. Check: automation->template. (See /config/configuration.yaml, line 12).
    Invalid config for [template]: required key not provided @ data['sensor'][0]['state']. Got None. (See /config/configuration.yaml, line 138).


Greetings Werner

Would you please so kind and post your configuration.yaml?

Hallo,
gern, ich hoffe ich habe alle Zugangsdaten entfernt.

Gruß Werner

Hi there,
gladly, I hope I have removed all access data.

Greetings Werner

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

frontend:
  themes: !include_dir_merge_named themes
  
# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

# Test Garagentor Cover
cover:
  - platform: template
    covers:
      garage_door:
        friendly_name: "Garagentor mit Pfeilen"
        device_class: garage
        value_template: "{{ is_state('binary_sensor.tk04_sonoff_turkontakt_ias_zone', 'on') }}"
        open_cover:
          service: light.toggle
          entity_id: light.ewelink_zb_sw01_1e1c6b22_on_off
        close_cover:
          service: light.toggle
          entity_id: light.ewelink_zb_sw01_1e1c6b22_on_off
        stop_cover:
          service: light.toggle
          entity_id: light.ewelink_zb_sw01_1e1c6b22_on_off

camera:
   - platform: onvif
     host: 
     port: 
     username: 
     password: 
     extra_arguments: -rtsp_flags none -rtsp_transport udp -q:v 2
     name: Rundum
     
alarm_control_panel:
  - platform: manual
    name: 
    code:
    arming_time: 10
    delay_time: 20

# Gruppe Hauptlich Küche
light:
  - platform: group
    name: Hauptlicht Küche
    entities:
      - light.lc10_paulmann_level_light_color_on_off
      - light.lc11_paulmann_level_light_color_on_off
      - light.lc12_paulmann_level_light_color_on_off

# Gruppe Ambientlicht Küche
  - platform: group
    name: Ambientlicht Küche
    entities:
      - light.iluminize_hk_zd_rgbcct_a_5878befe_level_light_color_on_off
      - light.iluminize_hk_zd_rgbcct_a_44ada7fe_level_light_color_on_off
      
# Telegram Bot anbindung
telegram_bot:
  - platform: polling
    api_key: 
    allowed_chat_ids:
      - 
      
notify:
  - name: HA_Message
    platform: telegram
    chat_id: 
    

# Datum und Uhrzeit    
sensor:
  - platform: template
    sensors:
      datum_zeit_time:
        friendly_name: ""
        value_template: >
          {% set days = ['Mo.', 'Di.', 'Mi.', 'Do.', 'Endl.Fr.', 'Sa.', 'So.'] %}
          {{ days[now().weekday()] }}
          {{ now().strftime('%d.%m. - %H:%M') }}  Uhr

# Einbindung Octoprint
octoprint:
  host: 
  api_key: 
  
  
# Entitäten verbinden Gargentor (Test)
switch:
  - platform: template
    switches:
      garage_door:
        friendly_name: "Garagentorschalter"
        value_template: "{{ is_state('binary_sensor.tk04_sonoff_turkontakt_ias_zone', 'on') }}"
        turn_on:
          service: light.toggle
          entity_id: light.ewelink_zb_sw01_1e1c6b22_on_off
        turn_off:
          service: light.toggle
          entity_id: light.ewelink_zb_sw01_1e1c6b22_on_off
        icon_template: >-
          {{ 'mdi:garage' if is_state('binary_sensor.tk04_sonoff_turkontakt_ias_zone','off') else 'mdi:garage-open' }}

# Lüfter SZ-KG EIN AUS 
  - platform: command_line
    switches:
      luefterszkg:
        command_on: "/usr/bin/curl -X GET http://192.168.178.72/?power=on"
        command_off: "/usr/bin/curl -X GET http://192.168.178.72/?power=off"
        value_template: '{{ value == "1" }}'
        friendly_name: Lüfter SZ-KG
      luefterwzdg:
        command_on: "/usr/bin/curl -X GET http://192.168.178.39/?power=on"
        command_off: "/usr/bin/curl -X GET http://192.168.178.39/?power=off"
        value_template: '{{ value == "1" }}'
        friendly_name: Lüfter WZ-DG
      luefterszmoritz:
        command_on: "/usr/bin/curl -X GET http://192.168.178.79/?power=on"
        command_off: "/usr/bin/curl -X GET http://192.168.178.79/?power=off"
        value_template: '{{ value == "1" }}'
        friendly_name: Lüfter SZ-Moritz
      lueftermoritz:
        command_on: "/usr/bin/curl -X GET http://192.168.178.71/?power=on"
        command_off: "/usr/bin/curl -X GET http://192.168.178.71/?power=off"
        value_template: '{{ value == "1" }}'
        friendly_name: Lüfter Moritz


template:
  - sensor:
      - unique_id: fensterstatus_kueche
      - name: Status Küchenfenster
        state: |
          {% set b1 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_b460d606_on_off') %}
          {% set b2 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_b095e406_on_off') %}
          {% if b1 == 'off' and b2 == 'off' %} Fenster Küche geschlossen
          {% elif b1 == 'on' and b2 == 'off' %} Fenster Küche gekippt
          {% elif b2 == 'on' %} Fenster Küche offen
          {% elif b1 == 'unavailable' or b2 == 'unavailable' %} Fenster Küche nicht verfügbar
          {% else %} ?
          {% endif %}
        icon: |

          {% set b1 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_b460d606_on_off') %}
          {% set b2 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_b095e406_on_off') %}
          {% if b1 == 'off' and b2 == 'off' %} mdi:window-closed-variant
          {% elif b1 == 'on' and b2 == 'off' %} mdi:angle-acute
          {% elif b2 == 'on' %} mdi:window-open-variant
          {% elif b1 == 'unavailable' or b2 == 'unavailable' %} mdi:exclamation
          {% else %} ?
          {% endif %}


Seems that your legacy template sensor and my example („modern“ method) clash.

This is my example built in legacy design:


      fensterstatus_kueche:
        friendly_name: Status Küchenfenster
        value_template: |
          {% set b1 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_b460d606_on_off') %}
          {% set b2 = states('binary_sensor. lumi_lumi_sensor_magnet_aq2_b095e406_on_off') %}
          {% if b1 == 'off' and b2 == 'off' %} Fenster Küche geschlossen
          {% elif b1 == 'on' and b2 == 'off' %} Fenster Küche gekippt
          {% elif b2 == 'on' %} Fenster Küche offen
          {% elif b1 == 'unavailable' or b2 == 'unavailable' %} Fenster Küche nicht verfügbar
          {% else %} ?
          {% endif %}
        icon_template: |
          {% set b1 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_b460d606_on_off') %}
          {% set b2 = states('binary_sensor. lumi_lumi_sensor_magnet_aq2_b095e406_on_off') %}
          {% if b1 == 'off' and b2 == 'off' %} mdi:window-closed-variant
          {% elif b1 == 'on' and b2 == 'off' %} mdi:angle-acute
          {% elif b2 == 'on' %} mdi:window-open-variant
          {% elif b1 == 'unavailable' or b2 == 'unavailable' %} mdi:exclamation
          {% else %} ?
          {% endif %}


Copy it under the last line of your sensor datum_zeit_time.

2 Likes