Nice one! I did the following, since I just wanted to know which garbage container is the next one:
- platform: template
sensors:
####################################################################
# WASTE COLLECTION
####################################################################
waste_collection_upcoming:
friendly_name: Upcoming waste collection
unit_of_measurement: ''
attribute_templates:
upcoming_list: |
{% if state_attr('sensor.waste_collection_upcoming_key', 'upcoming_list') %}
{{ state_attr('sensor.waste_collection_upcoming_key', 'upcoming_list') }}
{% endif %}
Sort-date: |
{% if states('sensor.waste_collection_upcoming_key') is defined %}
{% set waste_upcoming_key = states('sensor.waste_collection_upcoming_key') %}
{{ state_attr('sensor.' + waste_upcoming_key, 'Sort-date') }}
{% endif %}
entity_picture: |
{% if states('sensor.waste_collection_upcoming_key') is defined %}
{% set waste_upcoming_key = states('sensor.waste_collection_upcoming_key') %}
{{ state_attr('sensor.' + waste_upcoming_key, 'entity_picture') }}
{% endif %}
Days-until: |
{% if states('sensor.waste_collection_upcoming_key') is defined %}
{% set waste_upcoming_key = states('sensor.waste_collection_upcoming_key') %}
{{ state_attr('sensor.' + waste_upcoming_key, 'Days-until') }}
{% endif %}
container_color: |
{% if states('sensor.waste_collection_upcoming_key') is defined %}
{% if states('sensor.waste_collection_upcoming_key') == 'gad_gft' %}
green
{% endif %}
{% if states('sensor.waste_collection_upcoming_key') == 'gad_papier' %}
blue
{% endif %}
{% if states('sensor.waste_collection_upcoming_key') == 'gad_pmd' %}
orange
{% endif %}
{% if states('sensor.waste_collection_upcoming_key') == 'gad_restafval' %}
gray
{% endif %}
{% endif %}
waste_name: |
{% if states('sensor.waste_collection_upcoming_key') is defined %}
{% if states('sensor.waste_collection_upcoming_key') == 'gad_gft' %}
GFT
{% endif %}
{% if states('sensor.waste_collection_upcoming_key') == 'gad_papier' %}
Paper
{% endif %}
{% if states('sensor.waste_collection_upcoming_key') == 'gad_pmd' %}
PMD
{% endif %}
{% if states('sensor.waste_collection_upcoming_key') == 'gad_restafval' %}
General
{% endif %}
{% endif %}
value_template: |
{% if (states('sensor.waste_collection_upcoming_key') is defined)
and (state_attr('sensor.' + states('sensor.waste_collection_upcoming_key'), 'Sort-date') is defined) %}
{% set waste_upcoming_key = states('sensor.waste_collection_upcoming_key') %}
{% if (waste_upcoming_key is defined) %}
{% set t = state_attr('sensor.' + waste_upcoming_key, 'Sort-date') %}
{# set t = strptime(t+"-+0000", "%Y-%m-%d,%H:%M:%S-%z") #}
{% set long_date = (as_timestamp(t) | timestamp_custom('%a, %d %B %Y')) %}
{% set full_day = (as_timestamp(t) | timestamp_custom('%A')) %}
{% if (state_attr('sensor.' + waste_upcoming_key, 'Days-until') is defined) and ((state_attr('sensor.' + waste_upcoming_key, 'Days-until') | int) < 7) %}
{% set days_until = state_attr('sensor.' + waste_upcoming_key, 'Days-until') | int %}
{% if days_until < 1 %}
Today
{% elif days_until < 2 %}
Tomorrow
{% elif days_until < 6 %}
This {{ full_day }}
{% endif %}
{% else %}
{{ long_date }}
{% endif %}
{% else %}
?
{% endif %}
{% else %}
?
{% endif %}
# Variable to use in `waste_collection_upcoming`
waste_collection_upcoming_key:
friendly_name: Upcoming waste collection key of sensor
unit_of_measurement: ''
attribute_templates:
upcoming_list: |
{% if (states('sensor.gad_gft') is defined) and (state_attr('sensor.gad_gft', 'Days-until') is defined) %}
{% set days_until_gft = state_attr('sensor.gad_gft', 'Days-until') %}
{% endif %}
{% if (states('sensor.gad_papier') is defined) and (state_attr('sensor.gad_papier', 'Days-until') is defined) %}
{% set days_until_papier = state_attr('sensor.gad_papier', 'Days-until') %}
{% endif %}
{% if (states('sensor.gad_pmd') is defined) and (state_attr('sensor.gad_pmd', 'Days-until') is defined) %}
{% set days_until_pmd = state_attr('sensor.gad_pmd', 'Days-until') %}
{% endif %}
{% if (states('sensor.gad_restafval') is defined) and (state_attr('sensor.gad_restafval', 'Days-until') is defined) %}
{% set days_until_restafval = state_attr('sensor.gad_restafval', 'Days-until') %}
{% endif %}
{{ [{
"name": "gad_gft",
"days_until": (days_until_gft | default(999, true)) | int
}, {
"name": "gad_papier",
"days_until": (days_until_papier | default(999, true)) | int
}, {
"name": "gad_pmd",
"days_until": (days_until_pmd | default(999, true)) | int
}, {
"name": "gad_restafval",
"days_until": (days_until_restafval | default(999, true)) | int
}] | sort(attribute="days_until") }}
value_template: |
{% if (states('sensor.gad_gft') is defined) and (state_attr('sensor.gad_gft', 'Days-until') is defined) %}
{% set days_until_gft = state_attr('sensor.gad_gft', 'Days-until') %}
{% endif %}
{% if (states('sensor.gad_papier') is defined) and (state_attr('sensor.gad_papier', 'Days-until') is defined) %}
{% set days_until_papier = state_attr('sensor.gad_papier', 'Days-until') %}
{% endif %}
{% if (states('sensor.gad_pmd') is defined) and (state_attr('sensor.gad_pmd', 'Days-until') is defined) %}
{% set days_until_pmd = state_attr('sensor.gad_pmd', 'Days-until') %}
{% endif %}
{% if (states('sensor.gad_restafval') is defined) and (state_attr('sensor.gad_restafval', 'Days-until') is defined) %}
{% set days_until_restafval = state_attr('sensor.gad_restafval', 'Days-until') %}
{% endif %}
{% set waste_list = [{
"name": "gad_gft",
"days_until": (days_until_gft | default(999, true)) | int
}, {
"name": "gad_papier",
"days_until": (days_until_papier | default(999, true)) | int
}, {
"name": "gad_pmd",
"days_until": (days_until_pmd | default(999, true)) | int
}, {
"name": "gad_restafval",
"days_until": (days_until_restafval | default(999, true)) | int
}] %}
{{ (waste_list | sort(attribute="days_until"))[0].name }}
waste_collection_upcoming
should give the first one that will be collected. I’ve noticed some warnings in my logs about this ‘old’ code, so don’t know if this will keep working. When I check devtools I still get correct returns for this one. I use the afvalbeheer integration that provides me with the base information.