Date countdown based on calender item

Is there a way that I can do this with Home Assistant? I want to create a date countdown based on what is in the agenda. so a day counter until the date/moment in the agenda

It’s nice to have insight into how many days it will take until the holiday starts, for example

I think this service can be used to find the specific ‘holiday date’
Calendar.list_events

Or a date helper can also work I guess?
Input_datetime.test

And an input textbox that contains the output: days until the holiday

Hm not sure how to start?

Something like this

type: custom:mushroom-template-card
primary: >-
  {{ (as_timestamp(states.calendar.calendar.attributes.end_time) -
  as_timestamp(now())) | timestamp_custom("%H hours and %M min") }}
secondary: ''
icon: mdi:timelapse


1 Like

How does it work? I need to define the start date of the event somewhere right?

Yes you can use a helper date that you can create in tue UI

Ok I’ve got a new date sensor, how can I integrate it into your code?

use it instead of the calendar.attributes.end_time. Try it out in the dev tools :slight_smile:

I get an error after the change
termplate:
{{ (as_timestamp(states.input_datetime.datumtest) - as_timestamp(now())) |
timestamp_custom(“%H hours and %M min”) }}

Error:
ValueError: Template error: as_timestamp got invalid input ‘<template TemplateState(<state input_datetime.datumtest=2024-01-13; has_date=True, has_time=False, editable=True, year=2024, month=1, day=13, timestamp=1705100400.0, friendly_name=datumtest @ 2023-12-05T08:32:50.142417+01:00>)>’ when rendering template ‘{{ (as_timestamp(states.input_datetime.datumtest) - as_timestamp(now())) | timestamp_custom(“%H hours and %M min”) }}’ but no default was specified

strange because it’s not recognizes the date.
image

Did you figure out how to make this work? I’d like to use a countdown to a specific date too.

Yes
this one works:

make a input datetime helper with the date and then put this code into a template card:
Days {{ (strptime(states('input_datetime.test'), '%Y-%m-%d', today_at()) | as_local - today_at()).days }}

Thanks. I decided to use the Anniversaries (HACS) integration and it works fine.

Hi Marco, Tried this and it works, however calculates the days wrong.

I have this in my template

{{ state_attr('calendar.f1_calendar','start_time') }}

{{ (as_timestamp(states.calendar.f1_calendar.attributes.start_time) -
   as_timestamp(now())) | timestamp_custom("%d days and %H hours and %M min") }}

{{ (as_timestamp(states.calendar.f1_calendar.attributes.start_time) -
   as_timestamp(now())) / 60 / 60 / 24}}

and I get this result

2024-03-02 15:00:00

07 days and 05 hours and 00 min

6.167015774363168

Not sure why it does calculate 7 days, when in fact it is only 6, It does the same for the hours and mnutes. Any idea?

The date/time in the calendar is in a different timezone than your HA instance is setup in.
You have to convert now() to the same timezone as the calendar. The calendar will be in UTC and you are in UTC+1 I guess?

Fits perfectly for the hour difference, I was not aware of this. Will look into converting.

How about the “days” being one to many. Might be it think im 23hours wrong, or any other good idea.

1 Like

Because timestamps are not meant really meant to calculate durations like that. The %d option of timestamp_custom reflects the numerical day of the date calculated by the timestamp math you performed, i.e. 6.167 days after Jan 1, 1970 at 00:00:00 UTC.

Your timestamp math converts to the datetime string 1970-01-07 04:00:30.162905+00:00. So 7 is correct for %d.

There are a couple custom macros available for calculating relative times and performing other time-related functions:

RelativeTime Plus
Easy Time Jinja

2 Likes

Thanks for the explanation. I will go for some simple math and a round(0).

1 Like