Turning lights off when timer empty

Hi all!

I’m quite new with home assistant and I’m trying to automate some outside lights which I want to turn on when sun sets and have it turned off after a couple of hours. The only “but” is that the time has to be at least 23:00 so seasonal difference where sun sets in the winter around 18:00 or in summer around 21:00 are not an issue when using a fixed amount of time.

I used to be a Homey automation user where I could set “trigger - condition - action and an else action”. Here I created a timer and when the timer was idle it went to the else if the condition was not met and the else-action would then be to add 10 more minutes to the timer. This would loop until the condition, after 23:00, was met and the timer went to idle to turn off the lights.

I think I need to have an other approach with home automation but I could not find a topic pointing me into the right direction.

Anyone have a idea for me?

What I have for now , and where I want to replace the delay with a timer, is:

- id: '1592462333363'
  alias: Garden - Lights on sunset
  description: ''
  trigger:
  - event: sunset
    offset: -00:12:00
    platform: sun
  condition: []
  action:
  - brightness_pct: 70
    device_id: c18977c18d0f49629536b2fd5a79edbb
    domain: light
    entity_id: light.Garden_light1
    type: turn_on
  - delay: 01:30:00
  - device_id: c18977c18d0f49629536b2fd5a79edbb
    domain: light
    entity_id: light.Garden_light1
    type: turn_off
  mode: single

just make two automations - one to turn the light on as you have above and one to turn the lights off.

the off automation could be like this:

- alias: Garden - Lights off
  trigger:
  - platform: state
    entity_id: light.Garden_light1
    to: 'on'
    for:
      hours: 1
      minutes: 30
  - platform: time
    at: '23:00:00'
  condition:
  - condition: time
    after: '22:59:00' 
  action:
  - service: light.turn_off
    entity_id: light.Garden_light1
  mode: single

That will turn the light off after it’s been on for 1.5 hours as long as it’s after 2300. if it’s not after 2300 when the on time expires it will wait until 2300 and then turn off the light.

Thanks, I was thinking too complex indeed. I will test this!