Reading Jinja 2 documention is confusing and I try many options, none seem to work for me
(sorry if I miss something obvious)
I have set up different events, each has an input_datetime (date only) and I do corresponding sensor to give me countdown to that event (examples of entities) : -
The above gives me a number eg 23 and that’s okay but I could change output to {{ ('%03d' % delta) }} if that helps as easier to sort as text so ‘023’
I have card to show events and countdowns but card very big and most important - Not Sorted so you have to scan list (I have other sensors for next event and what that event is but very difficult to see the 2nd next event etc.)
So the makdown card allows more compact output but needs sorting (text or integer - don’t care)
Current markdown is : -
### Event List
- type: markdown
title: Event Countdown Short
content: |
{%- for item in states.sensor if 'cal_cd_' in item.entity_id %}
{{ ('%03d' % item.state | int(0) ~ ' - ' ~ item.name) | replace(' Countdown', '') -}}
{% endfor %}
This produces : -
The " Countdown" appears in ‘some’ friendly names due to context but is not consistent
I also played around with {{ ('% 5d' % item.state | int(0) ~ ' - ' ~ item.name) | replace(' Countdown', '') -}} Which gives pleasing results in template editor but is completely ignored in markdown card
So is it possible to sort this output into a readable list ?
Sorry if I not explain it clearly
Hi 123,
As the state comes back text then when sorted order comes out as : -
113 blarb
17 blarb
175 blarb
182 blarb
…
Now I realise that Outputting the sensors as {{ ('%03d' % item.state | int(0) ~ ' - ' ~ item.name) | replace(' Countdown', '') -}} will allow this sort to work but if a number sort would work it be better readable.
Thanks for your response anyway
Edit: if I do the %03d I suppose I could replace the '0’s in the output with nbsp or something but then 30 would come out as 3 ???
That’s what I suspected would happen because an entity’s state value is a string. When you sort numeric strings you don’t get the same ordering as when you sort numbers.
In the linked example, the sorting is performed on the entity’s attribute which is not limited to storing values as a string but can be other types like datetime, int, float, boolean, etc. That makes it more flexible for sorting purposes.
The linked example in cyn’s post above (the one that you said served as inspiration for using a Markdown card).
Yes, you can enhance your Template Sensor so that it also stores the number of days as an integer value in an attribute. If the new attribute’s name is days then the template can be changed to this:
If you are interested, you can simplify the template used to compute the number of days remaining until the event’s date. You can use either of these two templates (your choice):
Ah Ha !
No - I think I got that
The bad bit for me was adding the attribute, so I’m away to experiment it.
I went to : - Template - Home Assistant
and : -
You’re using the legacy style of defining a Template Sensor, not the modern style. In the legacy style, the option is called attribute_templates whereas in modern style it’s attributes. Look at the example I posted above.
I have a sensor that is ‘spare’ it has text to lable it - this is what the dynamic key at bottom displays. (I will later adjust template to give text in that case ‘sensor.cal_cd_other’ )
Will also go through and adjust sensors as you gave me
The Tab effect is very good - I tried just to remove the headings and keep tabs but then whole effect ruined. So went back
It is still VERY GOOD
Many, Many, Many, Many Thanks
Cryptic
Post Script Edit :
|**Days**||**Evemt**|
|----:|--|:----|
{% for item in states.sensor | selectattr('object_id', 'match', 'cal_cd_') | sort(attribute='attributes.days') -%}
|{{item.state}}||{{ states('input_text.cal_title_other') if 'Other' in item.name else (item.name | replace(' Countdown', '')) }}|
{% endfor %}