Automation for window, depending light is on or off

Hi,

I have a automatic window in my bathroom. When the light goes on, I want the window to open for 15 minutes and then close. When the light goes off, the window must close immediately.

I’ve made two automations (with mode restart) for each purpose but the problem is that one automation is still running while the other is started. So, if the light goes out, the window will close. The other automation want to close the window as well, 15.minutes after starting.

I tried to combine both automations in one but I can’t manage to get this done.

Anyone an idea to solve this?

By the way. Don’t mind the logic of the automation. It’s an example which I can use in an other sitiation.

I assume you use loops of some kind?
Don’t. Only use events.

Hi,

Can you explain rhat. I made an automation in the visual editor. Here’s one of them.

alias: Dakluik open na douchen
description: ""
trigger:
  - platform: device
    type: turned_off
    device_id: 514ab9068757e04c06eac53ec2e04ea2
    entity_id: switch.sonoff_1001661da3
    domain: switch
condition: []
action:
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: scene.turn_on
    target:
      entity_id: scene.dakluik_open
    metadata: {}
  - delay:
      hours: 0
      minutes: 15
      seconds: 0
      milliseconds: 0
  - service: scene.turn_on
    target:
      entity_id: scene.dakluik_dicht
    metadata: {}
mode: restart

rather than that using delays, which keeps the automation running in a loop, do something like

  trigger:
  - platform: state
    id: switch_off
    entity_id:
    - switch.sonoff_1001661da3
    to: 'off'
    for:
      minutes: 5
  - platform: state
    id: switch_on
    entity_id:
    - switch.sonoff_1001661da3
    to: 'on'
    for:
      minutes: 15
  action:
  - choose:
    - conditions:
        condition: trigger
        id: switch_off
      sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.dakluik_open
    - conditions:
        condition: trigger
        id: switch_on
      sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.dakluik_dicht

I made it into a single automation, because that’s what people want, but it’s far less cumbersome in 2 automations :wink:

Thanks, I will look into it!