‘Hello’ to everybody out there,
i’m struggling.
Szenario:
I did install custom anniversairy-card via UI. Everything works, means: i can create new entries and so on.
For displaying the values (and other stuff), i use the great custom-html-template-card.
Now i’m struggling, but first pure template code (without html, css and others):
First attemp:
{% set alle_sensoren = states.sensor | list %}
{% set geburtstag_sensoren = alle_sensoren | selectattr('entity_id', 'search',
'geburtstag') | sort(attribute='state', reverse=false) %}
Target: to sort the entries by ‘day to birthday’ (=> state).
Error: state is a string, so the order is that 9 is more than 334.
hmm, ok for me the solution i thought: change string to int
Result: ha, ha, ha — didn’t find a solution for that.
2nd attemp: use dictionairy and namespace
{%- set alle_sensoren = states.sensor | list -%}
{%- set geburtstag_sensoren = alle_sensoren | selectattr('entity_id', 'search',
'geburtstag') | sort(attribute='state', reverse=false) -%}
{%- set gebliste=namespace(gebkind={}) -%}
{%- for sensor in geburtstag_sensoren -%}
{%- set jubilar = { sensor.attributes.friendly_name : {'gebdatum': sensor.attributes.date, 'gebmonat': sensor.attributes.date, 'gebtag':sensor.attributes.date.day, 'gebalter':sensor.attributes.current_years +1, 'gebdeadline':sensor.state|int}} -%}
{# DEBUGGING #}
{{- jubilar -}}
{# --------- #}
{# {% set gebliste.gebkind = gebliste.gebkind + jubilar %} #}
{ #DEBUGGING #}
{# {{ gebliste.gebkind }} #}
{# --------- #}
{%- endfor -%}
Result:
ok, boy, go forward use namespace:
{%- set alle_sensoren = states.sensor | list -%}
{%- set geburtstag_sensoren = alle_sensoren | selectattr('entity_id', 'search',
'geburtstag') | sort(attribute='state', reverse=false) -%}
{%- set gebliste=namespace(gebkind=[]) -%}
{%- for sensor in geburtstag_sensoren -%}
{%- set jubilar = { sensor.attributes.friendly_name : {'gebdatum': sensor.attributes.date, 'gebmonat': sensor.attributes.date, 'gebtag':sensor.attributes.date.day, 'gebalter':sensor.attributes.current_years +1, 'gebdeadline':sensor.state|int}} -%}
{# DEBUGGING #}
{{- jubilar -}}
{# --------- #}
{% set gebliste.gebkind = gebliste.gebkind + jubilar %}
{# DEBUGGING #}
{{ gebliste.gebkind }} #}
{# --------- #}
{%- endfor -%}
ha, ha, ha …
doesn’t matter: use a dictionairy instead:
replace that:
{%- set gebliste=namespace(gebkind=[]) -%}
by that:
{%- set gebliste=namespace(gebkind={}) -%}
ha, ha, ha…
ooooookayyyyy. then i ‘dirty’ one :
replace:
{%- set jubilar = { ... }
with:
{%- set jubilar = [{ ... }]
holla, die Waldfee !
Now sorting…:
{%- set alle_sensoren = states.sensor | list -%}
{%- set geburtstag_sensoren = alle_sensoren | selectattr('entity_id', 'search',
'geburtstag') | sort(attribute='state', reverse=false) -%}
{%- set gebliste=namespace(gebkind=[]) -%}
{%- for sensor in geburtstag_sensoren -%}
{%- set jubilar = [{ sensor.attributes.friendly_name : {
'gebdatum': sensor.attributes.date,
'gebmonat': sensor.attributes.date,
'gebtag':sensor.attributes.date.day,
'gebalter':sensor.attributes.current_years +1,
'gebdeadline':sensor.state|int
}}] -%}
{# DEBUGGING #}
{# {{- jubilar -}} #}
{# --------- #}
{% set gebliste.gebkind = gebliste.gebkind + jubilar | sort(attribute='gebdeadline', reverse=true) %}
{# DEBUGGING #}
{{ gebliste.gebkind}}
{# --------- #}
{%- endfor -%}
ha, ha, ha, 9 or 99 are both more than 334 (still a string, or again ?)
that’s the point i’m standing at, right now…
INFO:
testing in HA by template editor under developer tools
Please, somebody here who can help me ?
Many thanks in advance !
So long
PC