Have I got this rigth?

I have coded up this binary sensor, but does it work?
Maybe someone kan be kind to interpret it, and then I will tell you what the intention was…
Just to NOT give you any hints :wink:

  - platform: template
    sensors:
      advent_time:
        friendly_name: "Adventtid"
        value_template: >-
          {% set n = now() %}
          {% set x = datetime.datetime(n.year, 12,3) %}
          {% set xweekday = x.isoweekday() %}
          {% if xweekday == 7 %}
          { set xweekday = 0 }
          {% endif %}
          {% set firstadvent = x - xweekday %} ## was wrong here x, not n!
          {{ (n.month > firstadvent.month) or (n.month == firstadvent.month and n.day >= firstadvent.day) or (1 == n.month and n.day <= 13) }}

Thank you in advance!

Hint: timedelta

How and why?

like this?

  - platform: template
    sensors:
      advent_time:
        friendly_name: "Adventtid"
        value_template: >-
          {% set n = now() %}
          {% set x = datetime.datetime(n.year, 12,3) %}
          {% set xweekday = x.isoweekday() %}
          {% if xweekday == 7 %}
          { set xweekday = 0 }
          {% endif %}
          {% set firstadvent = x - timedelta(days=xweekday) %}
          {{ (n.month > firstadvent.month) or (n.month == firstadvent.month and n.day >= firstadvent.day) or (1 == n.month and n.day <= 13) }}

So, I have struggled a lot, python is not the choice of mine…
But I come to this:

  - platform: template
    sensors:
      test_time:
        friendly_name: "atest"
        value_template: >-
          {% set n = now() %}
          {% set x = as_datetime("2021-12-03") %}
          {% set xweekday = x.isoweekday() %}
          {% if xweekday == 7 %}
          { set xweekday = 0 }
          {% endif %}
          {% set firstadvent = x - timedelta(days=xweekday) %}
          {{ (n.month > firstadvent.month) or (n.month == firstadvent.month and n.day >= firstadvent.day) or (1 == n.month and n.day <= 13) }}

Now it is the date in

          {% set x = as_datetime("2021-12-03") %}

I want to put it together with the year from n.
Something like str(n.year) + “-12-03”, but then I get error,
“Keep on struggling, keep on struggling”, oh no, it was jogging in that song…

Got it to work, probably it exists better way, but I’m learning…

  - platform: template
    sensors:
      advent_time:
        friendly_name: "Adventtid"
        value_template: >-
          {% set n = now() %}
          {% set y = n.year | string+"-12-03" %}
          {% set x = as_datetime(y) %}
          {% set xweekday = x.isoweekday() %}
          {% if xweekday == 7 %}
          { set xweekday = 0 }
          {% endif %}
          {% set firstadvent = x - timedelta(days=xweekday) %}
          {{ (n.month > firstadvent.month) or (n.month == firstadvent.month and n.day >= firstadvent.day) or (1 == n.month and n.day <= 13) }}

But what does it do? :slight_smile:

I think this should work too…

{% set today = now().date() %}
{% set x = today.replace(month=12, day=3) %}
{% set xweekday = x.isoweekday() %}
{% set firstadvent = x - timedelta(days=xweekday) %}
{{ firstadvent <= today <= today.replace(year= today.year+1, month=1, day=13)}}

It’s really only one line shorter than yours once you remove the “if” statement that isn’t doing anything.

It might, thank you for your time.
Here in Sweden we have four sundays prior to christmas. The last one can be on christmas eve, December 24 at latest. So the first Advent is at latest December 3.
Some christmas light are lighten up at First of Advent.
And the will do so until “tjugondagknut” :slight_smile: , very freely translated as “twenty-day-knut”.
It is twenty days after Christmas eve and is the names day of Knut.
At tjugondagknut you shake the christmas tree and you throw it out.
That is if you don’t already done that becaus it has lost most of its needles…

So what this does is to get Dec 3 current year, get its weekday, and then with help of isoweekday() moves to the correct sunday. Actually, the first of Advent occurs on the sunday prior to Dec 4.
That is why xweekday == 7 has to be checked (Dec 3 is a sunday).
This one will be on from first of advent, until Jan 13 :slight_smile:

I don’t think the code for this will ever change.

It’s a great day, I’ve learned something!