Automation During Date Range

remarkable so may things to be done in less lines wow you rule!

Any idea why this wouldn’t work? I want it to run if it’s from the 26th through the 31st? It didn’t fire tonight, although I probably have this wrong.

{% set n = now() %} {{ n.month == 10 and 26 >= n.day == 31 }}

or maybe…?

{>25 n.day =31}}

Not sure…

Like this:

{% set n = now() %} {{ n.month == 10 and 26 <= n.day <= 31 }}

Or simply this:

{{ now().month == 10 and 26 <= now().day <= 31 }}
1 Like

Thanks, I’ll try that. I’m a little confused by the format. In the case of setting lights for Halloween, I only had a few days to figure it out. Thanks again.

hi guys, can anyone help me to set a condition to run an automation only between the months October and April?

I guess this is wrong?

value_template: '{{ now().month >= 10 or now().month <= 4 }}'

many thanks.

Your template is correct if you are using it in a Template Condition like this:

  condition:
    - condition: template
      value_template: '{{ now().month >= 10 or now().month <= 4 }}'

If you wish, you can also do it like this:

  condition:
    - condition: template
      value_template: '{{ now().month in [1,2,3,4,10,11,12] }}'
3 Likes

Thanks so much! it didn’t work but I noticed now I had an extra space at the very beginning. maybe that caused an error…

Hi Taras, the automation is not running. Do you have any idea what could be the problem? Thanks again.

I have no idea because you never posted an automation (just a lone condition). Post the automation so we can all examine it. However, post it in a new topic instead of in this one.

yes of course, i was just about to do that :slight_smile: Thanks.

- id: '1607512023354'
  alias: Rollo Close 9pm (Winter)
  description: ''
  trigger:
  - platform: time
    at: '21:00'
  condition:
  - condition: template
    value_template: 'value_template: ''{{ now().month in [1,2,3,4,10,11,12] }}'''
  action:
  - device_id: ddefb11478ca62dc338bc2aad06ac5a6
    domain: cover
    entity_id: cover.rfy_000a01_1
    type: close
  mode: single

ah i think i saw my mistake!
the value template line is wrong… oh boy.

Exactly. :+1:

I realize your post is old but I just finished setting up HACS Custom Scheduler Component & Card for my Christmas lighting. It uses From and To dates which I have an automation that will run on January 2nd to change the schedule to the current year.

alias: Christmas Lighting Schedule
description: ''
trigger:
  - platform: time
    at: '00:01:00'
condition:
  - condition: template
    value_template: '{{ now().month == 1 and now().day == 2 }}'
action:
  - service: scheduler.edit
    data:
      entity_id: switch.schedule_december_on
      start_date: '{{ now().year}}-11-30'
      end_date: '{{ now().year}}-12-31'
  - service: scheduler.edit
    data:
      entity_id: switch.schedule_december_off
      start_date: '{{ now().year}}-12-01'
      end_date: '{{ now().year + 1}}-01-01'
mode: single

image


image

I also have a Node-Red flow that does it using bigtimer but the above is prettier in my HA front end!
image

[{"id":"a18db3b210c6710b","type":"bigtimer","z":"b896509c.caf71","outtopic":"","outpayload1":"turn_on","outpayload2":"turn_off","name":"December","comment":"","lat":"49.176400","lon":"-122.970130","starttime":"5005","endtime":"5006","starttime2":0,"endtime2":0,"startoff":0,"endoff":0,"startoff2":0,"endoff2":0,"offs":0,"outtext1":"","outtext2":"","timeout":1440,"sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":false,"feb":false,"mar":false,"apr":false,"may":false,"jun":false,"jul":false,"aug":false,"sep":false,"oct":false,"nov":false,"dec":true,"day1":0,"month1":"0","day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"day6":0,"month6":0,"day7":0,"month7":0,"day8":0,"month8":0,"day9":0,"month9":0,"day10":0,"month10":0,"day11":0,"month11":0,"day12":0,"month12":0,"d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"d6":0,"w6":0,"xday1":0,"xmonth1":0,"xday2":0,"xmonth2":0,"xday3":0,"xmonth3":0,"xday4":0,"xmonth4":0,"xday5":0,"xmonth5":0,"xday6":0,"xmonth6":0,"xday7":0,"xmonth7":0,"xday8":0,"xmonth8":0,"xday9":0,"xmonth9":0,"xday10":0,"xmonth10":0,"xday11":0,"xmonth11":0,"xday12":0,"xmonth12":0,"xd1":0,"xw1":0,"xd2":0,"xw2":0,"xd3":0,"xw3":0,"xd4":0,"xw4":0,"xd5":0,"xw5":0,"xd6":0,"xw6":0,"suspend":false,"random":false,"randon1":false,"randoff1":false,"randon2":false,"randoff2":false,"repeat":true,"atstart":true,"odd":false,"even":false,"x":110,"y":240,"wires":[["d8ed835e6cfce90f"],[],[]]},{"id":"242f814b1a0d7c90","type":"bigtimer","z":"b896509c.caf71","outtopic":"","outpayload1":"turn_on","outpayload2":"turn_off","name":"Jan 1st","comment":"","lat":"49.176400","lon":"-122.970130","starttime":"0","endtime":"5006","starttime2":0,"endtime2":0,"startoff":0,"endoff":0,"startoff2":0,"endoff2":0,"offs":0,"outtext1":"","outtext2":"","timeout":1440,"sun":false,"mon":false,"tue":false,"wed":false,"thu":false,"fri":false,"sat":false,"jan":false,"feb":false,"mar":false,"apr":false,"may":false,"jun":false,"jul":false,"aug":false,"sep":false,"oct":false,"nov":false,"dec":false,"day1":"1","month1":"1","day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"day6":0,"month6":0,"day7":0,"month7":0,"day8":0,"month8":0,"day9":0,"month9":0,"day10":0,"month10":0,"day11":0,"month11":0,"day12":0,"month12":0,"d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"d6":0,"w6":0,"xday1":0,"xmonth1":0,"xday2":0,"xmonth2":0,"xday3":0,"xmonth3":0,"xday4":0,"xmonth4":0,"xday5":0,"xmonth5":0,"xday6":0,"xmonth6":0,"xday7":0,"xmonth7":0,"xday8":0,"xmonth8":0,"xday9":0,"xmonth9":0,"xday10":0,"xmonth10":0,"xday11":0,"xmonth11":0,"xday12":0,"xmonth12":0,"xd1":0,"xw1":0,"xd2":0,"xw2":0,"xd3":0,"xw3":0,"xd4":0,"xw4":0,"xd5":0,"xw5":0,"xd6":0,"xw6":0,"suspend":false,"random":false,"randon1":false,"randoff1":false,"randon2":false,"randoff2":false,"repeat":true,"atstart":true,"odd":false,"even":false,"x":120,"y":312,"wires":[["d8ed835e6cfce90f"],[],[]]},{"id":"d8ed835e6cfce90f","type":"api-call-service","z":"b896509c.caf71","name":"Christmas Lights","server":"c879ac61.99cd1","version":3,"debugenabled":false,"service_domain":"switch","service":"{{payload}}","entityId":"switch.sonoff_10012b7d75, switch.sonoff_10012ef410, switch.sonoff_re5v1c","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":490,"y":264,"wires":[[]]},{"id":"c879ac61.99cd1","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]
1 Like

I have simplified how I am using this as part of my reticulation/water routine. Going to expand the automation for seasonal lights in the near future.

Comments have been included in the YAML code below

I had a similar problem for turning on the christmas lights. Came up with the following solution prior finding this thread ,-). I leave this here to my future self … in case I need to find it in one year.

  condition:
    - condition: template
      alias: "Only during Advent and till two weeks after christmas"
      value_template: |-
        {{ -14 <= (now().replace(month=12,day=24,hour=20) - now()).days <= 25 }}

2 Likes

If anyone wants to have a bunch of binary sensor entities for common date evaluations, just add these to your templates YAML.

    # Months
    - name: Is January
      state: >
        {{ now().month == 1 }}

    - name: Is February
      state: >
        {{ now().month == 2 }}

    - name: Is March
      state: >
        {{ now().month == 3 }}

    - name: Is April
      state: >
        {{ now().month == 4 }}

    - name: Is May
      state: >
        {{ now().month == 5 }}

    - name: Is June
      state: >
        {{ now().month == 6 }}

    - name: Is July
      state: >
        {{ now().month == 7 }}

    - name: Is August
      state: >
        {{ now().month == 8 }}

    - name: Is September
      state: >
        {{ now().month == 9 }}

    - name: Is October
      state: >
        {{ now().month == 10 }}

    - name: Is November
      state: >
        {{ now().month == 11 }}

    - name: Is December
      state: >
        {{ now().month == 12 }}

    # Days of Week
    - name: Is Monday
      state: >
        {{ now().isoweekday() == 1 }}

    - name: Is Tuesday
      state: >
        {{ now().isoweekday() == 2 }}

    - name: Is Wednesday
      state: >
        {{ now().isoweekday() == 3 }}

    - name: Is Thursday
      state: >
        {{ now().isoweekday() == 4 }}

    - name: Is Friday
      state: >
        {{ now().isoweekday() == 5 }}

    - name: Is Saturday
      state: >
        {{ now().isoweekday() == 6 }}

    - name: Is Sunday
      state: >
        {{ now().isoweekday() == 7 }}

    # Special Days
    - name: Is First Day of Month
      state: >
        {{ now().day == 1 }}

    - name: Is 15th Day of Month
      state: >
        {{ now().day == 15 }}

    - name: Is Last Day of Month # Credit: https://stackoverflow.com/a/13565185
      state: >
        {%- set next_month = now().replace(day=28) + timedelta(days=4) -%}
        {%- set last_day = next_month - timedelta(days=next_month.day) -%}
        {{ last_day == now().day }}

    # Holidays
    - name: Is New Years Day
      state: >
        {{ now().month == 1 and now().day == 1 }}

    - name: Is Groundhogs Day
      state: >
        {{ now().month == 2 and now().day == 2 }}

    - name: Is Valentines Day
      state: >
        {{ now().month == 2 and now().day == 14 }}

    - name: Is April Fools Day
      state: >
        {{ now().month == 4 and now().day == 1 }}

    - name: Is Earth Day
      state: >
        {{ now().month == 4 and now().day == 22 }}

    - name: Is May Day
      state: >
        {{ now().month == 5 and now().day == 1 }}

    - name: Is Cinco De Mayo
      state: >
        {{ now().month == 5 and now().day == 5 }}

    - name: Is Flag Day
      state: >
        {{ now().month == 6 and now().day == 14 }}

    - name: Is Juneteenth
      state: >
        {{ now().month == 6 and now().day == 19 }}

    - name: Is Independence Day
      state: >
        {{ now().month == 7 and now().day == 4 }}

    - name: Is Veterans Day
      state: >
        {{ now().month == 11 and now().day == 11 }}

    - name: Is Christmas
      state: >
        {{ now().month == 12 and now().day == 25 }}

    - name: Is New Years Eve
      state: >
        {{ now().month == 12 and now().day == 31 }}
2 Likes

I was inspired by Rak’s solution for X-mas lights. However, it was not resolving to TRUE in the first days of January. So I have changed it to:

alias: Only some days before and after X-mas
condition: template
value_template: >-
  {{ (now().month == 1 and now().day < 7) or (now().month == 12 and now().day > 10) }}

5 years gone and still no solution for a date?
I just tried the “time” condition given from hass (without reading the docs: my fault, as it is only comparing the day time and workday and special days, but no dates)

Couldn’t this be enhanced to also look at the date? If 0 is given for year … than it’s not compared? Or duplicate the time condition as “date”?

1 Like

Hello everyone, I had the same needs, and took your insights to create a pull request to add two new conditions: date and datetime.
This way, we would be able to choose to compare “only the date”, “only the time” or the “date and time” !

Please take a look at it Add date and datetime conditions by Passific · Pull Request #128009 · home-assistant/core · GitHub

I just used ChatGPT to generate some sensors.

template:
  - binary_sensor:
      # New Year's Day
      - name: New Year's Day
        unique_id: new_years_day
        state: >
          {{ now().strftime('%Y-%m-%d') == (now().year | string) + "-01-01" }}
      # Valentine's Day
      - name: Valentine's Day
        unique_id: valentines_day
        state: >
          {{ now().strftime('%Y-%m-%d') == (now().year | string) + "-02-14" }}
      # St. Patrick's Day
      - name: St. Patrick's Day
        unique_id: st_patricks_day
        state: >
          {{ now().strftime('%Y-%m-%d') == (now().year | string) + "-03-17" }}
      # Easter (calculated dynamically)
      - name: Easter
        unique_id: easter
        state: >
          {% set a = now().year %}
          {% set d = (((a * 19) % 19) + 15) % 30 %}
          {% set e = ((a + (a // 4) + d + 2 - (a // 100) + ((a // 100) // 4)) % 7) %}
          {% set day = 22 + d + e %}
          {% if day > 31 %}
            {{ now().strftime('%Y-%m-%d') == (a | string) + "-04-" + (day - 31) | string }}
          {% else %}
            {{ now().strftime('%Y-%m-%d') == (a | string) + "-03-" + day | string }}
          {% endif %}
      # Independence Day
      - name: Independence Day
        unique_id: independence_day
        state: >
          {{ now().strftime('%Y-%m-%d') == (now().year | string) + "-07-04" }}
      # Halloween
      - name: Halloween
        unique_id: halloween
        state: >
          {{ now().strftime('%Y-%m-%d') == (now().year | string) + "-10-31" }}
      # Thanksgiving (4th Thursday of November)
      - name: Thanksgiving
        unique_id: thanksgiving
        state: >
          {% set nov1 = strptime((now().year | string) + "-11-01", "%Y-%m-%d") %}
          {% set offset = (3 - nov1.weekday()) % 7 %}
          {% set thanksgiving = nov1 + timedelta(days=(offset + 21)) %}
          {{ now().strftime('%Y-%m-%d') == thanksgiving.strftime('%Y-%m-%d') }}
      # Christmas
      - name: Christmas
        unique_id: christmas
        state: >
          {{ now().strftime('%Y-%m-%d') == (now().year | string) + "-12-25" }}

Worked perfect