Check date against current day

I want to be able to control what text appears.

  1. If the date in 'input-datetime.sprinklers_last_run is the same as todays date, I would like text to say “Today”.

  2. If the date was yesterday, “Yesterday”

  3. Any other date, just list the date.

Heres the current code that just shows the date.

Last run {{ states('input_datetime.sprinkler_last_run') }}


And heres a very rudimentary idea of what the code should be but clearly I dont know what to put in.

{% if is_state("input_datetime.sprinkler_last_run", "Check to see if date matches today") %}
      Last run today
{% elif is_state("input_datetime.sprinkler_last_run", "Check to see if date matches yesterday") %}
      Last run yesterday
{% elif is_state("input_datetime.sprinkler_last_run", "???") %}
      Last run {{ states('input_datetime.sprinkler_last_run') }}
{% endif %}

These two should give you today and tomorrow.
Sorry for not typing it all but I’m using my phone so it’s hard work.

{{ state_attr('input_datetime.sprinkler_last_run', 'timestamp') | timestamp_custom("%Y-%m-%d") == states('') }}
{{ strptime(states('input_datetime.sprinkler_last_run'), '%Y-%m-%d %H:%M:%S' )+ timedelta(days = 1)  == now() + timedelta(days = 1) }}


Appreciate it. Mumbo jumbo to me but it appears to be working!



Spoke too soon. “TODAY” works but when i changed the helper to yesterdays date, this is what happens. Im only entering a date, not time into the helper. Trying to understand your code for yesterday, it appears to be looking for time?

Yes off course it’s not working…

I add days to both the helper and now :man_facepalming:

I believe it should be {{ helper == now - timeselta() }}

No timeselta on the helper side and minus day on now.

Believe I made the changes you said but it still wont show me yesterday when the date in the helper is yesterday. It just shows the entire code.

{% if  state_attr('input_datetime.planter_box_watering_last_run', 'timestamp') | timestamp_custom("%Y-%m-%d") == states('')  %}
      Last run today
{% elif strptime(states('input_datetime.planter_box_watering_last_run'), '%Y-%m-%d %H:%M:%S' ) == now() - timedelta(days = 1) %}
      Last run yesterday
{% else %}
      Last run {{ states('input_datetime.planter_box_watering_last_run') }}
{% endif %}


{% set days = {timedelta(days=-1): 'yesterday', timedelta(days=0): 'today'} %}
{% set t = states('input_datetime.sprinkler_last_run') %}
Last run {{ days.get(t | as_datetime | as_local - today_at(), t) }}
1 Like

This seems to have done the trick. Thank you!

1 Like