Pull an dynamic attribute name from a sensor?

Hi there,

i have a sensor that is populated with dates. It looks like that:

2024-01-25: Gelbe Säcke
2024-01-29: Restmülltonnen
2024-02-08: Gelbe Säcke
2024-02-12: Restmülltonnen
attribution: Last update: 01/18/24 12:16:41
icon: mdi:sack
friendly_name: Abfallentsorgung

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?

Thank you!

Like this, from my system?

- 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.

So today the sensors attrobutes may look like:

2024-01-29: Restmülltonnen
2024-02-08: Gelbe Säcke

and after that like:

2024-02-08: Gelbe Säcke
2024-02-14: Papiertonnen

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 …

So you want the second date as the sensor state, and the type as an attribute? Have a play with this:

{% set d2 = (states['sensor.local_bin_collection']['attributes']|select('<','3')|list)[1] %}
{{ d2 }}: {{ al[d2] }}

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.

Untested and written on a phone:

- sensor:                                                                        
    - name: Next bin
      state: >-
        {{ (states['sensor.local_bin_collection']['attributes']|select('<','3')|list)[1] }}
      attributes:
        type: >-
          {{ states['sensor.local_bin_collection']['attributes'][(states['sensor.local_bin_collection']['attributes']|select('<','3')|list)[1]] }}

State is meant to be the key of the second attribute where that key is alphabetically less than 3.

Type attribute is the value of that key.

That’s it! Thank you so much!
The

... list)[1]

seems to the crucial part to select the second row of attribute entries, right?

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.

1 Like