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?
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