Turn on, for a short duration, then turn off code

So, I’m trying to combine some automations, neaten-up, if you will. I have working automations, 2, that do this, but want to combine the 2 to 1. I tried playing with the building blocks, they seem to do what I want, but they don’t work like that.
I want to turn on a light(smartplug) upon the door sensor triggering, then turn it off after maybe 4 minutes.
The 2 automations that work are here:

- id: '1708744525201'
  alias: New Front Porch Light on Door opening After sunset 2-23-24
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.lumi_lumi_sensor_magnet_aq2_87da0008_on_off
    from: 'off'
    to: 'on'
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - service: switch.turn_on
    metadata: {}
    data: {}
    target:
      device_id: e0f4c7d64634bd117c387dd8fcba3ad8
  mode: single

This works to turn everything on.
Then, the automation to turn off:

- id: '1631584513193'
  alias: Doorlamps off after 4mins turned on.
  description: Doorlamps off after 4mins turned on.
  trigger:
  - platform: state
    entity_id:
    - light.fr_door_lamps_light
    to: 'on'
    for:
      hours: 0
      minutes: 4
      seconds: 0
    from: 'off'
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - type: turn_off
    device_id: da1decfe8b469feb3e5e0ec596c8cdc6
    entity_id: adcf612f106de094ea6dc8d6182efef7
    domain: light
  mode: single

Some way to make this one, without adding a helper timer.
Thanks.

Could you not just add a wait time-out to the script? Or, a wait template that reads an input number entity so you can customize it from the UI?

as @arretx said:

- id: '1708744525201'
  alias: New Front Porch Light on Door opening After sunset 2-23-24
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.lumi_lumi_sensor_magnet_aq2_87da0008_on_off
    from: 'off'
    to: 'on'
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - service: switch.turn_on
    metadata: {}
    data: {}
    target:
      device_id: e0f4c7d64634bd117c387dd8fcba3ad8
  delay:
    hours: 0
    minutes: 4
    seconds: 0
    milliseconds: 0
  action:
  - type: turn_off
    device_id: da1decfe8b469feb3e5e0ec596c8cdc6
    entity_id: adcf612f106de094ea6dc8d6182efef7
    domain: light
  mode: single

you should also avoid using device_id… explained here.

I’m getting a lot of HA grief for that first action line. Not running like it should.

I was trying to avoid setting timers on it. It works fine standing alone, I was just looking to combine it into 1 automation. Still playing with it, thanks.

see my code above. it’s all in one automation.

Yes, I cut and pasted your code, and my editor kicked out that the first action line had bad indentation. I pulled it back a space and the warning went away, but it didn’t work.

And, of course I found out I pasted the wrong code of mine, that came from helping someone earlier.
Sorry about that. the first code entry of mine is wrong, the second is correct.

  alias: DoorLamps ON at entry after sundown 2-27-24
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.lumi_lumi_sensor_magnet_aq2_87da0008_on_off
    from: 'off'
    to: 'on'
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - service: light.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: light.fr_door_lamps_light
  mode: single

Here’s the second automation … same as above except for device id cleanup…

  alias: Doorlamps off after 4mins turned on.
  description: Doorlamps off after 4mins turned on.
  trigger:
  - platform: state
    entity_id:
    - light.fr_door_lamps_light
    to: 'on'
    for:
      hours: 0
      minutes: 4
      seconds: 0
    from: 'off'
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.fr_door_lamps_light
  mode: single

THIS is the correct code . Thanks to your suggestion, I cleaned up all of my config of device ids. :slight_smile:
I just went into automations, and re-entered the action device to be an entity versus the actual device (green versus blue button).
Odd thing I noticed, all text entries are red now, not the old blue at the beginning of the line, in config.yaml. My older entries are still blue though.

sorry, i didn’t check your original code, i just added the delay part.
add this delay, then your action to turn_off.

    delay:
      hours: 0
      minutes: 4
      seconds: 0
      milliseconds: 0
alias: DoorLamps ON at entry after sundown 2-27-24
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.lumi_lumi_sensor_magnet_aq2_87da0008_on_off
    from: "off"
    to: "on"
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - service: light.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: light.fr_door_lamps_light
  - delay:
      hours: 0
      minutes: 4
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.fr_door_lamps_light
mode: single

Never mind about the red text comment, I figured that out. Must have fat fingered something. Extra id line with no minus, doing nothing, threw off everything below that line.

Bingo, that worked. Thanks so much! Learning more every time I dig into something.