Garage Lights turn off light only after 5min only if they where turned on from a motion

Hi guys,

I will like to have the garage light to auto turn on and after 5 minutes off, but if I press a button I will like they stay on until I press the button again (or off after 60 minutes)

whats happening now is that the lights goes off after 5 minutes and I have continue to do the dance to keep it on!

This is my current script

alias: Garage Lights Auto ON for X minutes (when Motion or garage door open)
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.garage_door_contact_sensor
    to: "on"
  - type: motion
    platform: device
    device_id: 5b98f1025e631970ffc9d4df32147fec
    entity_id: binary_sensor.garage_motion
    domain: binary_sensor
condition:
  - condition: device
    type: is_off
    device_id: acb3b4b8d936e4f7eb7f24c52571d5c4
    entity_id: switch.garage_lights
    domain: switch
action:
  - type: turn_on
    device_id: acb3b4b8d936e4f7eb7f24c52571d5c4
    entity_id: switch.garage_lights
    domain: switch
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: acb3b4b8d936e4f7eb7f24c52571d5c4
    entity_id: switch.garage_lights
    domain: switch
mode: queued

I’d just create a timer helper.
Have motion, the button event and the timer finishing as triggers.
if the motion is the trigger AND the light is off, turn the light on and start the timer with a 5 minute duation.
if the button event is the trigger then turn the light on and start the timer with a 30 or 60 minute duration (if the timer was already running it will be restarted)
if the trigger is the timer finished event - turn the light off.

Anyone has an example script?

Give me 20 minutes

Wondering if anyone has an example script…