Lovelace conditional card, based on time

All,
Is there a way to do this based upon day of week and month? Specifically, I want my Christmas Season to be after Thanksgiving, which would be the 4th Friday {after 4th Thursday, Thanksgiving) in November? I have a card in my mushroom dashboard that is only active at Christmas time, so I want to automate that date.

Currently I’m using the template above and manually setting the date, but the point of automation is to do it automatically.

Looking for this answer, as well. Did you ever figure it out?

Here is a formula for calculating Thanksgiving Day in the current year.

{% set current_year= now().year %}
{% set day_nov_starts = as_datetime( current_year ~ "-11-01").isoweekday() %}
{% if day_nov_starts > 4 -%} {# case that November starts on Friday, Saturday, or Sunday #}
{% set Thanksgiving_day = 33 - day_nov_starts %}
{%- else -%} {# case that November starts on Monday to Thursday #}
{% set Thanksgiving_day = 26 - day_nov_starts %}
{%- endif %}
{% set Thanksgiving = as_datetime( current_year ~ "-11-" ~ Thanksgiving_day) %}

I hope it can serve your needs. :christmas_tree:

Hi!

I have made a binary sensor for christmas time, here christmas is between first advent sunday (the sunday f weeks before christmas) and the thirteens day ( 6th of january) and it works well, maybe it can be done a bit mor elegant.

    - name: "christmas_time"
      unique_id: 88bbd5ca-71ce-42c6-b8bd-66950579ab2e
      state: >
        {%- set week, day = 4, 6 %}
        {%- set today = now().date() %}
        {%- set christmaseve = today.replace(month=12, day=24) %}
        {%- set christmasday = christmaseve + timedelta(days=1) %}
        {%- set epiphany = today.replace(month=1, day=6) %}
        {%- set temp = christmasday %}
        {%- set adjust = (day - christmasday.weekday()) % 7 %}
        {%- set temp = temp + timedelta(days=adjust) %}
        {%- set firstadventsunday = temp - timedelta(weeks=week) %}
        {%- set secondadventsunday = temp - timedelta(weeks=week-1) %}
        {%- set thirdadventsunday = temp - timedelta(weeks=week-2) %}
        {%- set fourthadventsunday = temp - timedelta(weeks= week-3) %}
        {{ today <= epiphany or today >= firstadventsunday }}

I would want all the different days to be given as attributes to the binary sensor. I tried to just add attributes like this

    - name: "christmas_time"
      unique_id: 88bbd5ca-71ce-42c6-b8bd-66950579ab2e
      state: >
        {%- set week, day = 4, 6 %}
        {%- set today = now().date() %}
        {%- set christmaseve = today.replace(month=12, day=24) %}
        {%- set christmasday = christmaseve + timedelta(days=1) %}
        {%- set epiphany = today.replace(month=1, day=6) %}
        {%- set temp = christmasday %}
        {%- set adjust = (day - christmasday.weekday()) % 7 %}
        {%- set temp = temp + timedelta(days=adjust) %}
        {%- set firstadventsunday = temp - timedelta(weeks=week) %}
        {%- set secondadventsunday = temp - timedelta(weeks=week-1) %}
        {%- set thirdadventsunday = temp - timedelta(weeks=week-2) %}
        {%- set fourthadventsunday = temp - timedelta(weeks= week-3) %}
        {{ today <= epiphany or today >= firstadventsunday }}
      attributes:
        epiphany : >
          {{ epiphany }}
        firstadventsunday : >
          {{ firstadventsunday  }}
        secondadventsunday : >
          {{ secondadventsunday }}
        thirdadventsunday : >
          {{ thirdadventsunday }}
        fourthadventsunday : >
          {{ fourthadventsunday }}
        christmaseve : >
          {{ christmaseve  }}
        christmasday : >
          {{ christmasday  }}

But there isn’t any info in the attributes, it looks like this:

How do i get the info to the attributes? Do i have to do the calcualtions for each attribute or can it be done in som other mor elegant way?

Thank you all for your help!

Hi, not directly answering your question but, why do you not use the local calendar integration instead? For fixed dates it runs year after year, for the rest you do it ones per year. I use that for my christmas lighting, halloween, easter and all the other special days. I even put special events there to be spoken by TTS during the day.

You could do something like that:


      attributes:
        {%- set week, day = 4, 6 %}
        {%- set today = now().date() %}
        {%- set christmaseve = today.replace(month=12, day=24) %}
        {%- set christmasday = christmaseve + timedelta(days=1) %}
        {%- set epiphany = today.replace(month=1, day=6) %}
        {%- set temp = christmasday %}
        {%- set adjust = (day - christmasday.weekday()) % 7 %}
        {%- set temp = temp + timedelta(days=adjust) %}
        {%- set firstadventsunday = temp - timedelta(weeks=week) %}
        {%- set secondadventsunday = temp - timedelta(weeks=week-1) %}
        {%- set thirdadventsunday = temp - timedelta(weeks=week-2) %}
        {%- set fourthadventsunday = temp - timedelta(weeks= week-3) %}

        {%- set festivities = {
               'Christmas Eve': christmaseve,
               'Christmas Day': christmasday,
               and so on
              }
            %}
        {{ festivities }}

You can call single events by using


{{ festivities['Christmas Day'] }}

Thank you!

So i do need to do the same calculation twice. Ones under the state: section and again under attributes: section. Ihoped there was a way to only do the calculations once. But hey, you can’t always get what you want. :slight_smile:

But instead i got a great explenation of how to do it.

Thank you!

UPDATE!

It sadly didn’t work.

i got this error in the log.

I added this:

      attributes: >
        {%- set week, day = 4, 6 %}
        {%- set today = now().date() %}
        {%- set christmaseve = today.replace(month=12, day=24) %}
        {%- set christmasday = christmaseve + timedelta(days=1) %}
        {%- set epiphany = today.replace(month=1, day=6) %}
        {%- set temp = christmasday %}
        {%- set adjust = (day - christmasday.weekday()) % 7 %}
        {%- set temp = temp + timedelta(days=adjust) %}
        {%- set firstadventsunday = temp - timedelta(weeks=week) %}
        {%- set secondadventsunday = temp - timedelta(weeks=week-1) %}
        {%- set thirdadventsunday = temp - timedelta(weeks=week-2) %}
        {%- set fourthadventsunday = temp - timedelta(weeks= week-3) %}
        {%- set festivities = {
               'Christmas Eve': christmaseve,
               'Christmas Day': christmasday
              }
            %}
        {{ festivities }}

I tried with both

attributes:

and with

attributes: >

Neither did work.

Do I have some error in my code maybe?

Thank you!

That’s absolutely a way to do it. I will check that too. I also wanted to learn how to do it and thought that a christmas binary sensor would be a great sensor to try with.

I was looking for the same and came multiple times on this topic. It’s a few years old so i thought i post an update on how it’s by done now. People who find this post through Google can use the most efficient way.

  1. Create an input helper: schedule with the times when it should been displayed.
  2. On the dashboard use the conditional card
  3. On the conditional card add the condition: “Entity state” and select your input helper.

This should do the trick.

AWESOME when a system keeps updating and makes it everytime easyer to use.

1 Like