Template if date is equal or after current date

Not sure if it actually works, but it seems, at least that

for sensor in entities 

should be

for sensor_entity in sensor_entities 

then it says this

Never use screenshots for code :wink:

I guess you forget to change if states(sensor) by if states(sensor_entity)

Now it shows all the sensors is overdue… is that something with the date format

the humdifier sensor state is formated like this

2023-08-02T13:00:51+00:00

You are comparing strings that do not even have the same format.

Try

        chores: |
            {% set sensor_entities = [
              'sensor.clean_doors', 
              'sensor.clean_drain', 
              'sensor.clean_fridge',
              'sensor.clean_behind_furnitures',
              'sensor.clean_humdifier'
            ] %}
            {% set current_date = now().date() %}
            {%- for sensor in entities if as_datetime(states(sensor)) > now() %}
                {% set chore_name = state_attr(sensor_entity, 'friendly_name') %}
                {{ "Overdue Chore: " + chore_name }}
              {% else %}
                {{ "No overdue chores today." }}
            {%- endfor %} 

Optimized

        chores: |
          {%- set entities = [
              'sensor.clean_doors', 
              'sensor.clean_drain', 
              'sensor.clean_fridge',
              'sensor.clean_behind_furnitures',
              'sensor.clean_humdifier'
            ] %}
          {%- for name in expand(entities) | selectattr('state', '<', utcnow().isoformat()) | map(attribute='name') %}
            Overdue Chore: {{ name }}
          {%- else %}
            No overdue chores today.
          {%- endfor %}
1 Like

Nice, but I doubt the else will work :wink:

Now, it didn’t do what I think the OP wanted it to do, either… Looks like bits and pieces of code patched together without actually understanding how it works.

Yes it will. That’s normal syntax, just rare

1 Like

if I paste this in template test

chores: |
            {% set sensor_entities = [
              'sensor.clean_doors', 
              'sensor.clean_drain', 
              'sensor.clean_fridge',
              'sensor.clean_behind_furnitures',
              'sensor.clean_humdifier'
            ] %}
            {% set current_date = now().date() %}
            {%- for sensor_entity in sensor_entities if as_datetime(states(sensor_entity)) > now() %}
                {% set chore_name = state_attr(sensor_entity, 'friendly_name') %}
                {{ "Overdue Chore: " + chore_name }}
              {% else %}
                {{ "No overdue chores today." }}
            {%- endfor %}

It says no overdue chores today

but at least clean_humdifer is the state 2023-08-02T13:00:51+00:00 , so it should show…

If I change the > to < it show all sensor…

What dates do those sensors contain?
Dates when cleaning is due, or…
What are the date from the other sensors?

Overdue should be using < because lower than now() is in the past. Greather than (>) now is in the future.

Did you try the template I posted?

1 Like

@petro I tried it, and it complains about a missing {% endfor %}

Seems you accidentally used {% endif %} instead of {% endfor %}

Your example in the screenshot helped me in finding out what what was wrong. Good to know you can use {% else %} in a for loop

when i try it in template i get an error

TemplateRuntimeError: No filter named ‘name’.

The sensors contain due date example
sensor.clean_humdifier state: 2023-08-02T13:00:51+00:00
sensor.clean_doors state: 2023-10-28T15:59:57+00:00

i tried just using only one sensor, it shows it correct then

           {% set sensor_entity = 'sensor.clean_humdifier' %}
            {% set current_date = now().date() %}
            {% set sensor_state = states(sensor_entity) %}
            {% if sensor_state %}
              {% set sensor_date = sensor_state %}
              {% if sensor_date <= current_date.strftime('%Y-%m-%d') %}
                {% set chore_name = state_attr(sensor_entity, 'friendly_name') %}
                {{ "Overdue Chore: " + chore_name }}
              {% else %}
                {{ "No overdue chores today." }}
              {% endif %}
            {% endif %}

Fixed

Thanks. Now it says No overdue chores today but it should show these to

what’s your current time? Do {{ now() }} in the template editor and paste the results here

2023-11-06 15:45:56.601114+01:00

You should use < here

{%- for sensor_entity in sensor_entities if as_datetime(states(sensor_entity)) > now() %}

As a chore will be overdue if the sensor datatime is lower than the current datetime

You said before it shows all then, what is the state of a chore you wouldn’t expect to show up (so is there a chore which is not overdue yet)?

he’s not using that template, he’s using this one →