WTH Run automation between start and end time

I would like to be able to set a start and end time for an automation.

For example. Run an automation between 10pm and 1am.

Now I have to use a time pattern and check the time as a condition. However, this also results in the log notice that the automation is already running, when it uses a delay.

So start and end time would be great.

Delays in automations should generally be avoided. If you have to use it, add max_exceeded: silent along with mode: single to silence the warnings.

I don’t think we need a separate way to handle these kinds of automations. The current approach makes sense (trigger the automation every 5 minutes if time is between 10pm and 1am). You’d need to provide all these numbers anyways, right?

Or are you looking for a way to retrigger the automation as soon as it finishes running its actions? A more concrete example might help.

A better way would be to trigger only on state changes and use the time condition. Time pattern triggers are rarely the best answer.

3 Likes

State changes don’t always work when you are not home and it comes to PIR sensors. If you want to randomly want to turn of lights at night between a certain time period, when you are not home.

What I would like is to be able to let an automation run between 10pm and 1am for example.

In case I am not home and the TV is off and the PIR sensor has no motion detected it should turn off the light at a random time. I do it with a time pattern now, but that is not ideal. The automation checks ever x minutes if the tv is on and the pir sensor is off for about 20 minutes. And it also checks if the time is between a certain time schedule. Then it triggers a light off command with a random delay.

The above should also work when I am home. And turn of the tv and go upstairs for example. The pir trigger/condition is then different. So I can not always use a state change.

There are more people on the forum with this issue.

Probably because they too try and use time pattern automations.

Your explanation of the automation is vague and I’m not sure what you are actually trying to do.
Why would a light be on and why does this need to run all the time if it has already run once to turn off the light?

Explain the logic in your automation and we will help you remove the time pattern

The light is on, because it goes on before sunset through an automation. Even when I am not home, to make it look someone is home. :slight_smile:

So, I need to have it turned off automatically too when I am home or not at home. It should not make a difference. In case I am home it should be turned off when the TV is turned off and the pir sensor is not active for a certain time. Otherwise it should remain on.

Here is how my automation looks now.

---
#
# https://www.home-assistant.io/docs/automation/
#
id: 2ae6f3d9-e213-4d03-9fd1-c0adb8f41a95
alias: living_room_ceiling_light_turn_off_at_night
trigger:
  - platform: time_pattern
    minutes: '/2'
condition: 
  condition: and
  conditions:
    - condition: time
      after: '22:15'
      before: '02:00'
    - condition: state
      entity_id: device_tracker.living_room_tv
      state: 'not_home'
    - condition: state
      entity_id: light.living_room_ceiling_light
      state: 'on'
    - condition: state
      entity_id: binary_sensor.living_room_pir_sensor_occupancy
      state: 'off'
      for:
        minutes: 15
action:
  - delay: 
      minutes: '{{ range(1, 20) | random }}'
  - service: script.living_room_ceiling_light_turn_off

I would say:

---
#
# https://www.home-assistant.io/docs/automation/
#
id: 2ae6f3d9-e213-4d03-9fd1-c0adb8f41a95
alias: living_room_ceiling_light_turn_off_at_night
trigger:
  - platform: state
    entity_id: device_tracker.living_room_tv
    state: 'not_home'
  - platform: time
    at: "22:15:00"
  - platform: state
    entity_id: binary_sensor.living_room_pir_sensor_occupancy
    state: 'off'
    for:
      minutes: 15
condition: 
  - condition: time
    after: '22:15'
    before: '02:00'
  - condition: state
    entity_id: light.living_room_ceiling_light
    state: 'on'
  - condition: state
    entity_id: device_tracker.living_room_tv
    state: 'not_home'
  - condition: state
    entity_id: binary_sensor.living_room_pir_sensor_occupancy
    state: 'off'
    for:
      minutes: 15
action:
  - delay: 
      minutes: '{{ range(1, 20) | random }}'
  - service: script.living_room_ceiling_light_turn_off

Thanks. That looks interesting. Haven’t looked at it that way. :slight_smile:

Had to do a small change:

---
#
# https://www.home-assistant.io/docs/automation/
#
id: 2ae6f3d9-e213-4d03-9fd1-c0adb8f41a95
alias: living_room_ceiling_light_turn_off_at_night
trigger:
  - platform: state
    entity_id: device_tracker.living_room_tv
    from: 'home'
    to: 'not_home'
  - platform: time
    at: "22:15:00"
  - platform: state
    entity_id: binary_sensor.living_room_pir_sensor_occupancy
    from: 'on'
    to: 'off'
    for:
       minutes: 15
condition: 
  condition: and
  conditions:
    - condition: time
      after: '22:15'
      before: '02:00'
    - condition: state
      entity_id: device_tracker.living_room_tv
      state: 'not_home'
    - condition: state
      entity_id: light.living_room_ceiling_light
      state: 'on'
    - condition: state
      entity_id: binary_sensor.living_room_pir_sensor_occupancy
      state: 'off'
      for:
        minutes: 15
action:
  - delay: 
      minutes: '{{ range(1, 20) | random }}'
  - service: script.living_room_ceiling_light_turn_off
mode: single

State does not work in the trigger. Does not validate.

Yes true…
But you don’t need the and in conditions.

Missed that part. :slight_smile: Is it “and” by default then?

Yes conditions are default and, triggers are default or

1 Like

personally, I’d set a datetime helper to a random time between 10pm and 1am in the turn light on @ sundown automation. Then make a simple automation that fires once at that random time and checks if anyone is home and there’s no motion. It would require a template, which everyone is scared of… but here’s the template in the set datetime action.

put this inside your light on after sundown automation.

- service: input_datetime.set_datetime
  target:
    entity_id: input_datetime.light_off
  data:
    # Range(60, 181) makes a random number of minutes between 60 and 180 minutes
    # the 1 is needed to include the 180th minute
    datetime: >
      {% set minutes = range(60, 181) | random %}
      {{ (today_at("22:00") + timedelta(minutes=minutes)).strftime('%Y-%m-%d %H:%M:%S') }}

Then your turn off automation is simply

id: 2ae6f3d9-e213-4d03-9fd1-c0adb8f41a95
alias: living_room_ceiling_light_turn_off_at_night
trigger:
  - platform: time
    at: input_datetime.light_off
condition:
    - condition: state
      entity_id: device_tracker.living_room_tv
      state: 'not_home'
    - condition: state
      entity_id: light.living_room_ceiling_light
      state: 'on'
    - condition: state
      entity_id: binary_sensor.living_room_pir_sensor_occupancy
      state: 'off'
      for:
        minutes: 15
action:
  - service: script.living_room_ceiling_light_turn_off

This will survive restarts, it’ll only trigger once. Not much overhead for updates aside from the template which shouldn’t ever break.

I might put in a PR to make it so that the template could work like this instead to make it easier on people:

    # Range(60, 181) makes a random number of minutes between 60 and 180 minutes
    # the 1 is needed to include the 180th minute
    datetime: >
      {% set minutes = range(60, 181) | random %}
      {{ today_at("22:00") + timedelta(minutes=minutes) }}

heck, it may work right now, i’d have to check.

I can’t see a reason why it shouldn’t.

Not sure why the list is needed

I just put it in there because I couldn’t remember if it was needed.

EDIT: Just tested the code, it’s not needed. Will update.

In regards to this, I don’t remember if datetime objects are accepted when outputting to the datetime field. It most likely is and OP is can try both. I’d try the one without the text conversion first.

Thanks. I guess I learned it the wrong way then. :slight_smile:

Thanks for the idea. I will maybe try it later.

But the fact it only fires once could prevent it from turning off the light. For example, if one of the conditions is not met it will not fire again and the light will stay on the whole night. At least, that’s what I understand from it right now.

Yes that’s true, but then just remove the no-motion, not sure why that matters if you aren’t home.

Someone else had asked for a way to turn on a Xmas tree 20 minutes before sunset and turn it off later at 22:32 plus a random number of minutes. I suggested a Trigger-based Template Sensor to compute the turn-off time (whose device_class is timestamp). The resulting sensor is used in a Time Trigger.

Details here: