How can automation A stop running automation B?

In my bathroom I have a couple of motion sensors. I want to create one automation that turns on the lights if one of the motion sensors detects motion, and another that turns off the lights a few minutes after both sensors detects no motion. In HomeSeer this worked well to prevent the kids being left in the dark when bathing. But how do I do this in Home Assistant? Can one automation cancel another automation that is currently running and on a Delay step?

I would simply create a group containing both motion sensors and use the group to trigger a single automation. In its simplest form, it would look something like this:

- alias: 'Automatic Bathroom Light'
  mode: single
  trigger:
    - platform: state
      entity_id: group.bathroom_motion
      from: 'off'
      to: 'on'
    - platform: state
      entity_id: group.bathroom_motion
      to: 'off'
      for: '00:02:00'
  action:
    - service: "light.turn_{{ trigger.to_state.state }}"
      entity_id: light.bathroom
  • When either of the two motion sensors turns on, the group’s state changes to on and triggers the automation’s first trigger.
  • When both motion sensors are off, the group’s state changes to off. After it remains off for 2 minutes, the automation’s second trigger is triggered.
  • The automation’s action selects the service based on the to_state. The first trigger turns on the light and the second trigger turns it off.
1 Like

Wow, I’m blown away! I really see the benefit of using duration in the triggers rather than delay in the actions.

Can you please explain what the logic is behind needing ‘from’ and ‘to’ states in the first trigger vs. not needing av ‘from’ state in the second trigger?

You could add from: 'on' to the second trigger but it’s not essential. If the previous state was on or unavailable is not important; what we are interested in is that it changes to: 'off' and stays that way for a certain amount of time.

Similarly the first trigger could work without from: 'off' but then it might trigger when the state changes from unavailable to on which is not a state-change we want to trigger this automation. In addition, when an entity’s attributes change, but not its state, that still represents a state-change (for a State Trigger). In other words, you could have the situation where it triggers even though from was on and to is on. By specifying the exact state-change we want, it minimizes the possibility of triggering on an unexpected state-change.