Need help with Template Default

Hey all,

Prior to 2022.6 update, all my templates were working fine, but now I get an error from not having any default value, despite my best efforts to enter a default value…

*> Error: In 'template' condition: ValueError: Template error: as_timestamp got invalid input 'states.sensor.last_boot.last_changed' when rendering template '{{ (as_timestamp(now())|int(default=0) - as_timestamp('states.sensor.last_boot.last_changed' | default(0)) | int > 300) | int(default="0")}}' but no default was specified*
*> *
*> Step ConfigChanged VariablesRelated logbook entries*

Can someone please tell me how to enter the default value in the correct way, so I can apply this to all my similar templates - and get things working as before(!?) :slight_smile:

Putting defaults is a nice-to-have and just a workaround. I assume the actual point of creating those errors is to make us realize there are cases where our templates just don’t work (or not as intended).

In you case states.sensor.last_boot.last_changed doesn’t have a proper value at the time of the error, whether because sensor.last_boot doesn’t exists, has a “unknown/unavailable” state or just didn’t change, yet.

You should guard against this with, e.g., an availability attribute.

I had another look at it in the Template editor and indeed was the template throwing an error.
Turns out the single quotes around ‘states.sensor.last_boot.last_changed’ were the reason why the correct data was not found.
With the quotes removed, all is now working fine again.

Thanks a bunch!

I have the same question as OP. Did you find the right syntax for a default value? I occasionally get this error when the sensor I’m using in a value template trigger is temporarily unavailable. This sensor does not have an availability attribute so I think it would make sense to use a default value of 0.

Template error: float got invalid input 'unavailable' when rendering template '{{states('sensor.grid_net_smoothed') | float | abs}}' but no default was specified

I figured it out.

 {% if states('sensor.grid_net_smoothed') is not number %}
    0
  {% else %}
    {{ states('sensor.grid_net_smoothed')|float | abs }}
  {% endif %}

Or, more simply and what the default is meant to do:

{{ states('sensor.grid_net_smoothed') |float(0) |abs }}

Documented here:

1 Like