Delay and auto off for PIR "on"

Good day

I would like the code to do the following:
Switch on relay 4 when movement is detected - Working
Switch Off Relay 4 when movement is detected again - No Idea how
Switch Off Relay 4 after 5min regardless of any movement.

automation 1:
  alias: Night Light
  trigger:
    platform: state
    entity_id: binary_sensor.spare_room_pir_sensor
    state: "on"
  action:
    service: homeassistant.turn_on
    entity_id: switch.relay_4

Please supply me with the correct code for this automation

I have created a file called night_light.yaml in a folder called automations.
I have also created a folder called scripts with a file called night_light.yaml.

Here is the night_light.yaml file contents under automation folder:

        alias: Night Light
        trigger:
        platform: state
        entity_id: binary_sensor.spare_room_pir_sensor
        state: "on"
      action:
        service: script.turn_on
        entity_id: script.night_light```

Here is the night_light.yaml file contents under script folder:

```  night_light:
    alias: Night Light
    sequence:
      - alias: Night Light On
        service: homeassistant.turn_on
        data:
          entity_id: switch.relay_4
- delay:
          minutes: 5
      - alias: Turn Night Light Off
        service: homeassistant.turn_off
        data:
          entity_id: switch.relay_4```

I have also added this line in the scripts.yaml file:
```- !include scripts/night_light.yaml```

And this line under the automation.yaml file.
```- !include automation/mode/night_light.yaml```
1 Like