Holiday Lights

Using the below to turn on the Christmas Light Season sensor. It will turn on the day after Thanksgiving. Every year I need to enter the date for Thanksgiving as shown below.

Do you have any suggestions to make it do this automatically?

binary_sensor:
  # Christmas Light Season
  - platform: template
    sensors:
      christmas_light_season:
        friendly_name: Christmas Light Season
        value_template: >
          {% set today = states('sensor.date').split('-') %}
          {% set month = today[1]|int %}
          {% set day = today[2]|int %}
          {{ month == 11 and day > 25 or month == 12 }}
1 Like

I use this, it keeps the lights on between the day after thanks giving and jan 15th

  - 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 jan15th = temp.replace(month=1, day=15) %}
          {{ today <= jan15th or today > thanksgiving }}

This doesn’t turn them on jan 1st +

  - 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 jan1 = temp.replace(month=1, day=1) %}
          {{ today < jan1 or today > thanksgiving }}

Awesome! Thanks!!!

Question! How would i modify your sensor to turn them off the first saturday AFTER new years day?

  - 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 }}
2 Likes

Amazing, thank you! Now to learn what all this means so i can use it for other ideas!

Sorry to necro the thread, how would I adjust this for additional seasons? I tried to comprehend, but its so elegant, I couldn’t reverse engineer it.

Specifically, the last Saturday of September through (but also including) Halloween night. (Halloween Season)?

For anyone as lost as I was, install the “Templates” integration, create a new integration for “Christmas lights season” and paste the code from “value_template” into the “State template” box.

Here’s a more readable (imo) version. I want the last night of Christmas lights to be New Years Day… and I still want them to switch off on the morning of Jan 2. So I set the end date to be noon on Jan 2 and the start date is noon on Thanksgiving (USA).

{# https://community.home-assistant.io/t/301306/8 #}
{% set now = now() -%}
{# https://stackoverflow.com/a/78339774 #}
{% set days = namespace(nov=[]) -%}
{% for n in range(1,30) -%}
  {% set d = now.replace(now.year,11,n,12) -%}
  {% if d.weekday()==3 -%}
    {% set days.nov = days.nov + [d] -%}
  {% endif -%}
{% endfor -%}
{% set thanksgiving = days.nov[3] -%}
{% set jan2 = now.replace(now.year,1,1,12) -%}
{{ not jan2 <= now <= thanksgiving }}

You can use easy_time and just do

{% from 'easy_time.jinja' import month_week_day, month_day %}
{{ month_day(1, 2) | as_datetime <= now() <= month_week_day(11, 4, 3) | as_datetime }}
1 Like

That should be “not” between Jan 2 and Thanksgiving.

{% from 'easy_time.jinja' import month_week_day, month_day %}
{{ not month_day(1, 2) | as_datetime <= now() <= month_week_day(11, 4, 3) | as_datetime }}