Problems with a value template in a binary_sensor for future events

I have the problem that I like to build a binary_sensor which checks an upcoming event (tomorrow) of a CalDAV calendar. To do so I created the following sensor:

    urlaub_marco_tomorrow:
      friendly_name: "Urlaub Marco (morgen)"
      value_template: >-
        {% if now().timestamp() >= as_timestamp(state_attr('calendar.gemeinsam_urlaub_marco', 'start_time')) - 3600*24 and 
          now().timestamp() <= as_timestamp(state_attr('calendar.gemeinsam_urlaub_marco', 'end_time')) - 3600*24 %}
          on
        {% else %}
          off
        {% endif %}

As long as there is an event in the calendar on the next day there is no problem but as soon as there is no event I get warnings (which are absolutely correct) in my log:

Logger: homeassistant.helpers.template
Source: helpers/template.py:1291
First occurred: 14:18:11 (1108 occurrences)
Last logged: 16:58:00

Template warning: 'as_timestamp' got invalid input 'None' when rendering template '{% if now().timestamp() >= as_timestamp(state_attr('calendar.gemeinsam_urlaub_marco', 'start_time')) - 3600*24 and now().timestamp() <= as_timestamp(state_attr('calendar.gemeinsam_urlaub_marco', 'end_time')) - 3600*24 %} on {% else %} off {% endif %}' but no default was specified. Currently 'as_timestamp' will return 'None', however this template will fail to render in Home Assistant core 2022.1

I thought I could solve the problem with the else clause but obviously I don’t.

Is there a way to deal with this kind or problem or do anybody know another way how to react on future events of CalDAV calendars?

The error explains how to fix it. You can read this thread for details about using default.

It seems I am to stupid to understand this. I really can’t find a way to solve my problem by reading your solution.
Just to clarify: I am sure there is the solution. My problem is that I am really new in coding and right know I am searching for examples which could help me because the article itself isn’t clear to me :frowning:

there’s examples in the article. Read it from top to bottom. Don’t skim.

I did not. Am I blind?

OK, now I tried the following in template tool:

{{ as_timestamp(state_attr('calendar.gemeinsam_urlaub_marco', 'start_time'), 0) }}

As a result I get 0. Would it be an idea to use the value 0 in an elif clause?

If you’re just trying to get back to the way it used to be, just add the zero. If you want to catch them, then add the if statements

1 Like

Now I got you. Thanks :pray:

One final question :wink:

My “final solution” for my sensor is at follows:


    urlaub_marco_tomorrow:
      friendly_name: "Urlaub Marco (morgen)"
      value_template: >-
        {{ now().timestamp() >= as_timestamp(state_attr('calendar.gemeinsam_urlaub_marco', 'start_time'), now().timestamp() + 3600*24) - 3600*24 and
          now().timestamp() <= as_timestamp(state_attr('calendar.gemeinsam_urlaub_marco', 'end_time'), now().timestamp() + 3600*24) - 3600*24 }}

Is that the way you thought? It works, I just want to know whether there is a better solution (to improve my understanding of code).