What is wrong with the following equation

Hello everyone,

I have following equation:

{{ (now().date() + timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S") == (strptime(states.sensor.limburg_net_first_next .attributes.Upcoming_day + " 2023", '%d %b %Y'))}}

This results in False.
However, when I take them apart I get the same result:

{{ (now().date() + timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S")}}

results in: 2023-06-01 00:00:00

{{strptime(states.sensor.limburg_net_first_next.attributes.Upcoming_day + " 2023", '%d %b %Y')}}

results in: 2023-06-01 00:00:00

What am I overlooking here?

Thank you for your response.

Is the blank between “next” and the dot a c/p error?

Thank you for your response, but apparently this is not the problem.

{% set upcoming = state_attr('sensor.limburg_net_first_next', 'Upcoming_day') %}
{{ (now()+timedelta(days=1)).strftime('%d %b %Y')  == upcoming + now().strftime(' %Y') }}

Thanks for your response, I tried this but the result is still False:

What does state_attr('sensor.limburg_net_first_next', 'Upcoming_day') return?

It returns:
01 jun

Thanks for your response,
But the result of both is exactly the same but the comparison still returns False. I do not understand it either!!

Hahah, Hold on, one is capitalized and the other is not!!!

Yep, add | title:

{% set upcoming = state_attr('sensor.limburg_net_first_next', 'Upcoming_day') %}
{{ (now()+timedelta(days=1)).strftime('%d %b %Y')  == upcoming | title + now().strftime(' %Y') }}

Thank you very much, this solved the problem!!
But don’t understand that my first equation didn’t work, Can you please explain that to me?? Maybe I’ll learn something from that.

The issue is that you’re comparing oranges to tangerines… they look similar, but inside they’re different.

The strptime() function takes a string and format information and returns a datetime object, while strftime() takes a datetime object and format information to return a string… so in the end you are comparing a string to a datetime object. You basically just over converted.

Additionally, one is side timezone aware and the other is timezone naive. This wouldn’t matter if both sides were strings since they are WYSIWYG, but it does matter for datetime objects.

Another equation, that is similar to your original would be the following, where we don’t convert the left hand side of the equation.

{% set upcoming = state_attr('sensor.limburg_net_first_next', 'Upcoming_day') %}
{{ today_at()  + timedelta(days=1) == strptime(upcoming + " 2023", '%d %b %Y') | as_local}}

Thank you very much for this explanation.
I’ll take a closer look at this one, because at first sight I don’t understand it at all!! I’m still fairly new here but trying to learn as much as possible. Do you know where there is a good course to understand all this a little better.

There’s no single source that covers all the things you can do with templates in HA, but make sure to check out:

Home Assistant Templating: This is the the most comprehensive source for HA-specific functions.

Jinja Docs: As you may have discovered, these docs are not geared toward new/casual users, but they contain a lot of important information.

Jinja for Ninjas: @skalavala has put together a well organized tutorial and group of examples. Since the HA devs and community are regularly adding template functions, there may be better/easier ways to do some of the things shown, but it’s still a great source. They also have a Jinja Cheatsheet you can download.

Many Python methods also to work in templates. I’ve never seen a complete list of which methods work and which don’t… But the Official Python docs have come in handy for me multiple times to figure out what’s going on in templates posted by some of the more advanced users on this forum.

1 Like