Motion sensor to tunr lights off if no motion

I have this automation to turn the lights off if there’s no motion for 5 minutes.

alias: Lights OFF if no motion
description: ''
trigger:
  - platform: state
    entity_id: group.sensors_sala
    to: 'off'
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition: []
action:
  - service: light.turn_off
    target:
      entity_id:
        - light.computer
        - light.dining
        - light.living
        - light.entrance
mode: single

The problem is that if the lights turn on (for example, by another automation), the lights stay on forever. Is there a way to fix this?

thanks.

Post the other automation as well, please.

One way (probably not the best):

  1. Create a Timer duration 5 mins
  2. An automation that triggers when light comes on (no matter why) and just starts the Timer
  3. An automation that triggers on motion and restarts the Timer
  4. An automation that triggers on timer.finished and turns off the light.

I have several automations through the day that do stuff like this:

alias: am 05:58 Morning Alert
description: 'We have to run to catch the bus'
trigger:
  - platform: time
    at: '05:58:00'
condition: []
action:
  - service: light.turn_on
    target:
      entity_id:
        - light.living
        - light.entrance
        - light.stairs
    data:
      brightness_pct: 50
  - service: light.turn_on
    target:
      entity_id:
        - light.living
        - light.entrance
        - light.stairs
    data:
      color_name: red
mode: single

I have the same issue with some of my lights. For example, someone turns on the basement lights, does not actually go into the basement to trigger the motion, and then the no-motion automation does not fire. The lights consequently just stay on. Maybe a trigger when the lights turn on for 5 minutes with a condition of no motion, turn off lights?