Date template to show nd st rd next to the date


Ive extracted the next event on a calendar I subscribe to but it shows as 8 March 2020

If there a way of having 1st, 2nd, 3rd, 4th etc instead of 1 March etc ?

Thanks Martyn

- platform: template
      value_template: "{{ as_timestamp(strptime(state_attr('calendar.leeds_rugby', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom('%d %B %Y') }}"
      friendly_name: "Date"

you gotta strip the day out and pass it to a function that decides what to add.

- platform: template
      value_template: >
        {%- macro ordinal(num) %}
        {%- set d = {1:'st',2:'nd',3:'rd'} %}
        {%- set num = num | int %}
        {%- if 10 < num % 100 <= 20 %}
          {{- num }}th
        {%- else %}
          {{- num }}{{ d.get(num % 10, 'th') }}
        {%- endif %}
        {%- endmacro %}
        {% set d = as_timestamp(strptime(state_attr('calendar.leeds_rugby', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom('%d') %}
        {% set end = as_timestamp(strptime(state_attr('calendar.leeds_rugby', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom(' %B %Y') %}
        {{ ordinal(d) ~ end }}
      friendly_name: "Date"

Thank you very much for the explanation. I will test that tonight and I can use on a couple of other things too.


Hi, sorry, quick addition to this. How would I get the day of the game included. I know its %A but I cant get it to work and say for example Saturday 14th March 2020.

Please could you advise where to add it ?



You have all the building blocks in the templates i provided. I’ll give you a hint. You’ll be doing what is done with end, but you’ll be making a variable named start and adding it to the beginning.

1 Like


Thanks for your guidance and I have infact got Saturday showing and now after some tweaking, I have the correct spaces.

Thanks again


1 Like

Hi Petro

Your solution is working and showing the date but I have noticed the date only updates if I restart Home Assistant.

Do I need anymore date sensors adding to configuration.yaml or automation to update at midnight?

Thanks in advance


add the calendar entity_id to the entity_id field for the template sensor.


Further on from my question above. Ive hit a calendar entry and it now says unknown in the number of days to the event.

Is there a way to either remove the unknown or even have a minus and the days counting up till it reaches the finish date ?

Ideally, Id like to move to the next event if possible as soon as its reached the start date.

Any help would be appreciated

are you sure another even exists? The way this is built is that it uses what’s in the calendar, if the calendar doesn’t serve up the information, how can the template?

Hi. Yes. Ive put all the school holidays in for the full school year.

It will change I guess when the finish date pasts but not until then, unless there is some code that tells it to…if thats possible


What code would tell it to? This is coming from a calendar. How does the calendar come up with it?

Even though this is an old thread, in case someone needs it for later and lands on this while doing a search, here is a good way to get the day ordinal with minimal fuss:

{{ now().day | ordinal }}