Condition to run action between 4th Friday of Nov. and Dec. 31st

I have a holiday lights automation that turns on various Zigbee lights for multiple holidays of the year. For Christmas, I currently use the local calendar integration as a condition to run a lighting script at sunset between November 29th (the last possible Friday after Thanksgiving that could occur) and December 31st. I would like to make this condition a little more dynamic and predict the day after Thanksgiving every year, which is always the fourth Friday in November. So, the condition should be “between the fourth friday of November and December 31st” instead of between November 29th and December 31st.

For reference, here is an excerpt of my lighting automation as it relates to Christmas lights (it controls other holidays as well):

alias: Holiday Auto Lights
description: Automates holiday lighting on front porch
triggers:
  - event: sunset
    id: sunset
    trigger: sun
  - event: sunrise
    offset: "-02:00:00"
    id: sunrise
    trigger: sun
  - entity_id:
      - calendar.holidays_in_united_states
    from: "on"
    to: "off"
    id: holiday ends
    trigger: state
  - entity_id:
      - calendar.local_holiday_calendar
    from: "on"
    to: "off"
    id: holiday ends
    trigger: state
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - sunset
          - condition: or
            conditions:
              - condition: state
                entity_id: calendar.holidays_in_united_states
                state: "on"
              - condition: state
                state: "on"
                entity_id: calendar.local_holiday_calendar
              - condition: state
                entity_id: sensor.holiday_date_tracker
                state: Tomorrow
        sequence:
          - choose:
              - conditions:
                  - condition: state
                    entity_id: calendar.local_holiday_calendar
                    attribute: message
                    state: Christmas Month
                  - condition: state
                    state: "on"
                    entity_id: calendar.local_holiday_calendar
                sequence:
                  - metadata: {}
                    data: {}
                    target:
                      entity_id: script.holiday_lights_christmas
                    action: script.turn_on
                alias: Christmas
        alias: Sunset/Activate Holiday Lights
      - conditions:
          - condition: trigger
            id:
              - sunrise
          - condition: or
            conditions:
              - condition: state
                entity_id: calendar.holidays_in_united_states
                state: "on"
              - condition: state
                state: "on"
                entity_id: calendar.local_holiday_calendar
              - condition: state
                entity_id: sensor.holiday_date_tracker
                state: Tomorrow
        sequence:
          - metadata: {}
            data: {}
            target:
              entity_id:
                - script.holiday_lights_christmas
            action: script.turn_off
          - metadata: {}
            data: {}
            target:
              entity_id:
                - light.front_porch_light
                - light.driveway_light
            action: light.turn_off
        alias: Sunrise Reset
      - conditions:
          - condition: trigger
            id:
              - holiday ends
        sequence:
          - delay:
              hours: 4
              minutes: 0
              seconds: 0
              milliseconds: 0
          - metadata: {}
            data: {}
            target:
              entity_id:
                - script.holiday_lights_christmas
            action: script.turn_off
          - metadata: {}
            data: {}
            target:
              entity_id:
                - light.front_porch_light
                - light.driveway_light
            action: light.turn_off
        alias: Holiday Ends Reset
mode: single

I believe this should work as thanksgiving.

{% set days = dict(mon=1, tue=2, wed=3, thu=4, fri=5, sat=6, sun=7).get("fri"|lower, 1) - as_datetime( now().year ~ "-12-01").isoweekday() | int +1 %}
{{ as_datetime(now().year ~ "-12-01")  - timedelta(days = iif(days <= 0, days+7, days)) }}

Hi stalkingturkey,

Here’s what I do.
Runs from November 1 until the 12th day of Christmas (12 days after Christmas)

Or… Another automation to disable the lights automation on 1 January.

The following Template Condition evaluates to true when the current date is between the fourth Friday in November and December 31st. Otherwise, it reports false.

conditions:
  - condition: template
    value_template: >
      {% set fourth = now().date().replace(month=11, day=22) %}
      {% set w = fourth.isoweekday() %}
      {% set fourth = fourth if w == 5 else fourth.replace(day=(22+(5-w)%7)) %}
      {{ fourth <= now().date() <= now().date().replace(month=12, day=31) }}

EDIT

If you want to test the template’s logic, copy-paste the following version into the Template Editor. Change the year/month/day values in variable t and observe the results.

{% set t = now().date().replace(year=2027, month=11, day=30) %}
{% set fourth = t.replace(month=11, day=22) %}
{% set w = fourth.isoweekday() %}
{% set fourth = fourth if w == 5 else fourth.replace(day=(22+(5-w)%7)) %}
4th Friday: {{ fourth }}
In range: {{ fourth <= t <= t.replace(month=12, day=31) }}