Creating Seasonal Templates?

I’ve seen and been given different seasonal templates. But I have no idea how they work even when studying them and couldn’t find a great example to learn either.

Is it possible to explain how to create a template that turns on the last Saturday of September and turns off the first Saturday of November?

I want to understand how to make my own and not rely on the goodness of others.

If you want the last Saturday in September:

  • Use now().month == 9 to specify September.
  • Use now().isoweekday() == 6 to specify Saturday.
  • September has 30 days so the last 7 days of September is a date greater than 23. The last Saturday can occur during those final 7 days. Use now().day > 23

You would use all three together in a Template Condition or Template Trigger or Template Sensor. Which to use depends on what you wish to do.

{{ now().month == 9 and now().day > 23 and now().isoweekday() == 6 }}

The same technique can be applied to determine the first Saturday of November.

{{ now().month == 11 and now().day < 8 and now().isweekday() == 6 }}
2 Likes

Thank you so much for explaining it!!

How would I combine them into a single template?

Something like this:

  - platform: template
    sensors:
      christmas_season:
        unique_id: christmas_season
        friendly_name: Christmas Season
        value_template: >
          {%- set month, week, day = 11, 4, 3 %}
          {%- set today = now().date() %}
          {%- set temp = today.replace(month=month, day=1) %}
          {%- set adjust = (day - temp.weekday()) % 7 %}
          {%- set temp = temp + timedelta(days=adjust) %}
          {%- set thanksgiving = temp + timedelta(weeks = week - 1) %}
          {%- set month, week, day = 1, 1, 5 %}
          {%- set temp = today.replace(month=month, day=1) %}
          {%- set adjust = (day - temp.weekday()) % 7 %}
          {%- set temp = temp + timedelta(days=adjust) %}
          {%- set firstsat = temp + timedelta(weeks = week - 1) %}
          {{ today < firstsat or today > thanksgiving }}
1 Like

Combine them how?

For example, if we do this, the template reports true for the two dates and false for every other date of the year.

{{ (now().month == 9 and now().day > 23 and now().isoweekday() == 6 ) or
  (now().month == 11 and now().day < 8 and now().isweekday() == 6) }}

It’s important to understand that the explanation I provided was creating a template that tells you if the current date is the last Saturday of September. It doesn’t calculate and report the date of the last Saturday of September.

So if your goal is to determine if the current date is between the last Saturday of September and the first Saturday in November, that’s a completely different requirement. That’s what the Christmas Season sensor you have does and you may have noticed it’s more complicated; it calculates the starting and ending dates in order to determine if the current date is between them.

My ultimate goal is to have a sensor that shows me if it is currently between the last Saturday in September and the first Saturday in November so my Halloween lights turn on or off automatically.

The Christmas sensor above does the same basic thing, but for Saturday after Thanksgiving to first Saturday in January. But I have no idea how it works.

Your explaination of how to determine if a day is a certain day is extremely helpful for understanding the process. But I’m lost on how to combine it all into a single template representing the timeframe.

It calculates the starting and ending dates. For example, this is the starting date:

{%- set month, week, day = 11, 4, 3 %}

I can explain how each line works but I am curious why you didn’t ask the person who created it?

I already asked them a bunch of questions and they trailed off and stopped answering, plus it was like 6 months ago. I felt it awkward to cold message then. So I made a general post in the forum.

If you mean this post, I don’t see any evidence of “trailed off and stopped answering”. Petro is a forum moderator and very helpful.

Just wanted to say I really appreciate your thorough answers !

1 Like

Petro is extremely helpful. I must have been mistaken on the he trailed off part. My apologies to both you and Petro for my mistake.