Template to find next garbage collection based on 4 sensors

I have 4 sensors for my garbage collection (general waste, glass, paper, plastic). Each of them outputs number of days until next collection.

In my dashboard I would like to display the next garbage collection and what is being collected. My jinja2 isnt very fluid so I haven’t been able to figure out how to find the lowest number of the 4 and at the same time output what is being collected.

Can anyone help me bring this code over the line so {{ list|min }} outputs for example 7 Rest.

{% set rest = [state_attr('sensor.movar_rest', 'daysTo'), "Rest"] %}
{% set papir = [state_attr('sensor.movar_papir', 'daysTo'), "Papir"] %}
{% set plast = [state_attr('sensor.movar_plast', 'daysTo'), "Plast"] %}
{% set glass = [state_attr('sensor.movar_glass', 'daysTo'), "Glass"] %}
{% set list = rest[0],papir[0],plast[0],glass[0] %}

{{ rest[0] }} {{ rest[1] }}
{{ papir[0] }} {{ papir[1] }}
{{ plast[0] }} {{ plast[1] }}
{{ glass[0] }} {{ glass[1] }}

{{ list|min }}

make it 1 list

{% set stuff = [
  [state_attr('sensor.movar_rest', 'daysTo'), "Rest"],
  [state_attr('sensor.movar_papir', 'daysTo'), "Papir"],
  [state_attr('sensor.movar_plast', 'daysTo'), "Plast"],
  [state_attr('sensor.movar_glass', 'daysTo'), "Glass"],
] %}
{{ stuff | min(attribute='0') | map(attribute='1') | first | default }}

Makes it hard to read but gets the job done

A different way to approach this is to get the next entity itself.

{% set entities = 'sensor.movar_rest', 'sensor.movar_papir', 'sensor.movar_plast', 'sensor.movar_glass' %}
{% set next = expand(entities) | selectattr('attributes.daysTo', 'defined') | sort(attribute='attributes.daysTo') | first | default %}
{{ next.name if next else '??' }}
1 Like

Thank you! Learned so much from this :slight_smile:

Thanks for spending your time to help!

On second thought.

I split them up in two strings like this {{ (stuff | min(attribute='0'))[0] }} and {{ (stuff | min(attribute='0'))[1] }}

But sometimes two different types might be collected on the same day. Do you have any ideas how I can easily display two of {{ (stuff | min(attribute='0'))[1] }} if they land on the same day?

My automation sends a notification for one or more collections, such as “Collection of garden waste and recycling today” or “Collection of household waste tomorrow”.

I have 3 time based triggers for the automation.
3 am, update sensors (id: update sensors)
4pm, notify of tomorrows collections (id: tomorrow)
7am, notify of todays collections (id: today)

The actions:

choose:
  - conditions:
      - condition: trigger
        id: update sensors
    sequence:
      - service: homeassistant.update_entity
        data: {}
        target:
          entity_id: sensor.bin_collection_green_days #updating one scrape sensor updates all
default:
  - variables:
      # 'collections' will look like "waste" or "garden waste and recycling"
      # The sensors entity names are used
      collections: |-
        {{
          [
            states.sensor.bin_collection_green_days,
            states.sensor.bin_collection_recycling_days,
            states.sensor.bin_collection_waste_days
          ]
          |selectattr('state','in',['1' if trigger.id == 'tomorrow' else '0'])
          |map(attribute='name')
          |join(' and ')
          |lower
        }}
  - condition: template
    value_template: "{{ collections|length > 0 }}"
  - service: notify.notify_residents
    data:
      message: Collection of {{ collections }} {{ trigger.id }}