Need help to understand template in if with dates

Hello everybody,

I am despairing of a simple code right now. I do not understand the logic here. Can anyone help me?
I want to check, if a grocy chore date is due. I added thee lines to my code to check the boolean:

   {% set SENSOR ='sensor.grocy_chores' -%}  
   {%- for attr in states[SENSOR].attributes.chores -%}
      {% if (attr.next_execution_assigned_to_user_id == 2) -%}
        {{
          {
            'type': 'button',
            'entity': SENSOR,
            'attribute': attr,
            'name': attr.name, 
            'tap_action':
              {
              'action': 'call-service',
              'service': 'grocy.execute_chore',
              'service_data':
                {
                'chore_id': attr.id,
                'done_by': attr.next_execution_assigned_to_user_id,
                }
              }
          }
        }},
        {% set t = now().strftime("%Y-%m-%d") -%}
        {%- set date = attr.next_estimated_execution_time -%}
        {{ date | as_timestamp | timestamp_custom('%Y-%m-%d') <= t }}
      {%- endif -%}
    {%- endfor %}

    {% set SENSOR = 'sensor.grocy_tasks' -%}

    {%- for attr in states[SENSOR].attributes.tasks -%}
       {% if (attr.assigned_to_user_id == 3) -%}
        {{
          {
            'type': 'button',
            'entity': SENSOR,
            'attribute': attr,
            'name': attr.name, 
            'tap_action':
              {
              'action': 'call-service',
              'service': 'grocy.complete_task',
              'service_data':
                {
                'task_id': attr.id,
                }
              }
          }
        }},
        {% set t = now().strftime("%Y-%m-%d") -%}
        {%- set date = attr.next_estimated_execution_time -%}
        {{ date | as_timestamp | timestamp_custom('%Y-%m-%d') <= t }}
      {%- endif -%}
     {%- endfor %}

It changes exaclty between true and false in each chore. But if I put this template in my if condition, it doesn’t work:

type: custom:auto-entities
card:
  type: entities
  title: Hausaufgaben
filter:
  template: >-
    {% set t = now().strftime("%Y-%m-%d") -%}  {% set SENSOR
    ='sensor.grocy_chores' -%}  {%- for attr in
    states[SENSOR].attributes.chores-%}
        {%- set date = attr.next_estimated_execution_time -%}
        {%- if (date | as_timestamp | timestamp_custom('%Y-%m-%d') <= t) -%}
          {%- if (attr.next_execution_assigned_to_user_id == 2) -%}
            {{
              {
             'type': 'button',
             'entity': SENSOR,
             'attribute': attr,
             'name': attr.name, 
             'tap_action':
              {
               'action': 'call-service',
               'service': 'grocy.execute_chore',
               'service_data':
                 {
                 'chore_id': attr.id,
                 'done_by': attr.next_execution_assigned_to_user_id,
                 }
                }
              }
             }},
          {% endif %}          
        {%- endif -%}
       {%- endfor %}

What’s the problem? Why doesn’t it work?

You need a namespace to pull the values out of the for loop:

type: custom:auto-entities
card:
  type: entities
filter:
  template: |
    {% set ns = namespace(chores=[])%}
    {% set t = now().date() -%}  
      {% set SENSOR ='sensor.grocy_chores' -%}  
        {%- for attr in states[SENSOR].attributes.chores-%}
          {%- set date = attr.next_estimated_execution_time | as_datetime -%}
          {%- if date.date() <= t -%}
            {%- if (attr.next_execution_assigned_to_user_id == 2) -%}
              {% set ns.chores = ns.chores +[
                {
                 'type': 'button',
                 'entity': SENSOR,
                 'attribute': attr,
                 'name': attr.name, 
                 'tap_action':
                    {
                     'action': 'call-service',
                     'service': 'grocy.execute_chore',
                     'service_data':
                       {
                       'chore_id': attr.id,
                       'done_by': attr.next_execution_assigned_to_user_id,
                       }
                      }
                    }
                   ] %}
                {% endif %}          
              {%- endif -%}
             {%- endfor %}
             {{ ns.chores }}

Dear Drew,
thanks for answering immediately. The auto-entity worked without namespace. My problem is the date issue. Your code doesn’t work, too.
btw: without the if line of the date works it pretty fine. :wink:

What exactly is the “date issue”?

EDIT: I see it now… You want to have it print true/false for overdue.

{% set t = now().strftime("%Y-%m-%d") -%}
{%- set date = attr.next_estimated_execution_time -%}
{{ date | as_timestamp | timestamp_custom('%Y-%m-%d') <= t }}

works fine and give true or false, but if if put it

it doesn’t work.

Date seems to be working fine when using datetime objects

type: custom:auto-entities
card:
  type: entities
  title: Chores
filter:
  template: |
    {% set ns = namespace(chores=[])%}
    {% set today = now().date() -%}  
    {% set my_sensor ='sensor.grocy_chores' -%}  
    {%- for attr in states[my_sensor].attributes.chores
    | sort(attribute='next_estimated_execution_time') -%}
      {%- set date = attr.next_estimated_execution_time | as_datetime -%}
      {%- if date.date() <= t + timedelta(days=2) and 
      attr.next_execution_assigned_to_user_id in [2, none]-%}
        {% set ns.chores = ns.chores + [ 
        { 
          'type': 'button',
          'entity': my_sensor,
          'attribute': attr,
          'name': attr.name ~ (' - OVERDUE' if (date.date() <= today) else ''),
          'tap_action':
          {
            'action': 'call-service',
            'service': 'grocy.execute_chore',
            'service_data': 
            {
            'chore_id': attr.id,
            'done_by': attr.next_execution_assigned_to_user_id or 2,
            } 
          }
        }
      ] %}
    {% endif %}{%- endfor %}
    {{ ns.chores }}

image

Hello Drew,
I’m really getting crazy. Copy->paste: Nothing working.
Copied the template to developer-tools: Error
image
I removed all “<”: Error didn’t remove.

This line makes the error.
I removed:

and it works. What’s wrong with my instance? Or my code?

I finally got it running:

      - type: custom:auto-entities
        card:
          type: entities
          title: Chores
        filter:
          template: >-
            {% set ns = namespace(chores=[])%} {% set today = now().date() -%}  
            {% set my_sensor ='sensor.grocy_chores' -%}   {%- for attr in
            states[my_sensor].attributes.chores -%}
              {%- if attr.next_execution_assigned_to_user_id in [2, none]-%}
                {%- set date = strptime(attr.next_estimated_execution_time, '%Y-%m-%dT%H:%M:%S').date()-%}
                {%- if date <= today -%}
                  {% set ns.chores = ns.chores + [ 
                  { 
                    'type': 'button',
                    'entity': my_sensor,
                    'attribute': attr,
                    'name': attr.name,
                    'tap_action':
                    {
                      'action': 'call-service',
                      'service': 'grocy.execute_chore',
                      'service_data': 
                      {
                      'chore_id': attr.id,
                      'done_by': attr.next_execution_assigned_to_user_id or 2,
                      } 
                    }
                  }
                ] %} 
              {% endif %}{% endif %}{%- endfor %}
              {{ns.chores}}