Automation to bypass montion sensor

Hello, I only recently started doing some automation on HA, I have all devices from Philips Hue, but got them ‘disable’ on Hue app and do the automations on HA.

I got some lights that turn on with a montion sensor, and I’m doing a automation to bypass that.
But the part that I need help is to make this automation check if some one forget to turn off lights after 30min.
If they forget to turn lights off on the switch and there is no montion on last 5 minutes, the automation should proceed with turn off switch, if there is montion detection on last 5min it shouls start a repeat check every 5 minutes, until there is no montion detect on last 5min.

Here is the code I have right now:

trigger:
  - platform: state
    entity_id:
      - input_boolean.x
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.x
            state: "on"
        sequence:
          - service: adaptive_lighting.apply
            data:
              lights:
                - light.a
                - light.b
              entity_id: switch.adaptive_lighting.y
              transition: "3"
              turn_on_lights: true
              adapt_color: true
              adapt_brightness: true
          - service: automation.turn_off
            data:
              stop_actions: true
            target:
              entity_id: automation.montion_control_lights
      - conditions:
          - condition: state
            entity_id: input_boolean.x
            state: "off"
        sequence:
          - service: light.turn_off
            data:
              transition: 10
            target:
              entity_id:
                - light.a
                - light.b
          - service: automation.turn_on
            data: {}
            target:
              entity_id: automation.montion_control_lights
  - delay:
      hours: 0
      minutes: 30
      seconds: 0
      milliseconds: 0
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: state
                entity_id: binary_sensor.sensor_montion
                state: "off"
                for:
                  minutes: 5
              - condition: or
                conditions:
                  - condition: state
                    entity_id: light.a
                    state: "on"
                  - condition: state
                    entity_id: light.b
                    state: "on"
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.x
      - conditions:
          - condition: and
            conditions:
              - condition: state
                entity_id: binary_sensor.sensor_motion
                state: "on"
                for:
                  minutes: 5
              - condition: state
                entity_id: light.a
                state: "on"
              - condition: state
                entity_id: light.b
                state: "on"
        sequence:
          - repeat:
              while:
                - condition: state
                  entity_id: binary_sensor.sensor_motion
                  for:
                    minutes: 5
                  state: "on"
              sequence: []

Maybe you could use a last_changed of the light if that goes to 30 minutes you can further run your automation.
I use things like this, this is for my tap:

{{ relative_time(states.input_boolean.kraan_aan_uit.last_changed) }}

That is what I do and I use a boolean too for manual operated light set that to one so I know that the motion sensor automation will not switch off the light.

Why lights?
I want to use montion sensor because if there is some one constantly in that area, after the delay of 30min I want the lights only go off/and automation back ON if for 5min there is montion in the area.

This is just like a backup, if someone forget to turn off switch, so it back to normal mode that is turn lights when montion detect, but this is done on another aumation