A repeat loop that waits for something to become true has one ugly failure mode: the thing never becomes true. The docs have a one-line fix for it, and I would add it to every loop like this.
A repeat with until runs its actions, then checks the conditions, and repeats until they are met. The actions always run at least once, because the check comes after each round. For a loop that retries up to five times, put the conditions in an Or condition:
actions:
- repeat:
sequence:
- action: notify.notify
data:
message: "Where are you?"
- delay:
minutes: 5
until:
- condition: or
conditions:
- condition: template
value_template: "{{ is_state('device_tracker.iphone', 'home') }}"
- condition: template
value_template: "{{ repeat.index >= 5 }}"
The docs say repeat.index holds the number of the round, starting at 1. The state check is the example from the docs. The Or part and the index check are the way the docs suggest to end a repeat after a number of rounds as well. There is also repeat.first, which is true during the first round.
The caveat is the always-once behaviour. Even if the device is already home, the sequence runs one time before the condition is looked at. If that is not what you want, put a condition or an if in front of the loop. The docs do not mention a built-in maximum number of rounds, so the limit is yours to write.
Further reading: Repeat until, Home Assistant docs