Easy Time Macros for Templates!

getting this when enabling experimental:

all frontend elements except themes do not load.

image

no issues when I roll back to non-experimental…

After you enable experimental, you have to restart and clear your cache

1 Like

had already restarted but forgot to empty cache and hard reload. All good, Thanks for the tip!

Also love the look and feel of the list/filters (unless it was available in non-experimental and I missed it…)

1 Like

I have problems to compare to count_the_days output. Most probably there is something obviously, what I don’t see, but what?

This is working:

{% from 'easy_time.jinja' import count_the_days %}
{% set tage = count_the_days('2023-12-19')|int(default=0) %} 
{% if tage == 1 %}
  {% set tage = 'Morgen' %} 
{% endif %}
{{ tage }}

and returns “Morgen”.

But when I don’t want to int the return value, a compare is not working.

{% from 'easy_time.jinja' import count_the_days %}
{% set tage = count_the_days('2023-12-19') %} 
{% if tage == '1' %}
  {% set tage = 'Morgen' %} 
{% endif %}
{{ tage }}

returns “1”. Same für tage == 1

Why is the string compare not working here? Something hidden in the return value?

are you running an older version? There was a bug where count_the_days was returning 1\n where the \n is a carriage return.

1 Like

Yes. This was it. Working now. So my guess about “hidden” artifacts wasn’t that wrong. Thanks for the quick reply and hint.

Yeah, it bit me in the butt for quite sometime because I use that calc in other spots! It’s fresh in my mind.

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!.

1 Like

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())}}