How to write a single automation for a motion-triggered light with time-out for turning off?

I currently have the following two automations to turn on and off the light with a motion sensor (more specifically two motion sensors grouped in binary_sensor.group_flur_sensor_occupancy). They are below.

I would like to consolidate these two automations into a single automation to simplify things. (I use the same configuration in other rooms.)

How would I do this?

This automation turns ON the light, when motion is detected:

alias: automation__flur__automatisches_licht_an
description: ""
mode: single
triggers:
  - entity_id:
      - binary_sensor.group_flur_sensor_occupancy
    from: "off"
    to: "on"
    trigger: state
actions:
  - target:
      entity_id: light.group_flur_licht_decke
    data: {}
    action: light.turn_on

This automation turns OFF the light, when no motion is detected for 5 minutes:

alias: automation__flur__automatisches_licht_aus
description: ""
mode: single
triggers:
  - entity_id:
      - binary_sensor.group_flur_sensor_occupancy
    from: "on"
    to: "off"
    for:
      hours: 0
      minutes: 5
      seconds: 0
    trigger: state
conditions: []
actions:
  - target:
      entity_id:
        - light.group_flur_licht_decke
    data: {}
    action: light.turn_off
1 Like

Here is an alternative, that you can consider.
Single automation with a choose actions based on trigger

alias: Automation - Study - Overhead Light
description: ""
mode: single
triggers:
  - entity_id:
      - binary_sensor.human_presence_sensor_wifi_motion
    id: Occupied
    to: "on"
    trigger: state
  - entity_id:
      - binary_sensor.human_presence_sensor_wifi_motion
    id: Vacant
    to: "off"
    trigger: state
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Occupied
          - condition: state
            entity_id: sun.sun
            state: below_horizon
          - condition: time
            after: "17:00:00"
            before: "23:59:00"
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.study_light_switch_1
      - conditions:
          - condition: trigger
            id:
              - Vacant
          - condition: state
            entity_id: switch.study_light_switch_1
            state: "on"
        sequence:
          - action: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.study_light_switch_1

This takes into consideration time of day, not to switch on the light during day light hours. Also not switching on the light when walking into the room after midnight, I need to manually switch it on if I need lights.

Thank you for the replies. That is very helpful.
I have a follow-up question: Is it possible to set the time-out based on the time of day, e.g. using templating. I.e. is it possible to do something like this(?):

  - platform: state
    entity_id: binary_sensor.your_motion_sensor_here # change this
    to: 'off'
    for:
      minutes: 2 # use templating to set the value here

Would be possible, but not so sure about using the template example.

Using the choose actions from my earlier example, you would be able to start a timer as an action. The times can be of different durations to control how long the light would need to stay on for based on the time of day.