Easy Time Macros for Templates!

Thank you for the nice library.
This is a really good idea.

I struggled quite a bit to install it using hacs as it was not showing. Until I went through the full thread here to find that I had to set HACS in experimental mode to see the Easy Time integration.

My browser is in ‘fr’, my OS (Win10) is in ‘fr’, my homeassistant instance is set to ‘fr’ and my user profile is set to ‘fr’.

Once installed, it took me a while to know that I had to edit the easy_time.jinja and hardcode my default_language in there to ‘fr’.
It probably mean that I’ll have to redo it at every update of your library ? Am I right ?

Let say that my son prefers ‘en’ and set this language in his ha profile.
Couldn’t we use the same template to display in ‘fr’ for me and in ‘en’ for him ?

Sorry if this all sounds silly… I did my best…

That’s only possible if you provide the language argument to macros based on the user. It won’t work for the frontend, but it would work for notifications.

Hi @petro , thanks for this Macro. For some reason, 12/31/23 returns ‘last Tuesday’ even though it was a Sunday. This happens for any date 12/26-12/31 it seems.

{% from 'easy_time.jinja' import speak_the_days %}
{{ speak_the_days("2023-12-31 00:00:00") }}

Result type: string
last Tuesday

Yeah, there seems to be an issue with the code, it will probably clear up on it’s own next week. I’ll have to take a look into this.

I have a couple of questions on this tool:

Is there a way of only showing the date and not the time when using as_datetime? For example, on the next DST, it works great but how can I only show the date?

You have an example on how to obtain Easter or Thanksgiving date. We know that those holidays are always on the same day of the week, but is there a way of showing the day of the week for a specific date such as what day of the week will July 4th or December 25th be?

Not really about this macro, but nonetheless:

{{ ("2024-02-26 11:23:45" | as_datetime).date() }}

The above will be according to your locale settings.

Or for custom control:

{{ ("2024-02-26 11:23:45" | as_datetime).strftime("%Y-%m-%d") }}

See The EPIC Time Conversion and Manipulation Thread!.

2 Likes

you can apply what Pieter said w/ easy_time too

{% from 'easy_time.jinja' import easter %}
{{ (easter() | as_datetime).date() }}

But if you want to use these things for automations, you should really just use the output from the template as-is, that’s how it’s designed.

1 Like

Thank you both - that works perfectly. On the second item, is there a way to show the day of the week for a future date such as 12/25/2024 (which day Christmas falls on)?

You can do…

{% from 'easy_time.jinja' import weekday %}
{{ weekday(('2024-12-25' | as_datetime).isoweekday()) }}

Or if you want one to work every year…

{% from 'easy_time.jinja' import weekday, month_day %}
{{ weekday((month_day(12,25) | as_datetime).isoweekday()) }}

or broken apart…

{% from 'easy_time.jinja' import weekday, month_day %}
{% set christmas = month_day(12,25) | as_datetime %}
{{ weekday(christmas.isoweekday()) }}

Hi, still loving this set of macro’s and have a small question.
Why would one not import all macro’groups’ at once, instead of only the ones needed, is this performance related?

that is nice… now how do I get from a custom_template with my anniversaries to a useable input for that day template…

{% set vj_marijn = '1964-08-27'|as_datetime|as_local %}

I can use this:

{% from 'feestdata.jinja' import vj_marijn %}
{% set event = vj_marijn %}
{% set datum = event.strftime('%-m,%d') %}
{{datum}}

which outputs 8,27, but when I add it in the template you showed above,

{% from 'easy_time.jinja' import weekday, month_day %}
{{ weekday((month_day(12,25) | as_datetime).isoweekday()) }}

and do

{% from 'easy_time.jinja' import weekday, month_day %}
{{ weekday((month_day(datum) | as_datetime).isoweekday()) }}

it says

TypeError: ‘str’ object cannot be interpreted as an integer

ofc, I can not |int it either

You know the answer to this. vj_marijn is a datetime object, how do you get month and day out of it as an integer? You’ve used this many times.

yeah, I guess

{% from 'easy_time.jinja' import weekday, month_day %}
{{ weekday((month_day(vj_marijn.month,vj_marijn.day) | as_datetime).isoweekday()) }}

works ok.

or, shorter:

{% from 'easy_time.jinja' import weekday, month_day %}
{{ weekday((month_day(event.month,event.day) | as_datetime).isoweekday()) }}

it was probably me figuring I am overdoing things here.
Have this bigger template: with all sorts of attributes, and this month, day combo could be added there, and next use it in a template-entity-row card, where I currently do this:

card:
  type: custom:template-entity-row
  entity: '[[entity]]'
  name: >
    {% set dagen = states(config.entity)|int(default=-1) %}
    {% if dagen == 0 %}
    {{state_attr(config.entity,'id')}} is vandaag:
    {% else %}
    {{state_attr(config.entity,'id')}} wordt {{state_attr(config.entity,'leeftijd')
      |int(default=-1) + 1}} over:
    {% endif %}
  state: >
    {% set dagen = states(config.entity)|int(default=-1) %}
    {% if dagen == 0 %} Jarig!
    {% else %} {{states(config.entity)}} {{'dag' if over == 1 else 'dagen'}}
    {% endif %}
  secondary: >
    {% set event = state_attr(config.entity,'datum')|default(0,true)|as_datetime|as_local %}
    {{state_attr(config.entity,'type')}}: {{event.strftime('%d-%m-%Y')}}

and only reference the config.entity

so I can use eg:

  secondary: >
    {% set event = state_attr(config.entity,'datum')|default(0,true)|as_datetime|as_local %}
    {{state_attr(config.entity,'type')}}: {{event.strftime('%d-%m-%Y')}}
    {% from 'easy_time.jinja' import weekday, month_day %}
    op {{weekday((month_day(event.month,event.day)|as_datetime).isoweekday())}}

I’m currently just trying to convert minutes so that something comes out with x hours and x minutes.

However, something always comes up for me within weeks.

I didn’t fully understand what was wrong. I tried different marcros

{% from 'easy_time.jinja' import custom_time %}
      {{ custom_time(120, 'hour') }}

→ 476942 Stunden

  {% from 'easy_time.jinja' import custom_relative_time %}
   {{ custom_relative_time(120) }}

→ 838 Wochen, 6 Tagen, 14 Stunden, 25 Minuten und 39 Sekunden vor

These macros look for timestamps, timestrings, timedeltas, or entity_ids. So you have to provide one of those. A small integer (in seconds) is not supported, well it is, but it assumes it’s a timestamp. 120 is 120 seconds past january 1st 1970, not exactly what you’d be looking for.

1 Like

Depending on where you want to use this, you could also work with the template function time_until or time_since.

Not that I would want to discourage you from using the easy time macro, using it myself a lot, but sometimes templating is another way… :slight_smile:

Those also only work with datetimes or entity_id’s.

They sure do, like practically everything else in HA related to date, time and mathematical operations. What else would one use than a date/time related format?

Even if you have minutes or seconds, you can convert that to a timestamp and use it, or am I off here?

You’re off here, just try it

{{ time_since(120) }}
{{ time_until(120) }}

{{ time_since(now()-timedelta(seconds=120)) }}
{{ time_until(now()+timedelta(seconds=120)) }}
{{ relative_time(now()) }}

Sorry, you misunderstood or I wasn’t clear, I’m totally with you on that one aka. that’s what I’m saying or trying to say… :rofl: :rofl: