How can I set the delay in an automation depending on the time of day?

Dear all,

I have the following automation, which turns off lights after they were turned on by a group of motion sensors, if no motion has been detected for 5 minutes:

alias: automation__flur__automatisches_licht_aus
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.group_flur_sensor_occupancy
    from: "on"
    to: "off"
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition: []
action:
  - service: light.turn_off
    target:
      entity_id:
        - light.group_flur_licht_decke
    data: {}
mode: single

I would like to change the on-time/delay of 5 minutes depending on the time of day.
How do I achieve this? (I searched/googled for it, but came up empty.)

Thanks and best,
Michael

Extra infos:
I need this, because when waking up/going to bed, we spend more time in the hall (which is adjacent to the bathroom) and it is annoying if lights go off too often. However, the rest of the time we just pass through it.

The way I do it is to have conditions in my actions (choices), and use time constraints. For instance, I keep the bedroom vanity lights on for 20 minutes to give time for getting ready in the morning, but the rest of the day is only stays on 5 minutes. I use this in my actions to accomplish that:

choose:
  - conditions:
      - condition: time
        after: "09:00:00"
        before: "11:59:59"
    sequence:
      - service: homeassistant.turn_on
        data: {}
        target:
          entity_id: light.bedroom_vanity
      - delay: "00:20:00"
  - conditions:
      - condition: time
        after: "17:00:00"
        before: "00:00:00"
    sequence:
      - service: homeassistant.turn_on
        data: {}
        entity_id: group.bedroom_core
      - delay: "00:05:00"
default:
  - service: homeassistant.turn_on
    data: {}
    target:
      entity_id: light.bedroom_ceiling
  - delay: "00:05:00"

In my case the delay is my variable so that the next action of turning off the light occurs next, in the morning it’s 20 minutes but 5 during the day. There are other ways to do this, but this has worked great for me for years.

Thank you for your answer! So if I understand correctly:

This is part of the “ON”-automation and it changes the time that lights remain on by using different delays based on conditions the condition. This seems great.

What I lack in understand is two things:

  • How are lights turned off with this automation? I.e. where in the code is service: homeassistant.turn_off called? Does this happen automatically after the delay has run out?
  • Second is more of a question: Will this reset the delay, if motion sensors trigger again within the delay time?

I am still inexperienced with writing/setting up automations in Home Assistant… Perhaps you could post a complete code example?
Because my setup currently has two automations (one for turning lights on (which I did not show) and the printed example, which turns them off) and this seems too complex (at least I don’t like it, because it spreads concerns into two code-locations).

Thanks again,
Michael

It’s the next action. I just showed the first action of determining the delay, the very next action is the turn_off since the condition set the delay. I also have this set for restart so it will keep the lights on as long as there is movement there.

Yes, that’s the restart part.

I’m sure someone will note that this doesn’t survive a restart, and I realize that but it doesn’t cause any problems for me thus far since it’ll get triggered again after a restart eventually (and quickly).

trigger:
  - platform: state
    entity_id:
      - binary_sensor.group_flur_sensor_occupancy
    from: "on"
    to: "off"
    for:
      minutes: >
        {% set h = now().hour %}
        {{ iif(6 <= h <= 8 or 21 <= h <= 23, 15, 5) }}

It’s 15 minutes between the hours of 6 and 8 or between 21 and 23. Otherwise it’s 5 minutes.

Adjust the time spans to meet your requirements.