I guess the part I’d need to see, using @rhumbertgz 's example…
The automation Trigger would be MotionSensorA turns “on”. The first step of the automation would be a wait_template for DoorContactB to turn “on”. So if MotionSensorA turns “on”, and then turns “off” again before DoorContactB turns “on”… what happens once DoorContactB turns on?
My impression, based on what I’ve seen happen in other automations, is that the automation was triggered and therefore still running. So, when DoorContactB turns “on”, even though MotionSensorA is no longer “on”, the script will continue to run. Which, might be what OP desires. But, if that’s not what OP desires, then a “condition” in the action will have to be added after the wait_template to ensure that it’s still on. From my experience, the condition of an automation (not in the action, but on the automation itself) only affects if the automation triggers. Once triggered, it’s still triggered until the “action” portion of the automation either completes or meets a “wait_template” or “condition” that ends the action.
This really makes the automations get long and ugly, when having more than one automation would make it easier to read. Imagine if we add another requirements to OP’s original idea:
If MotionSensorA turns on, THEN DoorContactB turns on, THEN LightC turns on, send a notification.
Then you end up with this (untested, possibly typos):
trigger:
- platform: state
entity_id: MotionSensorA
to: 'on'
action:
- wait_template: "{{ is_state('DoorContactB','on' }}"
- condition: state
entity_id: MotionSensorA
state: 'on'
- wait_template: "{{ is_state('LightC', 'on' }}"
- condition: state
entity_id: MotionSensorA
state: 'on'
- condition: state
entity_id: DoorContactB
state: 'on'
- service: notify.me
message: 'The Thing Happened'
Now add that you only want this to kick in if you’re “home” when it happens, and also only if you’re watching TV, and it gets awful. By, instead, turning on and off related automations, you can ensure that things only happen when other things have happened first.
- alias: motion happened
initial_state: 'on'
trigger:
- platform: state
entity_id: MotionSensorA
to: 'on'
action:
- service: automation.turn_on
entity_id: automation.door_opened
- alias: motion stopped
trigger:
- platform: state
entity_id: MotionSensorA
to: 'off'
action:
- service: automation.turn_off
entity_id:
- automation.door_opened
- automation.light_on
- alias: door opened
initial_state: 'off'
trigger:
- platform: state
entity_id: DoorContactB
to: 'on'
action:
- service: automation.turn_on
entity_id: automation.light_on
- alias: light on
initial_state: 'off'
trigger:
- platform: state
entity_id: LightC
to: 'on'
action:
- service: notify.me
message: "The Thing Happened"
Sure, it’s now 40 lines and 4 automations, compared to 18 lines and one automaton. But, adding something like “and only do this when Someone is Home” becomes easy and requires only 3 lines added to one automation. Additional problems are also solved, like, what if the door was already open when motion was detected? The wait_template version executes anyway, even though OP said “and THEN DoorContactB was opened”. So you’d have to add a condition before the wait_template to check that it was “off” first. But then, what if, while MotionSensorA turned on (while the door was open) and then the door closed, and then reopened (with MotionSensorA staying on the whole time)… the condition before the wait_template would have ended the action already. And this can be worked around by using an event trigger instead of a state trigger for the motion sensor (which most of them support), but, even then, timing becomes an issue (how often does your motion sensor send the event, and did the door close and reopen within that window?).
I’m not saying a “wait_template” is wrong. It serves a purpose. But, I quickly realized, way in the early days of my automation journey, that most of my automations got too complex for the “wait_template” I originally wrote to still apply, that I just stopped using them entirely and always went for multiple automations. Then, when you want to add a condition, or realize you want to wait for the door to be open for at least a minute before progressing, or whatever… the path to get there is simple, even if it’s more verbose.
To make grouping such automations together easier, I have this in my configuration.yaml:
automation: !include_dir_merge_list automations/
So I can make a file called “livingMotion” that has all the automation pieces together to make fine tuning it easier.
But, to be fair, I quickly got tired of the verbosity of multiple automations, as well, and, for anything new, I tend to handle it in AppDaemon if it’s even a little complicated or I have the foresight to know it will end up complicated in the future.