dicko
(Martyn)
March 8, 2020, 4:35pm
1
Hi
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
sensors:
rhinos_next_game_date:
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"
petro
(Petro)
March 9, 2020, 1:35pm
2
you gotta strip the day out and pass it to a function that decides what to add.
You can copy the macro and use it anywhere. Works with all numbers (int/string int) to infinity.
EDIT: I should mention that ‘unknown’ will result in 0th day. So that’s a thing. So you don’t need to check to see if the day_us sensor exists, but remember that 0th day is unknown.
pretty_day:
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 %}
{{- …
- platform: template
sensors:
rhinos_next_game_date:
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"
dicko
(Martyn)
March 9, 2020, 3:52pm
3
Thank you very much for the explanation. I will test that tonight and I can use on a couple of other things too.
Martyn
dicko
(Martyn)
March 12, 2020, 10:26am
4
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 ?
Thanks
Martyn
petro
(Petro)
March 12, 2020, 11:53am
5
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
dicko
(Martyn)
March 12, 2020, 12:32pm
6
Hi.
Thanks for your guidance and I have infact got Saturday showing and now after some tweaking, I have the correct spaces.
Thanks again
Martyn
1 Like
dicko
(Martyn)
March 24, 2020, 7:53am
7
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
Martyn
petro
(Petro)
March 24, 2020, 10:48am
8
add the calendar entity_id to the entity_id field for the template sensor.
dicko
(Martyn)
April 16, 2020, 11:41am
9
Hi
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
petro
(Petro)
April 16, 2020, 11:44am
10
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?
dicko
(Martyn)
April 16, 2020, 11:46am
11
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
petro
(Petro)
April 16, 2020, 11:50am
12
What code would tell it to? This is coming from a calendar. How does the calendar come up with it?
lumper5
(Lumper5)
January 20, 2024, 10:49am
13
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 }}