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