I think your current solution is still flawed. E.g., what if person
changes to “not_home” (say, from a zone name) within 3 minutes after the door is opened, and then changes to “home” 11+ seconds later (but still within 3 minutes of the door opening)?
The other problem is that you have restart mode for the script, but single mode (which is the default) for the automation. Which means, in effect, the sequence will not be restarted if the door opens again while the script is still running, because the automation won’t be triggered again to call it.
BTW, there’s now no point in having the actions in a separate script. At the very least I’d suggest moving them back to the automation and changing the mode of the automation to restart.
I think the solution I suggested does exactly what you want. I’d be interested to see if it works for you.
See Waiting for Scripts to Complete for the answer.
To handle multiple people, I think it might be easier to reverse the logic. I.e., trigger on a person changing to home, and then check if the door changed within the previous 3 minutes (shouldn’t matter if it opened or closed, just that it changed), or if it changes within the next 3 minutes (which actually might be easier with a new feature I’m working on – wait_for_trigger
, lol!)
Now bear with me here. In this case the automation should be parallel mode, since it needs to do the same, potentially lengthy thing, for multiple person
entities. However, what it does for each needs to run in restart mode, just in case the person
entity changes from something to “home” to something else and then back to “home” again quickly. So it makes sense to put the actual actions into a separate script so it and the automation can have different modes. BUT, and this is the “tricky” part, if the same script was used for each person, then it would restart when a second person changed to home while it was still running from a first person changing to home. SOOOO…, that means we actually need multiple scripts. One that does the grunt work, which uses parallel mode so it can do the work for multiple person
entities. But we need an intermediary between the parallel automation and the parallel script to do the restart function for each person.
Ok, if you’re not lost yet, here’s what I’d suggest:
Automation:
- trigger:
- platform: state
entity_id:
- person.PERSON1
- person.PERSON2
...
to: home
mode: parallel
action:
- service_template: "script.{{ trigger.to_state.object_id }}_arrived_home"
Person script (duplicate for each person
entity):
PERSON1_arrived_home:
mode: restart
sequence:
- service: script.person_arrived_home
data:
person: PERSON1
Script that does the real work:
person_arrived_home:
mode: parallel
sequence:
- choose:
# Did door open or close within the last 3 min?
- conditions:
- condition: template
value_template: >
{{ (now() - state.binary_sensor.garage_entry.last_changed)
.total_seconds() > 180 }}
# No. Wait up to 3 min for door to open. (We'll assume it's still closed.)
sequence:
- wait_template: "{{ is_state('binary_sensor.garage_entry', 'on') }}"
timeout: "00:03"
continue_on_timeout: false
# Either door opened/closed within the last 3 min,
# or it did within 3 min of person changing to home.
- data_template:
message: >
{{ state_attr('person.' ~ person, 'friendly_name') }} arrived home.
{{ ('Sup bro', 'Welcome home', 'Hey dude hows it goin', 'Whats shakin bacon',
'Whats crackalackin', 'whats-up buttercup')|random }}
entity_id: media_player.kitchen_display
service: tts.google_cloud_say