Run automation between 2 constant dates all the years

Hi. I´m trying to figure out the code to add to an automation so that it runs lifetime (any year), but only if the current day is between 25th June and 14th september (regardless the year number). I´m not having success, any ideas? Thank you very much in advance

Here are two examples that report a message at 08:00 but only if the current date is within the desired range (June 25 to September 14).

Time Trigger with Template Condition

alias: example 1
trigger:
- platform: time
  at: '08:00:00'
condition: "{{ (6,25) <= (now().month, now().day) <= (9,14) }}"
action:
- service: notify.persistent_notification
  data:
    message: 'Today is {{ now().timestamp() | timestamp_custom() }}'

Template Trigger

alias: example 2
trigger:
- platform: template
  value_template: "{{ now().hour == 8 and (6,25) <= (now().month, now().day) <= (9,14) }}"
action:
- service: notify.persistent_notification
  data:
    message: 'Today is {{ now().timestamp() | timestamp_custom() }}'

The part that you want, confirming the current date is within the range, is performed by this:

(6,25) <= (now().month, now().day) <= (9,14)

It creates a tuple consisting of the current month and day then checks if it’s greater or equal to (6,25) and less than or equal to (9,14).

2 Likes

Did the example I posted work for you?

Hi Taras. I’m on business trip, but I’ll be back at home for the weekend. I’m looking forward to check if your code works, I’m sure it will. In any case I’ll keep you updated. Thank you very much for your help. Best regards

Hi. Do you think it would be possible to generate something like a binary sensor or a calendar (something with just “on” or “off” value), with the condition: (6,25) <= (now().month, now().day) <= (9,14)???
This is, if current date is between 6,25 and 9,14, it has a value, and the rest of the days, the other value. My idea is to call it e.g. “summer”, and in my current automations add a condition “summer”. If the condition “summer” has the value “on”, the automationy will run. They wont in the rest of cases. Thank you in advance

template:
  - sensor:
      - name: My Seasons
        state: "{{ iif((6,25) <= (now().month, now().day) <= (9,14), 'Summer', 'Not Summer') }}"

or

template:
  - binary_sensor:
      - name: Is Summer
        state: "{{ (6,25) <= (now().month, now().day) <= (9,14) }}"

Binary sensor used in the two examples I posted earlier:

Time Trigger with Template Condition

alias: example 1
trigger:
- platform: time
  at: '08:00:00'
condition: "{{ is_state('binary_sensor.is_summer', 'on') }}"
action:
- service: notify.persistent_notification
  data:
    message: 'Today is {{ now().timestamp() | timestamp_custom() }}'

Template Trigger

alias: example 2
trigger:
- platform: template
  value_template: "{{ now().hour == 8 and is_state('binary_sensor.is_summer', 'on')  }}"
action:
- service: notify.persistent_notification
  data:
    message: 'Today is {{ now().timestamp() | timestamp_custom() }}'

Taras’ direction is spot-on; you could also use the Season integration:

https://www.home-assistant.io/integrations/season/

Thank you very much Taras. Works perfect!!!
Also thanks to Terry for the season integration
Great to have experts helping,
Best

Glad to hear it meets your requirements.


Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information, refer to guideline 21 in the FAQ.

Done. Thank you

You marked your own post which contains no information explaining how to solve the problem.

The purpose of the Solution tag is to mark the first post that answers your question. That’s why I suggested you mark my post containing examples of how to achieve what you requested.

OK, I think now is wright

1 Like

Hi there,
question; how to do the above (‘do some action in a date range’) in the visual editor?
Or is that not possible at all ?

thanks

  1. Switch the Automation Editor’s mode from visual to YAML.
  2. Copy the example above and paste it into the Editor (overwrite any existing YAML).
  3. Switch the Automation Editor’s mode from YAML to visual.

Do you know what my problem is?
There is too much info don’t know where to start.
And; I am not a programmer.
And; troubleshooting an automation is really a pain in the b…
Maybe that changes some day, who knows ?

thanks for your tip that helped.

Hey, so I’ve tried multiple ways, but they all end up with a result of “false”. I am trying to get a condition in an automation that runs only when between two dates. However, it is for the holiday season, so the date is overlapping in the next year. This is for my Christmas Lights and I’d like it to run only between Dec 1 and Jan 15. For testing purposes, I made the xmastimestart to be Nov 15 2023.

Now, I tried to set those dates as input_datetime helpers so I can tweak the dates whenever. Doing a template condition in my automation like:

{{ states('input_datetime.xmastimestart') <= states('sensor.date') <= states('input_datetime.xmastimeend') }}

doesn’t want to work, it returns false all the time. I also tried to create a binary sensor with hardcoded values based on the solution of this thread, because it’s not that much of a big deal if I don’t use input helpers:

template:
  - binary_sensor:
    - name: "IsXMasTime"
      state: "{{ (11,15,2023) <= (now().month, now().day, now().year) <= (1,15,2024) }}"
      icon: "mdi:calendar-clock"

, but even that shows as “Off” and I don’t why. When I look at the trace of the automation, I don’t see the actual values it tests, so I am not sure how to troubleshoot what is wrong. My hunch is some kind of date format shenanigans, or syntax since I don’t often use the yaml editor, but it is a bit beyond my knowledge for now.

Any help would be greatly appreciated, thank you!

edit: Removing the year in the binay_sensor doesn’t help

If you’re crossing from one year to the next you can either use an or or a not

#not
template:
  - binary_sensor:
      - name: "IsXMasTime"
        state: "{{ not (11, 30) >= (now().month, now().day) > (1,15) }}"
        icon: "mdi:calendar-clock"
#or
template:
  - binary_sensor:
      - name: "IsXMasTime"
        state: |
          {% set d = (now().month, now().day) %}
          {{  (11, 30) <= d or d <= (1,15) }}
        icon: "mdi:calendar-clock"

If you’re going to use datetime helpers you will be better off using an or, but you need to make sure you are comparing similar data types.

template:
  - binary_sensor:
      - name: "IsXMasTime"
        state: |
          {% set begin = states.input_datetime.xmastimestart.attributes %}
          {% set end = states.input_datetime.xmastimeend.attributes %}
          {% set d = [now().month, now().day] %}
          {{ [begin.month, begin.day] <= d or d < [end.month, end.day] }}
        icon: "mdi:calendar-clock"
1 Like

That works, I set it with the binary sensor that included the datetime helps. Really appreciate the prompt reply, thank you!

What documentation can help me better understand the yaml syntax for home assistant? I use it for infra as code, but it seems a bit confusing to me how the imbrications work here.