Understanding Automation Logic for Light Toggle

I’ve been experimenting with automations in my smart home setup, and I’m curious about how to refine one particular feature. I’ve set up an automation where my mudroom light turns on when I open the door and then turns off after five minutes. It works perfectly the first time I open the door—the light comes on and turns off after the delay. However, if I open the door again within those five minutes, the light instantly turns off instead of restarting the timer. If I keep opening and closing the door, the light toggles on and off with each action.

Here’s how the automation is set up:

yaml
alias: Turn on Mud Room light when door opens, then off 5 minutes later
trigger:
platform: state
entity_id: sensor.MudRoomDoor
from: ‘closed’
to: ‘open’
action:

  • service: homeassistant.turn_on
    entity_id: light.MudRoomLight
  • delay: ‘00:05:00’
  • service: homeassistant.turn_off
    entity_id: light.MudRoomLight

I was hoping that opening the door again during the delay would simply restart the timer, but instead, it turns the light off immediately. I know I could create separate automations for turning the light on and off, but I’d really like to understand why this setup behaves this way. Is there a way to modify the automation so that it restarts the timer instead of toggling the light off?

This feature is something my family really enjoys, and I’d love to make it work seamlessly. Any insights or suggestions would be greatly appreciated! Thanks in advance for your help!