Start second timer after first finished

Hello, complete new here, and searched many topics, and YouTube, but I can’t find a working solution.
I have the following situation: Motion sensor, and led strip. That’s it.
What I want: after movement, enable the led strip for a fixed time, e.g. 2 minutes. After the two minutes, motion can be detected again, for e.g. 15 seconds. And this time, when there is a new motion within these 15 seconds, the timer may start again for 15 seconds. And this goes on as long as there is motion.
Is this possible?

I have watched this YouTube tutorial, but in this example, the timer restarts after a new motion is detected. And I want a fixed time after the first motion.

I tried with delay, but just don’t know to make the second part.

This is the code from the Youtube tutorial

alias: Badkamer beweging copy original
description: ""
triggers:
  - trigger: event
    event_type: timer.finished
    event_data:
      entity_id: timer.badkamer_timer
    id: Timer finished
  - type: no_motion
    device_id: 7c9b2ceb48db904196b9aa1ae269caad
    entity_id: 1434ddd41f85d7fccc3e9f6342a9204b
    domain: binary_sensor
    trigger: device
    id: Motion stopped
  - type: motion
    device_id: 7c9b2ceb48db904196b9aa1ae269caad
    entity_id: 1434ddd41f85d7fccc3e9f6342a9204b
    domain: binary_sensor
    trigger: device
    id: Motion detected
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Motion stopped
        sequence:
          - action: timer.start
            data:
              duration: "120"
            target:
              entity_id: timer.badkamer
      - conditions:
          - condition: trigger
            id:
              - Motion detected
        sequence:
          - action: timer.cancel
            target:
              entity_id: timer.badkamer_timer
            data: {}
          - type: turn_on
            device_id: 7c9b2ceb48db904196b9aa1ae269caad
            entity_id: 8243d8b162c2cc1f8f8a023b6391e913
            domain: light
      - conditions:
          - condition: trigger
            id:
              - Timer finished
        sequence:
          - type: turn_off
            device_id: 7c9b2ceb48db904196b9aa1ae269caad
            entity_id: 8243d8b162c2cc1f8f8a023b6391e913
            domain: light
mode: single

And this is my simple code

alias: Badkamer licht aan
description: ""
triggers:
  - type: motion
    device_id: 7c9b2ceb48db904196b9aa1ae269caad
    entity_id: 1434ddd41f85d7fccc3e9f6342a9204b
    domain: binary_sensor
    trigger: device
conditions: []
actions:
  - action: light.turn_on
    metadata: {}
    data:
      brightness_pct: 33
      transition: 2
    target:
      entity_id: light.badkamer_esp32_badkamer_ledstrip
  - action: timer.start
    metadata: {}
    data:
      duration: "60"
    target:
      entity_id: timer.badkamer
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - action: light.turn_off
    metadata: {}
    data:
      transition: 0
    target:
      entity_id: light.badkamer_esp32_badkamer_ledstrip
mode: single

Regards, Peter

You can do this with repeat until no motion (delay 15 seconds). (You’ll have to look up the exact syntax.)

However, you should first make sure that your motion detector can actually detect a change in 15 second increments. My experience is that anything under a minute is iffy.

The way I handle this is to have the automation turn on the lights for longer than the minimum time of the sensor (5 minutes in this case) and if the sensor triggers again during those 5 minutes, it restarts the automation. This means it will always be on for 5 minutes from the last time motion was detected:

- alias: Bedroom closet lights on motion
  id: bedroom_closet_lights_on_with_motion
  mode: restart
  trigger:
    platform: state
    entity_id: binary_sensor.bedroom_closet_motion_sensor, binary_sensor.new_motion_sensor
    to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: switch.bedroom_closet_ceiling_lights
    - delay: '00:05:00'
    - service: homeassistant.turn_off
      entity_id: switch.bedroom_closet_ceiling_lights

-David

There’s an example of how to do this in the Cookbook:

Motion activated lights automation

Sorry, this is not what I want.
I have this sensor.
And I have an ESP32 with ESPhome installed.

I would like if motion is detected, the led turns on and stays on for (let’s say) two minutes. So, during this two minutes, the timer should not be restarted even when there is new motion detected.
Then, after those two minutes, a new motion may be detected and the led turns on for 15 seconds. When new motion is dectected within these 15 seconds, the timer may restart and the led stays on.
Then, with no more motion, the whole loop starts again.

Can this be made?

Regards, Peter

Short answer, yes this can be done using the timer, by setting the duration of the timer differently when certain conditions are met.

This is an example from my Garage Light, based on triggers and conditions the automation rechecks / resets once the timers expire.

alias: Automation - Garage - Light - Timer V2
description: ""
triggers:
  - entity_id:
      - switch.garage_laundry_switch_3
    to: "on"
    trigger: state
  - entity_id:
      - switch.garage_laundry_switch_3
    to: "off"
    trigger: state
  - event_type: timer.finished
    event_data:
      entity_id: timer.garage_light_timer
    id: timer_complete_garage_light_timer
    trigger: event
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - timer_complete_garage_light_timer
          - condition: or
            conditions:
              - condition: state
                entity_id: cover.garage_door_door
                state: open
              - condition: state
                entity_id: binary_sensor.motion_sensor_2_motion
                state: "on"
        sequence:
          - metadata: {}
            data:
              duration: "00:15:00"
            target:
              entity_id: timer.garage_light_timer
            action: timer.start
      - conditions:
          - condition: trigger
            id:
              - timer_complete_garage_light_timer
          - condition: or
            conditions:
              - condition: state
                entity_id: binary_sensor.motion_sensor_2_motion
                state: "on"
        sequence:
          - metadata: {}
            data:
              duration: "00:05:00"
            target:
              entity_id: timer.garage_light_timer
            action: timer.start
      - conditions:
          - condition: trigger
            id:
              - timer_complete_garage_light_timer
        sequence:
          - action: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.garage_laundry_switch_3
      - conditions:
          - condition: state
            entity_id: timer.garage_light_timer
            state: idle
          - condition: state
            entity_id: switch.garage_laundry_switch_3
            state: "on"
        sequence:
          - metadata: {}
            data:
              duration: "00:05:00"
            target:
              entity_id: timer.garage_light_timer
            action: timer.start
    default:
      - metadata: {}
        data: {}
        target:
          entity_id:
            - timer.garage_light_timer
        action: timer.cancel
mode: single

When the door is open, the timer runs for 15 minutes, but f the door is closed the timer runs for 5 minutes, and will restart if there is still motion in the garage. This automation also accommodates you manually switching he light on and off to cancel the timers.

This does require some testing with your motion sensor the ones I use does keep the "detected: state active for 40 to 50 seconds after the last motion, so if you don;t move in that instant the timer expire it doesn’t matter.