I’ve made a template to take datetime types and show them in a friendly or more-casual wording. For example:
- Today
- Tomorrow
- This Wednesday
- Next Wednesday
- etc…
It ‘works’ for me however it’s built upon a lot of IF statements and seems like a code smell.
I’d love some feedback, if anyone knows of a cleaner way to achieve this, or if it’s useful for anyone else.
{% if as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%H%M') == "0000" %}
{% if as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%j') | int == 359 %}
{{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%A')}}
{% elif as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%D') == as_timestamp(now(), default) | timestamp_custom('%D') %}
Today
{% elif as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%d') | int == as_timestamp(now(), default) | timestamp_custom('%d') | int +1 %}
Tomorrow
{% elif (as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%j') | int - as_timestamp(now(), default) | timestamp_custom('%j') | int ) < 6 %}
{{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%A')}}
{% elif (as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%j') | int - as_timestamp(now(), default) | timestamp_custom('%j') | int ) > 6 and as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%U') | int == as_timestamp(now(), default) | timestamp_custom('%U') | int +1 %}
Next {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%A')}}
{% else %}
{{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%d/%m')}}
{% endif %}
{% else %}
{% if as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%j') | int == 359 %}
{{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%H:%M')}} {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%A')}}
{% elif as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%D') == as_timestamp(now(), default) | timestamp_custom('%D') %}
{{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%H:%M')}} Today
{% elif as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%d') | int == as_timestamp(now(), default) | timestamp_custom('%d') | int +1 %}
{{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%H:%M')}} Tomorrow
{% elif (as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%j') | int - as_timestamp(now(), default) | timestamp_custom('%j') | int ) < 6 %}
{{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%H:%M')}} {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%A')}}
{% elif (as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%j') | int - as_timestamp(now(), default) | timestamp_custom('%j') | int ) > 6 and as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%U') | int == as_timestamp(now(), default) | timestamp_custom('%U') | int +1 %}
{{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%H:%M')}} Next {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%A')}}
{% else %}
{{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%d/%m')}}
{% endif %}
{% endif %}
We have an attribute called event_date
in the shortcuts.calendar
entity, which holds a datetime in ISO formatting.
In my instance, I want to treat start times of 00:00 as an all day event, which I don’t want to display a time for.
Lazy hack time: Because of how I’m calculating ‘This Week’ and ‘Next Week’, towards the end of the year I only display the date in a DD/MM format.