Correct in Template wrong error when loaded

can someone please help.
I’ve trying to keep track of when my certs are expected to expire

I ran the following in the template and got the expected result “73”.

{{ ((as_timestamp(states('sensor.cert_expiry_timestamp_MYSITE')) - now().timestamp()) / 86400) | round(0,default='none') }}"

So I loaded the below into HA

- platform: template
       value_template: "{{ ((as_timestamp(states('sensor.cert_expiry_timestamp_MYSITE')) - now().timestamp()) / 86400) | round(0,default='none') }}"
       unit_of_measurement: days
       friendly_name: MYSITE SSL Cert

Yet I why do I get the following error

  • TemplateError(‘TypeError: unsupported operand type(s) for -: ‘NoneType’ and ‘float’’) while processing template 'Template("{{ ((as_timestamp(states(‘sensor.cert_expiry_timestamp_MYSITE)) - now().timestamp()) / 86400) | round(0,default=‘none’) }}")’
    for attribute ‘_attr_native_value’ in entity ‘sensor.MYSITE_cert_expire’

Its just a guess, but when you tested it you have a numeric value in sensor.cert_expiry_timestamp_MYSITE.
When you then made the sensor and restarted HA, then the sensor had unavailable as value, which then made it fail.

because on restart the state isn’t correct. You’ll also notice that you have a paired warning with that template explaining that this could happen. I’m guessing you’re ignoring that? This is why you should use default values for as_timestamp.

Anyways, here’s a template that does the same thing but uses all the new template time functions.

{% set expiry = states('sensor.cert_expiry_timestamp_MYSITE') | as_datetime or now() %}
{{ (expiry - now()).days }}

so would that mean the “sensor.cert_expiry_timestamp_MYSITE” sensor hadn’t correctly loaded when my sensor template tried to read it.
more misunderstood than ignored, as when i look at the cert_expiry sensor it’s showing me a date.

thank you for your assistance.

It doesn’t show that at startup. It will be unavailable at startup, like every sensor. You always have to account for that with template entities.

If you test this it produces none

{{ states('sensor.zilch') | as_datetime | default(now())  }}

I’ve found the default filter to be very specific about what it considers to be undefined. In this case, the output of states is unknown which as_datetime reports as none which default doesn’t consider to be undefined so it just passes it along and the final result is none. :thinking: :man_shrugging:

In contrast, this reports the current date and time because the variable foo is truly undefined.

{{ foo | default(now()) }}

This will also report the current date and time:

{{ states.sensor.zilch.state | default(now()) }}

In practice, I’ve found it challenging to employ default because most of the templates I have created always supply it with something even when that something is none. :slightly_smiling_face:

1 Like

ah yes, i forgot about that. So i use this in my setup

{{ states('sensor.zilch') | as_datetime or now()  }}
1 Like