Easy Time Macros for Templates!

I updated the file manually. It works.
Thank you.

1 Like

given you have very useful translations here, I wonder if you would be interested in adding macros for weather related stuff. of course, actual weather conditions come to mind, but things like this would be useful (well they were for me, so I translated them…)

      - unique_id: wind_compass_abbreviation
        name: Windrichting Afkorting
        state: >
          {% set degrees = states('sensor.buienradar_wind_direction_azimuth')|float(0) %}
          {% set abbr = ['N','NNNO','NNO','ONNO','NO','NONO','ONO','OONO',
                         'O','OOZO','OZO','ZOZO','ZO','OZZO','ZZO','ZZZO',
                         'Z','ZZZW','ZZW','ZZW','ZW','ZWZW','WZW','WWZW',
                         'W','WWNW','WNW','NWNW','NW','WNNW','NNW','NNNW' ] %}
          {%- set i = ((degrees/11.25)|round(0))|int %}
          {%- set i = 0 if i == 32 else i %}
          {{abbr[i]}}

      - unique_id: wind_compass_direction
        name: >
          {% set degrees = states('sensor.buienradar_wind_direction_azimuth')|float(0) %}
          {% set abbr = states('sensor.wind_compass_abbreviation') %}
          {{abbr}} : {{degrees}} °
        state: >
          {% set degrees = states('sensor.buienradar_wind_direction_azimuth')|float(0) %}
          {% set direction = ['Noord','Noord Ten Oosten','Noordnoordoost',
                              'Noordoost Ten Noorden','Noordoost','Noordoost Ten Oosten',
                              'Oostnoordoost','Oost Ten Noorden','Oost','Oost Ten Zuiden',
                              'Oostzuidoost','Zuidoost Ten Oosten','Zuidoost',
                              'Zuidoost Ten Zuiden','Zuidzuidoost','Zuid Ten Oosten',
                              'Zuid','Zuid Ten Westen','Zuidzuidwest',
                              'Zuidwest Ten Zuiden','Zuidwest','Zuidwest Ten Westen',
                              'Westzuidwest','West Ten Zuiden','West','West Ten Noorden',
                              'Westnoordwest','Noordwest Ten Westen','Noordwest',
                              'Noordwest Ten Noorden','Noordnoordwest','Noord Ten Westen'] %}
          {%- set i = ((degrees/11.25)|round(0))|int %}
          {%- set i = 0 if i == 32 else i %}
          {{direction[i]}}

his might be a nice icon template to jot in:

      - unique_id: wind_bearing_icon
        name: Wind bearing icon
        state: >
          {% set bearing = states('sensor.buienradar_wind_direction_azimuth')|int(default=0) %}
          {% set icons = ['mdi:arrow-down','mdi:arrow-bottom-left','mdi:arrow-left',
                     'mdi:arrow-top-left','mdi:arrow-up','mdi:arrow-top-right',
                     'mdi:arrow-right','mdi:arrow-bottom-right'] %}
          {% set quadrant = (bearing/45)|round() %}
          {% if quadrant < icons|length %} {{icons[quadrant]}}
          {% else %} mdi:arrow-down
          {% endif %}

I’d make a separate lib for that. This one will be all about time. I have plans for other libs, I just want to get this to a point where there’s very little feature requests left.

sure.

what about an hour icon?

            {% set word = ['skipthis','one','two','three','four','five','six','seven',
                           'eight','nine','ten','eleven','twelve','one'] %}
            {%- set hour = now().strftime('%-I')|int %}
            {%- set minute = now().minute|int %}
            {%- set index = hour if minute <= 30 else hour + 1 %}mdi:clock-time-{{word[index]}}
              {{- '-outline' if states('sun.sun') != 'above_horizon' }}

also, and I might have missed it in the resource, dont you have a ‘Today sensor’ providing a full phrase like

Scherm­afbeelding 2023-04-14 om 14.17.54

        state: >
          {{states('sensor.dag')}} {{now().day}} {{states('sensor.maand')}} {{now().year}}

now ofc this is clumsily made up of other separates I have, but it is very useful in Markdown cards/template cards

1 Like

I take back what I said in the other post about the macros being a brilliant addition to HA albeit that I didn’t see a huge amount of actual utility in them. I love them.

I have updated my date time natural language macro a few post above to optionally return just the date or just the time. Again, I’m not saying it’s anything special but it’s there if it is any use to anyone.

PS. Is ther a way to reduce this? i.e. not have repeated lines for several ‘keys’?

  {%- set min_nl = 
    {
      0: hrs ~ " o'clock",
      1: 'a minute past ' ~ hrs,
      5: min ~ ' past ' ~ hrs,
      10: min ~ ' past ' ~ hrs,
      15: 'quarter past ' ~ hrs,
      20: min ~ ' past ' ~ hrs,
      25: min ~ ' past ' ~ hrs,
      30: 'half past ' ~ hrs,
      35: (60 - min) ~ ' to ' ~ (hrs + 1),
      40: (60 - min) ~ ' to ' ~ (hrs + 1),
      45: 'quarter to ' ~ (hrs + 1),
      50: (60 - min) ~ ' to ' ~ (hrs + 1),
      55: (60 - min) ~ ' to ' ~ (hrs + 1),
      59: 'a minute to ' ~ (hrs + 1)
    }
  %}

1.0.6 Release

New Features

  • Add a max period to easy_time, big_time, easy_relative_time, big_relative_time
  • Add input_datetime time only helpers
  • Added assumptions about calendar entities with easy and relative time macros

Bug Fixes & Other

  • Refactored the brains to pave the way towards timedeltas, duration sensors, timedelta formatting, countdowns, and differences between 2 ‘time’ inputs.

probably, but it’s not necessarily worth it.

One thing you made want to note is that changing Hacs to experimental makes the sensor.hacs unavailable. Took me awhile to figure out why my system monitor card suddenly stopped working.

1.0.7 Release

New Features

  • clock icon macro
  • days away counter (integer)
  • days away counter (speach)

New Languages

  • Italian

Fixes

  • Fixed issue with past French relative times
  • Various fixes and improvements
1 Like

like the new clock_icon :wink:

can I ask for some explanation though please? I remember having had a wrestle getting the hour hand to turn the clock correctly at 12 and had to do this

            {% set clock = ['skipthis','one','two','three','four','five','six','seven',
                           'eight','nine','ten','eleven','twelve','one'] %}
            {%- set hour = now().hour %}
            {%- set minute = now().minute %}
            {%- set hand = hour if minute <= 30 else hour + 1 %}mdi:clock-time-{{clock[hand]}}

but now see you doing:

{%- set _clock_icon = ['twelve','one','two','three','four','five','six','seven','eight','nine','ten','eleven'] %}

{%- macro clock_icon(hour=None) -%}
{%- if hour is none or hour is not integer -%}
  {%- set t = now() -%}
  {%- set h = (t.hour + t.minute // 30) % 12 %}
  {{- 'mdi:clock-time-' ~ _clock_icon[h] -}}
{%- else -%}
  {{- 'mdi:clock-time-' ~ _clock_icon[hour % 12] -}}
{%- endif -%}
{%- endmacro -%}

which both eliminates the ‘skip_this’ and the double ‘one’. which is much better ofc.

not sure I understand why this works and why that couldnt be done in the format I used

I’m not sure how yours even works in the first place. now().hour has a range of 0 to 23. a clock has a range of 0 to 11. 0 representing 12. All I’m doing is getting the remainder of i / 12 using modulo %. 0 / 12 has a remainder of 0, 1 / 12 has a remainder of 1… 13 / 12 has a remainder of 1… 15 / 12 has a remainder of 3, etc. So all I need to do is account for 0 to 11 because the maximum remainder will be 11.

see for yourself.

{%- for i in range(100) %}
{{ i }} -> {{ i % 12 }}
{%- endfor %}
1 Like

yes, I see that now. Ive changed it to:

      - unique_id: hour_icon
        name: Hour icon
        state: >
            {% set clock = ['twelve','one','two','three','four','five','six','seven',
                            'eight','nine','ten','eleven'] %}
            {%- set t = now() %}
            {%- set hand = (t.hour + t.minute // 30) % 12 %}mdi:clock-time-{{clock[hand]}}
            {{- '-outline' if states('sun.sun') == 'below_horizon' }}

btw, this is where my original template started to see the light:

and on the extra ‘one’:

This is awesome! What a useful and exciting introduction to Template Macros.

I do have one question/feature request: I’m trying to display elapsed minutes and hours from the last time a state changed. custom_time('states.entity.last_changed, 'hour, minute')" works great for this, except when there is less than a minute elapsed it says either “now” or “0 hours and 0 minutes”. I would prefer to say “less than a minute” in this case. Is there any to change/overwrite that?

I can probably add that but I’d need translations for all the included languages.

1.0.8 Release

New Features

  • Time between macros

New Languages

  • Portuguese
1 Like

Thanks for this release. It seems to be just what I am after as I have just started to tidy up some of my lovelace views with my own macros however there is no point in reinventing the wheel for my “Last Updated” type information.

However, im struggling to get this to work for some reason and im unsure why.

I just keep getting a question mark returned from the macro. Ive tried all sorts of variations and macros from the file but still get the same results.

Can anyone point me in the right direction please as at present when I input;

{% from 'easy_time.jinja' import clock, easy_time %}
## Checking that the easy_time.jinja is importing correctly
{{ clock() }}

## Verifying a date is being passed to the macro
{{ states.sensor.zigbee_repeater_living_room_rssi.last_changed }}

## Trying out easy_time
{{ easy_time('states.sensor.zigbee_repeater_living_room_rssi.last_changed') }}

This is my output (in dev console);

## Checking that the easy_time.jinja is importing correctly
8:55 AM

## Verifying a date is being passed to the macro
2023-05-10 07:50:18.109501+00:00

## Trying out easy_time
?

Any help much appreciated. Thank you.

Don’t put the states….last_updated in quotes.

Brilliant. Thanks - i just managed to get that working a few seconds before and came here to update my query! Thank you again for this great set of macros.

Im loving these macros so far.
Just whilst fiddling at work ive managed to reduce my overall lovelace frontend from 22800 lines down to 21600 lines and ive only been through the first 7000 lines or so! I know there is much more to come off too!

One thing that I would like to see but I am not sure is currently possible is to output the time when your passed just the seconds from an entity.

For example, at present I have the uptime of my nvr as a sensor reading such as 340015 (seconds).

However, there is no way to output this as “3 days, x hours”. It would be amazing if this could be considered in a future release.

Thanks

You can do that now without much effort, but yes I can add that

{% from 'easy_time.jinja' import custom_time, easy_time, big_time %}
{{ easy_time(now() - timedelta(seconds=340015)) }}
{{ big_time(now() - timedelta(seconds=340015)) }}
{{ custom_time(now() - timedelta(seconds=340015), 'hour,minute') }}
{{ custom_time(now() - timedelta(seconds=340015), 'day,hour') }}