Hello, I have almost got a nice bin collection system working, but I am looking for some help to figure out exactly why one of my template sensors is returning “unknown”:
I have tested everything I can think of, and am now at a brick wall…
Here is the YAML:
template:
### Template sensors to keep track of how many days until each bin collection.
### These check just after midnight each day to see how long each one is
- trigger:
- platform: time
at: "00:01:00"
- platform: homeassistant
event: start
action:
- action: calendar.get_events
data:
start_date_time: "{{ today_at() }}"
duration:
days: 60
target:
entity_id: calendar.bin_collections
response_variable: agenda
- variables:
events: "{{ agenda['calendar.bin_collections']['events'] }}"
days_recycling: >-
{% set match = events
| selectattr('summary', 'search', 'Recycling bin collection')
| map(attribute='start')
| map('as_datetime')
| list %}
{{ (match[0] | as_local - today_at()).days if match | length > 0 else -1 }}
days_garden: >-
{% set match = events
| selectattr('summary', 'search', 'Garden bin collection')
| map(attribute='start')
| map('as_datetime')
| list %}
{{ (match[0] | as_local - today_at()).days if match | length > 0 else -1 }}
sensor:
- name: "Recycling bin countdown"
unique_id: recycling_bin_countdown
icon: mdi:trash-can
state: >
{% if days_recycling == -1 %}
None upcoming
{% elif days_recycling == 0 %}
Today
{% elif days_recycling == 1 %}
Tomorrow
{% else %}
In {{ days_recycling }} Days
{% endif %}
- name: "Garden bin countdown"
icon: mdi:trash-can
unique_id: garden_bin_countdown
state: >
{% if days_garden == -1 %}
None upcoming
{% elif days_garden == 0 %}
Today
{% elif days_garden == 1 %}
Tomorrow
{% else %}
In {{ days_garden }} Days
{% endif %}
Brief description of this YAML
I have an action which runs every day just after midnight (or as well just after Home Assistant has restarted), which grabs the list of all events in my “bin collection” calendar over the next 60 days. It then takes the first upcoming one for each of recycling and garden bins, and counts the number of days until that bin collection - these get stored in the days_recycling and days_garden variables. If there is no bin collection found in the next 60 days, it sets the number of days to -1.
Once these two variables have been calculated, they are used to update the state of my recycling_bin_countdown and garden_bin_countdown sensors, which can have the state “Tomorrow” if the number of days is equal to 1, for example.
I can run the calendar.get_events action in Developer Tools:
and the response is the following:
calendar.bin_collections:
events:
- start: "2026-01-06"
end: "2026-01-07"
summary: Recycling bin collection
description: ""
- start: "2026-01-20"
end: "2026-01-21"
summary: Recycling bin collection
description: ""
This works correctly - in this case, there are two recycling bin collections within 60 days in my calendar, but NO garden bins, so that one is absent from the response.
I can then use the “Copy to clipboard as template” button to check everything works in the Template Editor using the following full code:
{% set agenda = {"calendar.bin_collections":{"events":[{"start":"2026-01-06","end":"2026-01-07","summary":"Recycling bin collection","description":""},{"start":"2026-01-20","end":"2026-01-21","summary":"Recycling bin collection","description":""}]}} %}
{% set events = agenda['calendar.bin_collections']['events'] %}
{% set match = events
| selectattr('summary', 'search', 'Recycling bin collection')
| map(attribute='start')
| map('as_datetime')
| list %}
{% set days_recycling = (match[0] | as_local - today_at()).days if match | length > 0 else -1 %}
{% set match = events
| selectattr('summary', 'search', 'Garden bin collection')
| map(attribute='start')
| map('as_datetime')
| list %}
{% set days_garden = (match[0] | as_local - today_at()).days if match | length > 0 else -1 %}
days_recycling = {{ days_recycling }}
days_garden = {{ days_garden }}
{% if days_recycling == -1 %}
None upcoming
{% elif days_recycling == 0 %}
Today
{% elif days_recycling == 1 %}
Tomorrow
{% else %}
In {{ days_recycling }} Days
{% endif %}
{% if days_garden == -1 %}
None upcoming
{% elif days_garden == 0 %}
Today
{% elif days_garden == 1 %}
Tomorrow
{% else %}
In {{ days_garden }} Days
{% endif %}
You can see this all works as expected, and returns “None upcoming” for the garden bin:
The Problem
It works correctly when a bin has an upcoming collection:
But fails when there is no upcoming collection, and gets the state “unknown” instead of “None upcoming”:
Anyone spot what’s going on here?
My feeling is that the logic is correct (because it’s returning -1 correctly in template editor), but it’s something to do with assigning variables in the YAML and then trying to use them in the trigger-based template sensor… Although it’s partly obtaining the variables correctly because I indeed obtain “In 26 Days” for my recycling bin…
- Installation method Home Assistant OS
- Core 2025.11.3
- Supervisor 2025.12.3
- Operating System 16.3
- Frontend 20251105.1








