Daylight savings template condition

Hi, i’m trying to create a template that will serve as a reminder to change our clocks when daylight savings ends in november. I only want the template to show when we are within 11 days (or whatever time period i decide) of it ending.

This year day light ends on nov 3, so i used the below code however it does not seem to work. The template continues to be visible at all times. Any suggestions?
TYIA

type: vertical-stack
cards:

  • type: custom:mushroom-template-card
    primary: “”
    secondary: >-
    Daylight Savings is over in {{ (strptime(‘11/03/2024’, ‘%m/%d/%Y’,
    today_at()) | as_local - today_at()).days }} days … don’t forget to
    change your clock {{user}}!
    icon: “”
    multiline_secondary: true
    visibility:
  • condition: template
    value_template: “{{ (10, 23) <= (now().month, now().day) <= (11, 3) }}”

Hi Michael,

Thanks for coming here and asking a question.
Would you be so kind as to adjusting the format of your code so that we can read it properly & check the YAML spacing, etc. It is very hard for us to tell what is what when the text formatter jumbles everything like that.
Use the </> button or this:
Here is an example of how to fix it from the site FAQ Page.
How to help us help you - or How to ask a good question.

sorry about that @Sir_Goodenough

type: vertical-stack
cards:
  - type: custom:mushroom-template-card
    primary: ""
    secondary: >-
      Daylight Savings is over in {{ (strptime('11/03/2024', '%m/%d/%Y',
      today_at()) | as_local - today_at()).days }} days ... don't forget to
      change your clock {{user}}!
    icon: ""
    multiline_secondary: true
visibility:
  - condition: template
    value_template: "{{ (10, 23) <= (now().month, now().day) <= (11, 3) }}"

1 Like

The visibility section is under the vertical stack with the current indentation, but vertical stack have no option for visibility.

It looks like the mushroom-template-card do not have such one option either.

You might have to use a conditional card, but I do not think you can use a value_template there, so your value_template might have to be put in a sensor template, which can then be referenced in the conditional card.

You may also want to take a look at the options available through Easy Time Macros - Daylight Saving.

You can set up a Template sensor that uses the days_until_dst function so you have an entity that can work with a Conditional Card or card visibility in other cards.

Example using Markdown Card with visibility
type: markdown
content: >-
  {%- from 'easy_time.jinja' import days_until_dst,next_dst_phrase -%}
  {%- set phrase = next_dst_phrase() -%}
  {% set gain = phrase is search('gain') %}
  Daylight Saving is {{'over' if gain else 'starting'}} in {{ days_until_dst() }} days.
  The clocks will need to {{'fall back' if gain else 'spring forward'-}}
  ... don't forget to change them, {{user}}!
title: "Notice: Upcoming Time Change"
visibility:
  - condition: numeric_state
    entity: sensor.days_until_time_change
    below: 4

thats great, very helpful. little bummed that i can’t have the card only visible within a week or so of a date, but this is good enough!

does this HACs make your HA instance slow? i noticed some differences in speed.

when creating the sensor, do you know what i would need to populate in each of the fields so i can get the markdown example you shared to work? sorry

strong text

Once you have installed the macro it should look like:

I see no reason why you can not make a template with your original value template and then use a state condition in visibility section instead of a numeric condition.

How do I uninstall this? lol sorry it’s making my app so slow

The vertical-stack card does support visibility. And the following in Dev Tools produces the expected output, so I think your template is good:

{{ (10, 23) <= (now().month, now().day) <= (11, 3) }}
{{ (10, 23) <= (10, 22) <= (11, 3) }}
{{ (10, 23) <= (11, 4) <= (11, 3) }}

But if I create a visibility using a template condition with anything in it, it is always visible. For example:

condition: template
value_template: "{{ (1 == 2) }}"

Whereas if I put your code in a template sensor helper, then have visibility based on that, it does work. Therefore I would call it a bug. However the bug may just be that nothing is telling you condition: template is not supported - having the code there (working as expected or not) and refreshing the screen causes many many uncaught exceptions in the browser:

Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'layout')
    at 39594.n2cd3-JQQQQ.js:7:20348
    at o.ct (88859.6pkgdkycQT4.js:2:80055)
    at o.update (88859.6pkgdkycQT4.js:2:80200)
    at o._$AS (app.90wKv9r7Ny0.js:2:159969)
    at N (app.90wKv9r7Ny0.js:2:163273)
    at R._$AI (app.90wKv9r7Ny0.js:2:164546)
    at I.v (app.90wKv9r7Ny0.js:2:164014)
    at R.g (app.90wKv9r7Ny0.js:2:165227)
    at R._$AI (app.90wKv9r7Ny0.js:2:164686)
    at B (app.90wKv9r7Ny0.js:2:168048)

So the workaround is just to use visibility based on a binary sensor that contains your template. I’d probably alternatively do the binary sensor differently though, because having this trigger once a minute for every day of the year feels weird.

@michaelblight super helpful. Thanks for the feedback. Any suggestions on how to write the code differently?

I think you are right.
It looks like the visibility is turned off on the documentation of that attribute though. :laughing:

Actually, you can: Define an input_boolean helper sensor (Input boolean - Home Assistant) that indicates if the card should be visible and use that as a condition for the conditional card. Next create an automation that sets and resets the boolean helper, using your statement as a condition. trigger the automation dayly using a time_pattern

oh boy, you guys are too smart for me. let me give it the old college try!

Thanks for the feedback/suggestions.

You can also define the boolean helper as a template sensor and add the automation code in it like this:

  template:
  - binary_sensor:
      unique_id: 2398674
      name: "daylight_saving_warning"
      state: >
            {{ ((strptime('11/03/2024', '%m/%d/%Y', today_at()) | as_local - today_at()).days) < 4 }}         
    trigger:
      - platform: state
        hours: "/24"

That’s really great @pdwonline. This worked like a charm, thanks so much everyone for the suggestions/tweaking!

You can also just use easy_time, create a template entity and build reminders off that. It will never require configuration changes either because it searches for the daylight savings time change.

{% from 'easy_time.jinja' import days_until_dst %}
{{ days_until_dst() }} 

It counts down to 0. So if you want to trigger 7 days ahead of time, you’d use a trigger on the template sensor.

triggers:
- trigger: time
  at: "10:00"
conditions:
- condition: state
  entity_id: sensor.<your_daylight_savings_countdown>
  state: '7'
actions: 
...

If you want to use a dynamic date that is valid very year instead of a hardcoded one, and also do this for summertime, udse this instead:

{% set today = today_at() %}
{% set last_sunday_october = today.replace(month=10, day=31) - timedelta(days=today.replace(month=10, day=31).weekday() + 1) %}
{% set last_saturday_march = today.replace(month=3, day=31) - timedelta(days=(today.replace(month=3, day=31).weekday() + 2) % 7) %}
{{ (last_sunday_october - today).days < 4 and last_saturday_march > today
    or (last_saturday_march - today).days < 4 and last_saturday_march > today }}

That doesn’t work for this year in the states. Scratch that, it does. I thought it was the 10th for some reason.

However it won’t work for every country

The macro provided by easy_time searches for the DST regardless what country you’re in. It will always work, down to the minute/second that DST occurs.

I remember having to make it work that way because someone in a random country had DST change at 1:05 am, it was really odd. Anyways, it’s a pretty light weight macro and it should just work.