Automation - Try until conditions are met

Can someone help me wrap my mind around how to change this automation, that closes the garage gate, after conditions are met, to an automation that repetes itself until those conditions are met?

alias: Garage Auto Close
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.car_status #Ultrasonic sensor that detects that car has arrived inside the garage
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition:
  - condition: state
    entity_id: person.mar #ping the phone - at home
    state: home
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - condition: state
    entity_id: binary_sensor.openclose_14 #the garage is open - as a condition
    state: "on"
  - condition: state
    entity_id: sensor.ble_phone_mar #ESPresence - the phone is inside the house
    state: bedroom
action:
  - service: switch.turn_on #switch that closes the garage
    data: {}
    target:
      entity_id: switch.garage
  - service: tts.google_translate_say
    data:
      entity_id: media_player.bedroom_speaker
      message: The garage is closing.
mode: single

Would the change of the state, from the ultrasonic sensor, from off to on, make impossible to create the “repeat until conditions are met”?

I prefer to use timers instead of wait conditions.
For my front door lock, when the lock command is sent a timer starts for 15 seconds. Once that timer finishes, HA checks if the door is locked. If it is, the automation finishes. If it is not locked, as can happen when the door wasn’t closed all the way, it send a tts announcement over the home speakers. It also restarts that timer for 5 minutes. When the timer finishes, it starts that check process again restarting the timer again. It will run indefinitely untill the door is locked properly.
Mu autolock/check automation

alias: Autolock Front door
description: ""
trigger:
  - platform: device
    device_id: 20054c5b37ed57a11e56e06e901f802c
    domain: lock
    entity_id: lock.new_front_door_lock
    type: unlocked
    id: Start Main Timer
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.front_door_lock_timer
    id: Timer finished
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.lock_test_timer
    id: Test Timer finished
  - platform: state
    entity_id:
      - binary_sensor.front_door_sensor
    to: "off"
    id: Start Main Timer
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Start Main Timer
        sequence:
          - choose:
              - conditions:
                  - condition: numeric_state
                    entity_id: sun.sun
                    attribute: elevation
                    above: 0
                sequence:
                  - service: timer.start
                    data:
                      duration: "900"
                    target:
                      entity_id: timer.front_door_lock_timer
              - conditions:
                  - condition: numeric_state
                    entity_id: sun.sun
                    below: 0
                    attribute: elevation
                sequence:
                  - service: timer.start
                    data:
                      duration: "300"
                    target:
                      entity_id: timer.front_door_lock_timer
      - conditions:
          - condition: trigger
            id: Timer finished
          - condition: state
            entity_id: binary_sensor.front_door_sensor
            state: "off"
        sequence:
          - service: lock.lock
            data: {}
            target:
              entity_id: lock.new_front_door_lock
          - service: timer.start
            data:
              duration: "15"
            target:
              entity_id: timer.lock_test_timer
      - conditions:
          - condition: trigger
            id: Test Timer finished
          - condition: state
            entity_id: lock.new_front_door_lock
            state: unlocked
        sequence:
          - service: script.Advanced_TTS_2
            data:
              who: media_player.tts_announcements
              message: |
           
                 The front door lock has malfunctioned. Please make sure the door is closed all the way and relock the door.
          - service: timer.start
            data:
              duration: "300"
            target:
              entity_id: timer.lock_test_timer
mode: single

In case it helps someone in the future, I ended up with an automation (thanks to the ideas of some people on HA’s Discord) that’s working perfectly:

alias: Garage Auto Closes
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.car_status #ultrasonic sensor - car inside the garage
      - binary_sensor.ble_phone_presence #Espresence - bedroom
    to: "on" #A mixture of triggers, almost working as conditions
  - platform: state
    entity_id: device_tracker.mar_phone_ping #ping the phone - at home
    to: home 
condition:
  - condition: state
    entity_id:
      - binary_sensor.openclose_14 #the garage gate is opened - sensor
      - binary_sensor.ble_phone_presence ##The solution was combining the triggers and conditions - Espresence in bedroom
    state: "on" 
  - condition: state
    entity_id: device_tracker.mar_phone_ping #ping the phone - at home
    state: home
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.garage #remote RF command that closes/opens garage
  - service: tts.google_translate_say
    data:
      message: The Garage is closing.
      entity_id: media_player.living_room_kitchen_bedroom #Final announcement - just to be sure
mode: single

For instance, yesterday, while I came into the house my kid was still in the garage. Only when he came inside the house I put the phone in the bedroom - where the Espresence (m5atom) is connected - and only then did the garage started closing. :ok_hand:

1 Like