Expand automation of a lamp

Dear all,

With my limited knowledge of yaml (beginner) I made 3 automations.
I’m pretty proud of them because they work very well.

Automation 1: Turn on lamp based on lux sensor
Automation 2: Turn off lamp based on lux sensor
Automation 3: Turn off lamp based on time

In automation 3 I would like to expand this.
It happens that I go to bed after the set time (23:30) and then the lamp goes off…

I would like that the light goes off automatically after a preset time (23:30) but…
if I go to bed after the preset time, (and therefore motion is detected) the lamp should remain on.
The lamp should not turn off until no movement has been detected for 15 minutes.

For this, I want to place a motion sensor in the room.
(binary_sensor.motion_sensor)

Who wants, and can help me to implement this in automation 3?

(I would like to keep 3 separate automations)

 - id: 'Automation_1'
   alias: Lamp 1 woonkamer aan
   description: Lamp aanschakelen op basis van lux
   mode: single
   trigger:
   - platform: numeric_state
     entity_id: sensor.illumination_sensor_luminosity
     below: 160
     for: '00:01:00'
   condition: []
   action:
   - type: turn_on
     device_id: cdaa5fcd017708f3ef424e3463e72193
     entity_id: switch.lamp_1_socket_1
     domain: switch


 - id: 'Automation_2'
   alias: Lamp 1 woonkamer uit
   description: Lamp uitschakelen op basis van lux
   mode: single
   trigger:
   - platform: numeric_state
     entity_id: sensor.illumination_sensor_luminosity
     above: 170
     for: '00:01:00'
   condition: []
   action:
   - type: turn_off
     device_id: cdaa5fcd017708f3ef424e3463e72193
     entity_id: switch.lamp_1_socket_1
     domain: switch


 - id: 'Automation_3'
   alias: Lamp 1 woonkamer uit op tijd
   description: Lamp uitschakelen op basis van tijd
   mode: single
   trigger:
     - platform: time
       at: '23:30:00'
   condition: []
   action:
   - type: turn_off
     device_id: cdaa5fcd017708f3ef424e3463e72193
     entity_id: switch.lamp_1_socket_1
     domain: switch

The basic change would be to add a second trigger and mirror it in the condition section.

 - id: 'Automation_3'
   alias: Lamp 1 woonkamer uit op tijd
   description: Lamp uitschakelen op basis van tijd
   mode: single
   trigger:
     - platform: time
       at: '23:30:00'
     - platform: state
       to: 'off'
       entity_id: binary_sensor.motion_sensor
       for: "00:15:00"
   condition: 
     - condition: state
       state: 'off'
       entity_id: binary_sensor.motion_sensor
       for: "00:15:00"
   action:
   - type: turn_off
     device_id: cdaa5fcd017708f3ef424e3463e72193
     entity_id: switch.lamp_1_socket_1
     domain: switch

For a more specific answer you should clarify what other rules need to be implemented regarding motion control of the light… What times of day should motion not turn the light off?

Thanks, I will try it today.