Automation for motion controlled light

Dear all:

I’m starting to convert all my HomeKit automations to HA. One of the feature in HomeKit is turn of after xx minutes. Basically I’d like an automation that:

  1. Turn on light when motion and some other conditions, when the light is off
  2. Turn off light if it was turned on by (1) after x minutes
  3. If the motion triggers turn on again, reset the timer
  4. if the light was already on when motion, never turn it off (manual controlled)
  5. when (1) is triggered and I manually turn off the light, ignore the motion trigger for xx minutes

All the above is done simply by allowing turn off after xx minutes in HomeKit, but I cannot see an easier way to do it without introducing a couple of helpers in HA, am I making simple things too complex than necessary?

Many thanks

Here’s a starting point:

Thanks that’s a nice short automation that does many things I listed.
Does anyone know a smart way to track if the light on was triggered by the motion sensor or else I don’t want the motion sensor turn off the light (with the automation example)? I can’t think of a way to do it without helper and I hate helpers…

Heres a rough cut of what your code would look like

Automation: Turn on/off lights automatically

alias: Motion-based light with auto-off and timer reset
trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor
    to: "on"
  - platform: state
    entity_id: light.your_light
    to: "on"
condition:
  - condition: state
    entity_id: light.your_light
    state: "off"
    for:
      seconds: 0  # ensure trigger from motion only if light was off
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: light.your_light
            state: "off"
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.your_light
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.light_auto_triggered
  - delay:
      minutes: 5  # replace with your preferred delay
  - condition: state
    entity_id: input_boolean.light_auto_triggered
    state: "on"
  - service: light.turn_off
    target:
      entity_id: light.your_light
  - service: input_boolean.turn_off
    target:
      entity_id: input_boolean.light_auto_triggered
mode: restart