Easy Time Macros for Templates!

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:

Ah, I thought you ment “they sure do accept ints”

1 Like

Sorry, not my best english-is-not-my-native-language-day… :rofl: :rofl:

I’m trying to learn Italian at the moment, and I can safely say it doesn’t make my english better… :rofl: :rofl: Not to talk about my native language German, getting a mix out of english, german and italian is mostly funny for others… :rofl: :rofl:

1 Like

Thanks understood the issue :).

EDIT: @petro do you think also an option with capitalize make sense?

See idea from button card helpers:

Is there a way of calculating the time until each season change with this?

Yes, you can use one of the many functions. easy_time, count_the_days, easy_time_between, etc.

Thanks, but does easy_time “know” the date and time of the season changes?