Delay trigger used for rain sensor

I am new to HA and doing my first automations. Delay’s are not fully understood and after reading a lot in the forum, I’m even more confused…

Here my problem:
I have a Pergola roof with louvers that shall be closed when rain starts (=rain sensor is true). My automation works fine so far. Whenever the rain sensor detects no more rain, the louvers open immediately. Now I want to delay the opening until 15min have passed without rain. I’ve added a delay into the opening automation (not shown in this post), however it didn’t seem work - the louvers apparently opened without the programmed delay - however when it started to rain again quickly afterwards, they didn’t close anymore.

Now I will try with the automation below: I added a delay in the closing sequence, e.g. after closing, there should be a 15min delay when there is no rain. In case it starts to rain again during this waiting period, the timer will be started again.

Does this work - can anybody give me a quick feedback?


alias: Rain detected - Close Pergola
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.shellyplusi4_3ce90e30d8c0_input_0_input
    from: "off"
    to: "on"
    alias: When Rain sensor is ON
condition: []
action:
  - service: scene.turn_on
    target:
      entity_id: scene.close_pergola
    metadata: {}
  - device_id: 311c62d2715cc03081d4c3e581c69b81
    domain: mobile_app
    type: notify
    message: Rain detected - Closing Pergola
  - delay:
      hours: 0
      minutes: 15
      seconds: 0
      milliseconds: 0
mode:  restart

Specifically this part of the example. Put your 15 minutes here and get rid of the delay. You can add the same thing from the user interface.

# If given, will trigger when the condition has been true for X time; you can also use days and milliseconds.
      for:
        hours: 0
        minutes: 1
        seconds: 0

Thanks a lot Ben, very helpful - changed my code accordingly. Will check whether it works !