Error rendering data template: ValueError: Template error: as_timestamp got invalid input '' when rendering template

Have looked at the topics here with the same error but cannot make anything out of it… :blush:
I get this error:

Error rendering data template: ValueError: Template error: as_timestamp got invalid input '' when rendering template 
<template>
but no default was specified

This is the template:

    {% set minutes = (as_timestamp(states.calendar.personal_personal.attributes.start_time) |
    timestamp_custom('%-M') |int(0)) %}           In personal calendar . {% set midnight = today_at() %} {%     set event =
    state_attr('calendar.personal_personal', 'start_time') | as_datetime |
    as_local %} {% set delta = event - midnight %} {% if delta.days == 0 %}
      Today
    {% elif delta.days == 1 %}
      Tomorrow
    {% elif delta.days == 2 %}
      The day after tomorrow
    {% else %}
      In {{ delta.days }} Days
    {% endif %}.  . at
    {{as_timestamp(states.calendar.personal_personal.attributes.start_time) |
    timestamp_custom('%-H') }} {%- if minutes > 0 -%}  {{minutes}} {%- endif
    %} . {{state_attr('calendar.personal_personal', 'message')}} .

This has worked though… :thinking:

Thanks for any help!

Only because you were lucky.

Your template makes a direct reference to the start_time attribute of calendar.personal_personal. This is not a recommended practice because that attribute doesn’t always exist. In addition, the information that’s provided can be misleading if there are multiple events scheduled at the same time or if there’s an All Day event (it will override all other events scheduled on that day).

The recommended technique is to use a Calendar Trigger or use the calendar.get_events service call and then process the resulting response_variable.

If you just want a quick fix now, first check if states.calendar.personal_personal.attributes.start_time is defined before attempting to use as_timestamp. Or, better yet, use state_attr('calendar.personal_personal', 'start_time') and confirm it doesn’t return none (because that’s what it will return if the attribute doesn’t exist).

1 Like

Thank you Taras for jumping in!

Yeah, sometimes I am :star_struck:

I was using the calendar.get_events and use the response_variable as the condition to process the template in the OP like this

{{ personalcalendar['calendar.personal_personal'].events | count > 0 }}

but not using the results in my template.
I think I have got it right now, the hard way.

One additional question: I have noticed that somehow these templates get rearranged so the readability becomes worse (for me at least)
Is there a way to prevent that?