Automation - While the sensor remains open

Good afternoon, this is my first topic, I looked for the Forum but I did not find the solution.

I have my garage gate connected to the HA.
I’m trying to do an automation so it monitors the sensor open 24 hours a day!
I need if the gate remains open for more than 5 minutes, it automatically closes, and if the gate has not closed, it keeps trying every 5 minutes, ALWAYS until the sensor status shows “CLOSED”

I think you can understand

My Code

- id: '1558329831180'
  alias: Garage - Automatico Portao
  trigger:
  - entity_id: binary_sensor.garage_door_sensor
    for: 00:05:00
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: binary_sensor.garage_door_sensor
    state: 'on'
  action:
  - data: 
      entity_id: switch.garage_door_switch
    service: switch.turn_on

on the basis that both your timings are set to 5 min, you could set the automation to run every 5 min and check that the last update is more than 5 min.
Downside, the first action may take up to 10 min (say it runs at 4.59 the first time, it’ll then wait for another 5 min)

- id: '1558329831180'
  alias: Garage - Automatico Portao
  trigger:
    - platform: time_pattern
      minutes: '/5'
      seconds: 0
  condition:
    - condition: state
      value_template: '{{(as_timestamp(now()) - as_timestamp(states.binary_sensor.garage_door_sensor.last_updated | default(0)) | int > 300 )}}'
    - condition: state
      entity_id: binary_sensor.garage_door_sensor
      state: 'on'
  action:
    - service: switch.turn_on
      data:
        entity_id: switch.garage_door_switch