Combining 2 automations into 1

I’ve got two automations that are controlling a hallway light with two motion sensors. The 1st one turns the light on when either of the motions sensors detects movement. The 2nd one turns the light off after two minutes when both motions sensors are inactive.

Here’s my proposed automation, putting both of them together.

- id: '2222222'
  alias: Hallway
  trigger:
  - entity_id: binary_sensor.hallway_motion
    platform: state
    to: 'on'
  - entity_id: binary_sensor.stairway_motion
    platform: state
    to: 'on'
  - entity_id: binary_sensor.hallway_motion
    for:
	  minutes: 2
    platform: state
    to: 'off'
  - entity_id: binary_sensor.stairway_motion
    for:
	  minutes: 2
    platform: state
    to: 'off'
  action:
    - service: homeassistant.turn_on
      data:
	    entity_id: light.hallway
    - condition: or
	  conditions:
      - condition: state
        entity_id: binary_sensor.hallway_motion
        state: on
      - condition: state
        entity_id: binary_sensor.stairway_motion
        state: on
    - service: homeassistant.turn_off
      data:
	    entity_id: light.hallway
    - condition: and
	  conditions:
      - condition: state
        entity_id: binary_sensor.hallway_motion
        state: off
      - condition: state
        entity_id: binary_sensor.stairway_motion
        state: off

Will this work at all?

Thanks!

I don’t think so. When a condition fails in the action of an automation it just ends the automation then without completing any other actions.

Consider triggering on just the motion sensors going on, then in your action turn on the light, have a wait_template that waits for both motion sensors to be off (or two wait_templates), then a two minute delay, and then turn off the lights. I would also include an automation condition (not in the action) that only fires this if the lights are off so that they don’t get turned off if you manually turn them on and then trigger the motion sensor (but of course this depends on what you want).