Help with automation.yaml - PIR to switch on/off light

I have an automation to control a PIR who turns on light in my basement. The only way i manage to turn the light off, is to put in a delay. Can somenone explain to me how i reset the delay when new motion is detected.

automations.yaml

- alias: 'Kjellarlys - '
  trigger:
  - entity_id: binary_sensor.kjellar_pir
    platform: state
    to: 'on'
  action:
  - entity_id: switch.bfa5cb2c8f5aa6eb74dqmq
    service: switch.turn_on
  - delay: '300'
  - entity_id: switch.bfa5cb2c8f5aa6eb74dqmq
    service: switch.turn_off

Short, sweet and to the point.
It’s amazing what you can find in the documentation
:wink:

Thannk you. That is how easy it can be. Sorry for not search it up myself. Will try to do that next time :slight_smile:

Here’s one possible way to do it using a single automation:

- alias: 'Kjellarlys - '
  trigger:
  - platform: state
    entity_id: binary_sensor.kjellar_pir
    to: 'on'
  - platform: state
    entity_id: binary_sensor.kjellar_pir
    to: 'off'
    for: '00:05:00'
  action:
    service: "switch.turn_{{ trigger.to_state.state }}"
    entity_id: switch.bfa5cb2c8f5aa6eb74dqmq

It will turn on the switch when kjellar_pir turns on and it will turn off the switch only when kjellar_pir turns off, and remains off, for 5 minutes (300 seconds).

1 Like

And further, you could template the ‘for’ time to have it adjustable from the front end (input number) set it to seconds, that way you could go from say 10 secs to 600 (10 mins) or more if needed