There is a better way to do this here: Motion activated lights automation
You should not wait in automations for too long. They can be interrupted by restarts or reloads.
Also try to avoid device automations, see: Why and how to avoid device_ids in automations and scripts