What did they change in datetime/strptime/timestamp to bork my template binary sensor?

Hi all,

I’m hoping someone knows off the top of their head what they changed since about April '22 which broke my template sensor which is simply supposed to return a “true” if there is a calendar event in the next 2 days, and a “false” otherwise.

  - platform: template
    sensors:
      reminders_label:
        value_template: >-
          {{(((( as_timestamp(strptime(state_attr('calendar.alena_and_seans_calendar', 'start_time'),"%F %T")) - as_timestamp(now())) / 86400)|round(2)) < 2)  }}

If I put just

{{ as_timestamp(strptime(state_attr('calendar.alena_and_seans_calendar', 'start_time'),"%F %T")) }}

into the template editor, I get the error:

ValueError: Template error: strptime got invalid input '2022-09-04 13:00:00' when rendering template '{{ as_timestamp(strptime(state_attr('calendar.alena_and_seans_calendar', 'start_time'),"%F %T")) }}' but no default was specified

So I’m guessing the output from the state_attr has somehow changed it’s type/format, but I’m not sure how, so I’m not sure what to do to fix it. Anyone have any idea?

Just need to add default at the end I believe

 86400)|round(2)) < 2) | default() }}

Or perhaps default(none)

1 Like

Tried both with default() and default(none) and it produces the same error. What is the default acomplishing?

strptime(state_attr('calendar.andy_calendar','start_time'),'%Y-%m-%d %H:%M:%S')

This works for me (for coping with the parsing part of the template)

Incidentally I looked at datetime — Basic date and time types — Python 3.8.13 documentation and I can’t see where %F %T came from…

%F and %T are there
https://cplusplus.com/reference/ctime/strftime/

%T	ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S	14:55:02

%F Short YYYY-MM-DD date, equivalent to %Y-%m-%d 2001-08-23

default() is supposed to set the “unknown value” to my understanding. I am not very proficient in templating I must say. I think you can do … default if broken text then make fixed text…
But to be honest I would usually have to ask…

meh also tired.

your timestamp is wrong format
needs to be YYYYmmddTHHMMSSz
you needs a separator T and tz info

'2022-09-04T13:00:00+0000'

Interesting, that format will have never worked. You’ve just been getting lucky.

Anyways, here’s a simpler approach.

{{ (state_attr('calendar.alena_and_seans_calendar', 'start_time') | as_datetime | as_local - now()).days < 2 }}

If you’re dead set on using as_timestamp…

{{ (as_timestamp(state_attr('calendar.alena_and_seans_calendar', 'start_time')) - now().timestamp()) / 86400 < 2 }}

It worked for more than a year, but I guess I was just getting lucky the whole time.

This one worked:

{{ (state_attr('calendar.alena_and_seans_calendar', 'start_time') | as_datetime | as_local - now()).days < 2 }}

I’ll take it, and I’m glad that I know I’m doing it right now. Thank you.

What does the as_local do?

as local time instead of gmt (Greenwich mean time or utc basically)

1 Like