Template error: strptime got invalid input 'None'

I would like to extract time from an calendar timestamp. I have read - I guess - all topics regarding this problem but didn’t find a working solution.

Entering following command under Developer Tools - Template gives a valid time like 18:00

{{ as_timestamp(strptime(state_attr('calendar.lokaler_kalender', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%H:%M") }}

In an automation I get this error

Error rendering data template: ValueError: Template error: strptime got invalid input ‘None’ when rendering template ‘{{ trigger.calendar_event.summary }} um {{ as_timestamp(strptime(state_attr(‘trigger.calendar_event’, ‘start_time’),’%Y-%m-%d %H:%M:%S’)) | timestamp_custom(“%-H:%M”) }}’ but no default was specified

I tried several times to insert a default but I didn’t get any valid solution.

Finally I found a solution. But it just gets the now(0)-value :frowning:

service: persistent_notification.create
data:
  message: >
    {{ trigger.calendar_event.summary }} um  {{
    as_timestamp(strptime(state_attr('trigger.calendar_event', 'start_time'),
    '%Y-%m-%d %H:%M:%S', now())) | timestamp_custom("%H:%M")  }}

1 Like

Simplify your template:

{{ (state_attr('calendar.lokaler_kalender', 'start_time')|as_datetime).strftime("%H:%M") }}

Sorry, my solution just returns the now() value :frowning:

You do not need to use the state_attr() function on the trigger variable to get the start time.

service: persistent_notification.create
data:
  message: >
    {{ trigger.calendar_event.summary }} um
    {{ (trigger.calendar_event.start | as_datetime).strftime("%H:%M") }}

Thanks a lot! It’s working :slight_smile: