Template breaking change in HA 2021.10

Hi,

a breaking change for template behaviour was introduced recently.
among other warnings I get:

Template warning: 'as_timestamp' got invalid input 'unavailable' when rendering template '{{ ((as_timestamp(states('sensor.date_time').replace(',','')) | int) + 1*60) == ((as_timestamp(states('sensor.pixel_5_next_alarm')) | 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

Even tough, I tought it’s only related to ‘float’ I did a try and error and changed it to:

{{ ((as_timestamp(states('sensor.date_time').replace(',','')) | int(default=0)) + 1*60) == ((as_timestamp(states('sensor.pixel_5_next_alarm')) | int(default=0))) }}

However this doesn’t seem to do the trick:

Template warning: 'as_timestamp' got invalid input 'unavailable' when rendering template '{{ ((as_timestamp(states('sensor.date_time').replace(',','')) | int(default=0)) + 1*60) == ((as_timestamp(states('sensor.pixel_5_next_alarm')) | int(default=0))) }}' but no default was specified. Currently 'as_timestamp' will return 'None', however this template will fail to render in Home Assistant core 2021.12

Can someone point me to the right syntax?

thx and best,
Pete

as_timestamp is the function that has the issue, not the int.

e.g.

as_timestamp(value, default) 

Also, as timestamp returns an int. The int is redundant. So that can be removed.

{{ as_timestamp(states('sensor.date_time').replace(',',''), 0) + 60 == as_timestamp(states('sensor.pixel_5_next_alarm'), 0) }}
2 Likes

479 / 5000
Resultados de traducción
I have this sensor so that the ssl certificates are given to me in days instead of week and I can’t get it to work, can you help me please? Thank you.
`` `- platform: template
scan_interval: 86400
sensors:
cert_ssl:
friendly_name: ‘Ssl Certificate’
value_template:> -
{{(((as_timestamp (states.sensor.cert_expiry_timestamp_warrior_kick_duckdns_org_32123.state) | int) - (now (). timestamp () | int)) / 86400) | int | round (0)}}
unit_of_measurement: ‘days’```

I have tried with as_timestamp (0) but nothing, not default = 0

gracias por todo

Look through your template, find the as_timestamp and add the default in the appropriate spot. Use my previous example as a guide. If you can’t get it working, post what you tried here.

Also, please keep your posts in english. So use google translate before posting. Thanks.

sorry i thought i wrote it in english i apologize

Did you try altering the template?

yes is this ok

  scan_interval: 86400
  sensors:
    cert_ssl:
      friendly_name: 'Certificado Ssl'
      value_template: >-
        {{ ((((states.sensor.cert_expiry_timestamp_warrior_kick_duckdns_org_32123.state | as_timestamp(0))|int ) - (now().timestamp()|int)) / 86400) | int | round(0) }}
      unit_of_measurement: 'dias'```

That should work but use

states('sensor.cert_expiry_timestamp_warrior_kick_duckdns_org_32123')

Instead of

states.sensor.cert_expiry_timestamp_warrior_kick_duckdns_org_32123.state

See the warning here:

https://www.home-assistant.io/docs/configuration/templating/#states

I will, thanks it is an old sensor not adapted to what is now

  scan_interval: 86400
  sensors:
    cert_ssl:
      friendly_name: 'Certificado Ssl'
      value_template: >-
        {{ ((states('sensor.cert_expiry_timestamp_warrior_kick_org_32123') | as_timestamp(0))|int ) - (now().timestamp()|int)) / 86400) | int | round(0) }}
      unit_of_measurement: 'dias'````







I got this error TemplateSyntaxError: unexpected ')'

You have unmatched parentheses. Try this

 {{ ( ( states('sensor.cert_expiry_timestamp_warrior_kick_org_32123') | as_timestamp(0) - now().timestamp() ) / 86400 ) | round(0) }}

Also you don’t need to convert timestamps with |int They’re not strings.

so perfect, thanks again

1 Like

is this related? How do I fix this error

2022-02-10 20:42:07 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'int' got invalid input 'unavailable' when rendering template '{% if (states('sensor.power_ac_fronius_inverter_1_http_192_168_1_148') | int is integer) and (states('sensor.power_ac_fronius_inverter_1_http_192_168_1_148') | int - states('sensor.house_power_consumption') | int > 0) %}
          {% if (states('sensor.power_ac_fronius_inverter_1_http_192_168_1_148') | int is integer) and ((states('sensor.power_ac_fronius_inverter_1_http_192_168_1_148') | int - states('sensor.house_power_consumption') | int) > 5999) %}
            5999
          {% else %}
            {{ (states('sensor.power_ac_fronius_inverter_1_http_192_168_1_148') | int - states('sensor.house_power_consumption') | int) }}
          {% endif %}
        {% else %}
          0
        {% endif %}' but no default was specified. Currently 'int' will return '0', however this template will fail to render in Home Assistant core 2022.1

at night, there is no sun, so the fonrius inverter is off.

I finally got around it with

        {% if (states('sensor.power_ac_fronius_inverter_1_http_192_168_1_148') is not string) and (states('sensor.power_ac_fronius_inverter_1_http_192_168_1_148') | int - states('sensor.house_power_consumption') | int > 0) %}
          {% if (states('sensor.power_ac_fronius_inverter_1_http_192_168_1_148') is not string) and ((states('sensor.power_ac_fronius_inverter_1_http_192_168_1_148') | int - states('sensor.house_power_consumption') | int) > 5999) %}
            5999
          {% else %}
            {{ (states('sensor.power_ac_fronius_inverter_1_http_192_168_1_148') | int - states('sensor.house_power_consumption') | int) }}
          {% endif %}
        {% else %}
          0
        {% endif %}

incase someone else has the same issues! :slight_smile:

That’s a convoluted way of solving the issue.

All you had to do was supply default values for the |int filters you are using. e.g. instead of |int you could supply a default value of 0 like this |int(0), or none, like this |int(none).

You can read more here:

Mhttps://community.home-assistant.io/t/updating-templates-with-the-new-default-values-in-2021-10-x/346198

You should also add an availability template to render the state of your sensor unavailable if this happens. This will protect against erroneous Energy Dashboard readings.

e.g. https://community.home-assistant.io/t/how-to-prevent-entity-from-resetting-to-0/369363/2

In addition to what tom said, you also want to keep the conversions to 1 instance and not many instances.

{% set power_ac = states('sensor.power_ac_fronius_inverter_1_http_192_168_1_148') | int(none) %}
{% set consumption = states('sensor.house_power_consumption') | int(none) %}
{% if none in [ power_ac, consumption ] and power_ac - consumption <= 0 %}
  0
{% else %}
  {{ [ power_ac - consumption, 5999] | min }}
{% endif %}

This has 1 conversion from string to int for each sensor, and it’s functionally identical to your other method.

Some things to note, if statements have order of operations. When you use if x and y, it resolves things left to right. If the left arugment fails, it doesn’t resolve the right because the and will fail no matter the value of the right. The if statement above none in [ power_ac, consumption ] is resolved before power_ac - consumption <= 0, which ensures that power_ac - consumption <= 0 will be numbers when the first statement passes, but it will not error when it fails.

Hi, sorry for the resurrection, but related;

TemplateError(‘ValueError: Template error: as_timestamp got invalid input ‘None’ when rendering template ‘{% set time = state_attr(‘sensor.tide_bhb’, ‘next_high_tide_time’) %} {% set ftime = as_timestamp(time)|timestamp_custom(’%I:%M %p’) %} {{ ftime }}’ but no default was specified’) while processing template ‘Template<template=({% set time = state_attr(‘sensor.tide_bhb’, ‘next_high_tide_time’) %} {% set ftime = as_timestamp(time)|timestamp_custom(’%I:%M %p’) %} {{ ftime }}) renders=4>’ for attribute ‘_attr_native_value’ in entity ‘sensor.next_high_tide’

How did I fix this error?
My template sensor is:

{% set time = state_attr('sensor.tide_bhb', 'next_high_tide_time') %}
{% set ftime = as_timestamp(time)|timestamp_custom('%I:%M %p') %}
{{ ftime }}

Thanks!!

Is it as simple as:

{% set time = state_attr('sensor.tide_bhb', 'next_high_tide_time') %}
{% set ftime = as_timestamp(time, 0)|timestamp_custom('%I:%M %p') %}
{{ ftime }}

Yes, you just needed to provide a default to the as_timestamp function, in the event that the data passed to it was invalid.

1 Like