Template sensor for garbage collection

EDIT: TURNS OUT IT WAS A WHITESPACE ISSUE…**

Hi, I’m fairly new to home assistant and I’m trying to set up a template sensor for next garbage collection day but somehow the sensor value is “unknown”. What am I doing wrong?

template: 
  - sensor:
      - name: "Garbage collection"
        state: > 
          {% 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 %} 
          {## THE NEXT LINE IS NOT REGISTERED AS VALUE ##}}
          {{ garbage.next_pickup.strftime('%d.%m.%Y') }}

Why is this not set as the value: {{ garbage.next_pickup.strftime(‘%d.%m.%Y’) }}
The code should loop for next 7 days and find next odd week wednesday. It’s rendering correctly in template editor inside HA…

The most common reason for that for new users is that they put their YAML configuration directly in the Template Helper’s State template* field… If you are using a Template helper it should look as follows, with only the template in the State template* field:

However, I think you need to increase the value you are using in range since the span is 14 days…

{% set garbage = namespace(next_pickup=now()) %} 
{%- for day in range(14) %} 
  {%- set tempDate = today_at() + timedelta(days=day) %}
    {%- if tempDate.weekday() == 2 and 
    tempDate.isocalendar().week is odd %}                                         
      {%- set garbage.next_pickup = tempDate %}
    {%- endif %} 
{%- endfor %}
{{- garbage.next_pickup.strftime('%d.%m.%Y') }}
1 Like

I used configuration.yaml that’s why i posted the whole thing. I was wondering if I went wrong there.

It seems that it was a whitespace issue… Didn’t know about those dashes after percent symbol. I’m a experienced developer myself. Sorry that the code was unfinished with the range and all that but because next week number is odd, it was just for testing purposes.

Thanks for the reply @Didgeridrew !!! <3