Adding Delay and Reset Timer to Motion Sensor Automation

I recently encountered an issue where my office lights would turn off after a two-minute delay even if I returned to the office within that timeframe. To address this, I decided to modify my existing automation to include a delay and reset functionality. Here’s how I approached the solution:1. Understanding the Problem: The lights would turn off after two minutes of inactivity, which was inconvenient if I only stepped out briefly. I wanted the lights to stay on if I returned within the delay period.2. Modifying the Automation: I added a delay action to the automation that turns off the lights. However, I also needed a way to reset this delay if motion was detected again within the two-minute window.3. Using Variables: I introduced a variable to track the state of the delay. When the delay starts, the variable is set to ‘active’. If motion is detected again, the variable is set to ‘cancelled’, which resets the delay.4. Adjusting the Logic: In the automation, I included a condition to check the state of the variable. If it’s ‘cancelled’, the lights remain on. If it’s ‘active’, the lights turn off after the delay.5. Testing and Refinement: I tested the automation multiple times to ensure it worked as expected. Adjustments were made to the delay duration and conditions to optimize the behavior.By implementing these changes, I achieved a more user-friendly automation where the lights stay on if I return within the delay period, enhancing both convenience and energy efficiency.