Automation During Date Range

Is there a simple way to have an automation happen during a date range? Say I want a light to turn on every day at sunset, but only for October 15th to the 20th?

I can’t find this in the docs… I’m using the Automations tab in Lovelace to set up automations, but there is no “date” condition.

Any help would be great…
Thanks!

You could write three automations:

  1. sunset light automation
  2. disable sunset light automation on October 15th
  3. enable sunset light automation on October 20th

It’s how I enable/disable my Christmas lights.

- id: xmas_lights_enable  # enable xmas lights auto on/off automation on December 1st at midday.
  alias: 'Xmas Lights enable'
  initial_state: true
  trigger:
    platform: time
    at: '12:05:05'
  condition:
    condition: template
    value_template: '{{ now().month == 12 and now().day == 1 }}'
  action:
  - service: homeassistant.turn_on
    entity_id: automation.xmas_lights_on
  - service: homeassistant.turn_on
    entity_id: automation.xmas_lights_off

- id: xmas_lights_disable  # disable xmas lights auto on/off automation on January 6th at midday.
  alias: 'Xmas Lights disable'
  initial_state: true
  trigger:
    platform: time
    at: '12:00:05'
  condition:
    condition: template
    value_template: '{{ now().month == 1 and now().day == 6 }}'
  action:
  - service: homeassistant.turn_off
    entity_id: automation.xmas_lights_on
  - service: homeassistant.turn_off
    entity_id: automation.xmas_lights_off
4 Likes

What about a condition?

eg:

        condition:
          - condition: template
            value_template: >
              {% set day = now().strftime('%d') %}
              {% set month = now().strftime('%m') %}
              {{  month == '10' and day > '14' and day < '21' }}
2 Likes

Agreed. Although you can simplify a bit…

        condition:
          - condition: template
            value_template: >
              {% set n = now() %}
              {{ n.month == 10 and 15 <= n.day <= 20 }}
5 Likes

Nice Phil.

The conditions are a good way to approach this and I was considering changing my method but the advantage of disabling the automation is that I see feedback in the front end that the light automation is disabled. So I get less ‘why didn’t that run?’ moments.

I’m a little baffled why there is no scheduling option? It seems that Home Automation 101 should include setting something to happen on a day/time…

I’ve not yet advanced enough to understand templates and such.

2 Likes

https://hass-apps.readthedocs.io/en/stable/apps/schedy/index.html

Appreciate it, but I should have mentioned. I’m running Home Assistant directly inside a FreeBSD jail on a NAS.

Is there a way to start an automation between January 8th and December 9th, then in different months and days?
thank you

Is correct?

  condition:
  - condition: template
    value_template: >
      {% set n = now() %}
      {{ 12 <= n.month <= 1 and n.day == 8 }}

That certainly is not correct. The number of the month cannot be both greater than or equal to 12 at the same time as being less than or equal to 1.

It’s not exactly clear what you want. Do you want the automation to be able to run on January 8th, and December 9th, or any day between them (i.e., after January 8th but before December 9th)?

Or do you want the opposite (i.e., on December 9th, and January 8th, or any day between them – after December 9th but before January 8th)?

automation should work between the days from 8 December to 8 January of the following year, every day, and another that should work on 9 January to 7 December of the same year for each day. thank you

Here’s one way you could do this for the first automation:

condition:
  condition: template
  value_template: >
    {% set n = now() %}
    {{ n.month == 12 and n.day >=8
       or n.month == 1 and n.day <= 8 }}

And for the second:

condition:
  condition: template
  value_template: >
    {% set n = now() %}
    {{ not (n.month == 12 and n.day >=8
            or n.month == 1 and n.day <= 8) }}
5 Likes

Is this for all the day between range?

Tom, I googled date range as I was looking for a solution for my Christmas Lights!!! Thanks for your post

I actually like Phil’s solution better and am using it now.

- id: xmas_lights_on
  alias: 'Xmas Lights On'
  trigger:
    platform: numeric_state
    entity_id: sensor.outside_light_level
    below: 660
  condition:
  - condition: sun
    after: sunrise # Active 4 hours after sunrise (prevents false triggering at dawn)
    after_offset: "+04:00:00"
  - condition: template # Only between December 1 and January 6.
    value_template: >
      {% set n = now() %}
      {{ n.month == 12 or ( n.month == 1 and ( 1 <= n.day <= 5 )) }}
  action:
    service: switch.turn_on
    entity_id: switch.xmas_tree_lights

- id: xmas_lights_off
  alias: 'Xmas Lights Off'
  trigger:
  - platform: time
    at: '02:00:00'
  - platform: state
    entity_id: binary_sensor.dark_outside
    to: 'off'
  condition:
    condition: state
    entity_id: switch.xmas_tree_lights # only if the lights are on
    state: 'on'
  action:
    service: switch.turn_off
    entity_id: switch.xmas_tree_lights
3 Likes

Thanks Phil, used your condition template and it works a treat

A simpler concept, but same principle…

I’m trying to turn on a Christmas tree at 5AM and turn it off at 10PM. A couple options…?

  1. Use the switch.toggle option, and somehow make sure no one messes with it manually and gets it out of sequence…

  2. Use two separate automations one that triggers on at 5AM, one that triggers off at 10PM

  3. (probably a smarter way!)

i thought i saw that i could use a condition template similar to what you are doing above, but i dont know how to ‘action’ it to do an on when the condition is met and an off when not.

thoughts? thanks

Do option 2. It’s simple and easy and not much configuration.

Using two automations is definitely simpler, and you get the added benefit of being able to individually turn them on or off if you like. But, for completeness, you can do it in one automation:

trigger:
  - platform: time
    at: 05:00:00
  - platform: time
    at: 22:00:00
action:
  - service_template: >
      switch.turn_{{ 'on' if trigger.now.hour == 5 else 'off' }}
    entity_id: switch.SWITCH
1 Like