Hey everyone, I wanted to share my recent experience setting up a template sensor for tracking the next garbage collection day in Home Assistant. I’m still pretty new to this, so I thought I’d walk through my journey in case it helps someone else out there!
Initially, I was really excited to get this working. I found a template online and tried to adapt it to my schedule. The idea was to create a sensor that would automatically update to show the next garbage collection day, which in my area is every other Wednesday. Here’s the code I started with:
yaml
sensor:
- platform: template
sensors:
garbage_collection:
friendly_name: “Garbage Collection”
value_template: |
{% set garbage = namespace(next_pickup=now()) %}
{% for day in range(7) %}
{% set tempDate = now() + timedelta(days=day) %}
{% if tempDate.weekday() == 2 and tempDate.isocalendar()[1] % 2 == 1 %}
{% set garbage.next_pickup = tempDate %}
{% endif %}
{% endfor %}
{{ garbage.next_pickup.strftime(‘%d.%m.%Y’) }}
At first, everything looked good in the template editor, but when I added it to my dashboard, the sensor just showed “unknown”. I was a bit stuck, but after some research and trial and error, I realized it was a simple whitespace issue in the YAML formatting. Once I fixed that, the sensor started working perfectly!
This experience taught me the importance of careful YAML syntax and how even small mistakes can cause big headaches. It also showed me how helpful the Home Assistant community is—without the forums and documentation, I might still be scratching my head.
If anyone else is trying to set up something similar, I’d recommend starting with a basic template and testing it step by step. Also, don’t hesitate to reach out for help here; the community is incredibly supportive!
Happy automating everyone! ![]()
![]()