Templating Help - Defaults

Hi All

I know this question regarding template and defaults gets asked a lot, so apologies in advance.
I have read 101 post about this subject and get most of it but dont know where I am missing the defaults from the following, so any advice appreciated as I have now given up.

Template:

{% set timenow = now().timestamp() %}
{% set event = state_attr('calendar.general_bin', 'start_time') | as_timestamp %}
{% set delta = (event - timenow) | int %}
{% if delta  <= 0 %} off {% elif delta <= 28800 %} on
{% else %} off {% endif %}

Error:

TemplateError(ā€˜ValueError: Template error: as_timestamp got invalid input ā€˜Noneā€™ when rendering template ā€˜{% set timenow = now().timestamp() %} {% set event = state_attr(ā€˜calendar.general_binā€™, ā€˜start_timeā€™) | as_timestamp %} {% set delta = (event - timenow) | int %} {% if delta <= 0 %} off {% elif delta <= 28800 %} on {% else %} off {% endif %}ā€™ but no default was specifiedā€™) while processing template ā€˜Template(ā€œ{% set timenow = now().timestamp() %} {% set event = state_attr(ā€˜calendar.general_binā€™, ā€˜start_timeā€™) | as_timestamp %} {% set delta = (event - timenow) | int %} {% if delta <= 0 %} off {% elif delta <= 28800 %} on {% else %} off {% endif %}ā€)ā€™ for attribute ā€˜_stateā€™ in entity ā€˜binary_sensor.general_bin_tomorrowā€™

Thanks in advance, I know its related to timestamp but failing to see where to put a default in.

Ross

You donā€™t need to use timestamp at allā€¦

template:
  binary_sensor:
    state: >
      {% set event = state_attr('calendar.general_bin', 'start_time') | as_datetime | as_local %}
      {% set delta = event - now() %}
      {{ iif(timedelta(hours=8) < delta or delta <= timedelta(), 'off', 'on') }}
    availability: >  
      {{ state_attr('calendar.general_bin', 'start_time') is not none }}

If you want to continue using as_timestamp:

1 Like

Thanks @Didgeridrew

When back in front of PC will change them to your suggestion and recheck.

This is how they have been for some time with no issues and or errors, although they are still rendering correctly, I assume the new errors are due to the 2022.6 update and having no default set.

Thanks again, appreciate the feedback