How to "Delay until...loop" in a automation

I’m writing an automation for a robot vacuum which after performing mopping, waits at a location for the mop to be removed. After arriving at location, I send a notify message via Alexa and then want to perform a loop reminding to remove the mop every 20 mins if it hasn’t been done yet.

The below code partly works, however it doesn’t immediately exit the delay/loop after removing the mop, instead, it waits for the remainder of the 20min delay; I want to continue with the next part of the automation (send her back to the dock) as soon as the mop sensor changes, any ideas appreciated!?

repeat:
  while:
    - condition: state
      entity_id: binary_sensor.sandy_mop_attached
      state: 'on'
      for:
        hours: 0
        minutes: 20
        seconds: 0
  sequence:
    - service: notify.alexa_media_everywhere
      data:
        data:
          type: announce
          method: speak
        message: Sandy is still waiting near the sink for the mop pad to be removed.
    - delay:
        minutes: 20
  - service: vacuum.return_to_base
    data: {}
    target:
      entity_id: vacuum.sandy

Maybe a wait template could be the solution?
Untested, but this will wait max. 20 minutes for binary_sensor to turn off, but if turned off it will pass immediatly.

repeat:
  while:
    - condition: state
      entity_id: binary_sensor.sandy_mop_attached
      state: 'on'
      for:
        hours: 0
        minutes: 20
        seconds: 0
  sequence:
    - service: notify.alexa_media_everywhere
      data:
        data:
          type: announce
          method: speak
        message: Sandy is still waiting near the sink for the mop pad to be removed.
  - wait_template: "{{ is_state('binary_sensor.sandy_mop_attached', 'off') }}"
    timeout: "00:20:00"
    continue_on_timeout: true
  - service: vacuum.return_to_base
    data: {}
    target:
      entity_id: vacuum.sandy

@madface Perfect, thank you! :slight_smile:

Edit: I spoke too soon, this works for two iterations (iteration 1 = when 1st run Alexa speaks, 2 is after the delay and she speaks, then it stops looping :frowning: - I tried changing the while: to until: (and state: off) but it still only does two iterations then stops?)

Edit2: Found a solution by adding a 10 second for: on the condition. The below now tests okay and working as intended :slight_smile: Thanks for your help @madface

repeat:
  until:
    - condition: state
      entity_id: binary_sensor.sandy_mop_attached
      state: 'off'
      for:
        seconds: 10
  sequence:
    - service: notify.alexa_media_everywhere
      data:
        data:
          type: announce
          method: speak
        message: >-
          Sandy has finished mopping the kitchen, she is now waiting near the
          sink for the mop pad to be removed. Please remove the pad and she will
          return home.
    - wait_template: '{{ states(''binary_sensor.sandy_mop_attached'') == "off" }}'
      timeout: '00:20:00'
      continue_on_timeout: true