I wanna create a template sensor that gives me the second attribute as a date and the garbage type.
As the attribute names (dates) change - how do i proceed here?
- sensor:
- name: Black bin
state: >-
{% set al = states['sensor.local_bin_collection']['attributes'] %}
{% set ns = namespace(dates=[]) %}
{% for k in al|select('<','3') %}
{% if "Black Bin" in al[k] %}
{% set ns.dates = ns.dates + [as_datetime(k)] %}
{% endif %}
{% endfor %}
{{ 'unknown' if not ns.dates else ns.dates[0] }}
You can paste the template into Developer Tools / Template to experiment.
The <'3' is a filter to remove non-date attributes from consideration. Works until the year 3000.
Thank you.
But if i understand this correctly, it gives me the date for the first attribute named “Black Bin”, right?
I am looking for the attibute name (date) and attribute value (garbage type) of my sensors attribute lists second entry.
Both of the things (name and value) will be dynamic - the date of the second entry will change as well as the value.
and i want to create a sensor that always gives me the date and value of the second row - but i will never exactly now which date and value will be there …
That would be great, yes.
Essentially i would like to get a sensor that do NOT tell me the next date and garbage type (that does the integration handle well), but the date and type after that.
Yes, provided the list excludes non-date attributes: it’s the index counting from zero. It also assumes that the dates will appear in order in the attributes list — you can add a |sort if you are concerned that’s an unsafe assumption.