Issues with a template after update to 2021-10-0

I know, that with the new version defaults values in templates needs to be defined…
Now, I am getting errors with the following block:

do I need to change the as_timestamp(…) into something like this?
as_timestamp(states('sensor.cert_expiry_timestamp_abc_de')) default ('00.00.0000')

s_cert_expiry_abc_de_days:
      friendly_name: 'abc.de exiration date'
      value_template: >
        {{ (as_timestamp(states('sensor.cert_expiry_timestamp_abc_de')) - as_timestamp(states('sensor.date.last_updated')) / 86400) | int }}
      icon_template: >-
        {% if states('sensor.s_cert_expiry_abc_de_days') | int > 0 %}
        mdi:certificate
        {% else %}
        mdi:certificate-outline
        {% endif %}
      unit_of_measurement: days

the following errors are thrown in the logs:

Logger: homeassistant.helpers.template
Source: helpers/template.py:1210
First occurred: 7. Oktober 2021, 10:40:35 (8 occurrences)
Last logged: 7. Oktober 2021, 10:40:35

    Template warning: 'as_timestamp' got invalid input 'unavailable' when rendering template '{{ ((as_timestamp(states('sensor.cert_expiry_timestamp_abc_de')) - as_timestamp(states.sensor.date.last_updated)) / 86400) | int }}' but no default was specified. Currently 'as_timestamp' will return 'None', however this template will fail to render in Home Assistant core 2021.12
Logger: homeassistant.helpers.event
Source: helpers/template.py:399
First occurred: 7. Oktober 2021, 10:40:35 (4 occurrences)
Last logged: 7. Oktober 2021, 10:40:35

    Error while processing template: Template("{{ ((as_timestamp(states('sensor.cert_expiry_timestamp_abc_de')) - as_timestamp(states.sensor.date.last_updated)) / 86400) | int }}")
	
	Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 397, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1604, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 513, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 399, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:73
Integration: Template (documentation, issues)
First occurred: 7. Oktober 2021, 10:40:35 (4 occurrences)
Last logged: 7. Oktober 2021, 10:40:35

    TemplateError('TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'') while processing template 'Template("{{ ((as_timestamp(states('sensor.cert_expiry_timestamp_abc_de')) - as_timestamp(states.sensor.date.last_updated)) / 86400) | int }}")' for attribute '_attr_native_value' in entity 'sensor.s_cert_expiry_abc_de_days'

Thanks a lot and with best regards,
Chris

Adding default won’t solve the underlying issue that you’re trying to do math on sensors that are unavailable.

Assuming sensor.cert_expiry_timestamp_abc_de is the culprit, the proper fix is to define availability on that one, so that the s_cert_expiry_abc_de_days won’t be rendered before the source sensor is available.

If you can’t, you can do something like

{% set cert_expiry = as_timestamp(states('sensor.cert_expiry_timestamp_abc_de'), None)  %}
{% if not cert_expiry %}
unavailable
{% else %}
...Normal calculation...
{% endif %}

thanks.
The interesting part is, that until the update - the sensors have worked without any issue :frowning:
Therefore, I thought - the sensor became “unavailable”, because something with the formatting was not correct…

also, cert_expiry_timestamp_abc_de IS available, when I check the DEVtools.

The issue seems to be the “sensor.date.last_updated” which I can’t find anymore…

If anybody else is looking for the solution, this works for me:

#############################
########  certbot  ##########
#############################
- platform: template
  sensors:
    days_before_ssl_expiration:
      friendly_name: Days before SSL expiration
      value_template: >-
       {{((as_timestamp(states('sensor.cert_expiry_timestamp_my_domain_name_8123')) - as_timestamp(now())) / 86400) | int }}
1 Like