Switch off the fan when the window is open or tilted

Hi there,

I have decentralized ventilation devices in the house, these can be switched on or off by calling a URL.
The integration looks like this:

  - platform: command_line
    switches:
      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

I also attached two window sensors to each window. This enables the to recognize the status between, closed, opened or tilted (Germany: geschlossen, geöffnet or gekippt).
I integrated these as follows:

      fensterstatus_schlafzimmer_dg:
        friendly_name: Fenster Schlafzimmer-DG
        value_template: |
          {% set b1 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_007cf906_on_off') %}
          {% set b2 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_698aff06_on_off') %}
          {% if b1 == 'off' and b2 == 'off' %} geschlossen
          {% elif b1 == 'on' and b2 == 'off' %} gekippt
          {% elif b2 == 'on' %} offen
          {% elif b1 == 'unavailable' or b2 == 'unavailable' %} nicht verfügbar
          {% else %} ?
          {% endif %}
        icon_template: |
          {% set b1 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_007cf906_on_off') %}
          {% set b2 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_698aff06_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 %}

Now I want to achieve the following.
If the window is open or tilted for more than a minute, the fan should be switched off.
When the window is closed again for at least 5 minutes, the fan should switch on again.

Since I am a beginner, I would be very happy to receive help.

Greetings Werner

When the window is open, does b1 sensor state is ON? If yes, then just use it as a state trigger-

trigger:
  - platform: state
    for: '00:01'
    to: 'on'
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_007cf906_on_off
condition: []
action:
  - service: switch.turn_off
    target:
      entity_id: switch.luefterwzdg
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_007cf906_on_off
        to: 'off'
        for: '00:05:00'
    continue_on_timeout: false
    timeout: '600'
  - service: switch.turn_on
    target:
      entity_id: switch.luefterwzdg
2 Likes

Hi there,

once again thank you very much.
Yes b1 is also “on” when the window is open. You are of course right, then all you have to do is use this sensor.
Thanks for the great solution!
It will work that way.
Another question about the timeout:
What is this used for? In the visual automation I can activate “Continue on timeout” - what can I achieve with it.

Greetings Werner

Hi there,

I found out so that the fan can be switched on again, even if the window was not closed.

Many thanks.

Greetings Werner

Gruß Werner

Please check the automation trace. As you can see, the continue_on_timeout is set to false. It means the wait_for_trigger has a timeout of 10 minutes and if the trigger is not present, it will not continue to turn the switch ON.

1 Like

Yes, I have already activated that and set the time to 6000. Thanks.

Greetings Werner