I have a template sensor setup to display my last made auto backup in a readable format (ex. “2 days ago”). After upgrading to 2021.10 I’m getting the log message that I need to add a default to the template for it to keep working in future Home Assistant versions. I’ve watched the release party and read the docs, but I’m not versed enough in templates to figure out where to add it. Below is the template sensor, I tried adding the default but it’s incorrect:
{%- set time = (as_timestamp(now()) - as_timestamp(state_attr('sensor.samba_backup', 'last_backup'))) | int(default=0) %}
{%- set minutes = ((time % 3600) // 60) %}
{%- set minutes = '{} minutes ago'.format(minutes) %}
{%- set hours = ((time % 86400) // 3600) %}
{%- set hours = '{} hours ago'.format(hours) %}
{%- set days = (time // 86400) %}
{%- set days = '{} days ago'.format(days) %}
{% if time < 60 %}
{{ 'Less than 1 minute ago' }}
{% elif time < 120 %}
{{ '1 minute ago' }}
{% elif time < 3600 %}
{{ minutes }}
{% elif time < 7200 %}
{{ '1 hour ago' }}
{% elif time < 86400 %}
{{ hours }}
{% elif time < 172800 %}
{{ '1 day ago' }}
{% else %}
{{ days }}
{% endif %}
And the error message:
Template warning: 'as_timestamp' got invalid input 'None' when rendering template '{%- set time = (as_timestamp(now()) - as_timestamp(state_attr('sensor.samba_backup', 'last_backup'))) | int(default=0) %} {%- set minutes = ((time % 3600) // 60) %} {%- set minutes = '{} minutes ago'.format(minutes) %} {%- set hours = ((time % 86400) // 3600) %} {%- set hours = '{} hours ago'.format(hours) %} {%- set days = (time // 86400) %} {%- set days = '{} days ago'.format(days) %} {% if time < 60 %} {{ 'Less than 1 minute ago' }} {% elif time < 120 %} {{ '1 minute ago' }} {% elif time < 3600 %} {{ minutes }} {% elif time < 7200 %} {{ '1 hour ago' }} {% elif time < 86400 %} {{ hours }} {% elif time < 172800 %} {{ '1 day ago' }} {% else %} {{ days }} {% endif %}' but no default was specified. Currently 'as_timestamp' will return 'None', however this template will fail to render in Home Assistant core 2021.12
@tom_l That’s amazing, thank you! Just shows how inexperienced I still am with templating. Having said that, I still get a similar error message. Not 100% sure but it seems to only occur at boot:
Template warning: 'strptime' got invalid input 'None' when rendering template '{{ relative_time( strptime( state_attr('sensor.samba_backup', 'last_backup'), '%Y-%m-%d %H:%M') ) }}' but no default was specified. Currently 'strptime' will return 'None', however this template will fail to render in Home Assistant core 2021.12
@Hellis81 the sensor does exist, using dev tools I get the following. Perhaps it is unavailable during boot?
Template warning: ‘strptime’ got invalid input ‘2021-10-07 07:49:14.573922+00:00’ when rendering template ‘{% set format_str = ‘%d.%m.%y %H:%M:%S’ %} {% set last_triggered_str = as_timestamp(strptime(state_attr(‘automation.heiman_last_action_emergency’, ‘last_triggered’), ‘%Y-%m-%d %H:%M:%S’)) | timestamp_custom(format_str) %} {{ (now().replace(tzinfo=None) - strptime(last_triggered_str, format_str)).total_seconds() > 10 }}’ but no default was specified. Currently ‘strptime’ will return ‘2021-10-07 07:49:14.573922+00:00’, however this template will fail to render in Home Assistant core 2021.12
I’m struggling with ‘strptime’ too, directly after a restart of HA:
2021-10-08 15:37:07 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'strptime' got invalid input 'unknown' when rendering template '{{ relative_time(strptime(states('sensor.octopi_last_boot'), '%Y-%m-%dT%H:%M:%S%z' )) }}' but no default was specified. Currently 'strptime' will return 'unknown', however this template will fail to render in Home Assistant core 2021.12
Add a default value to strptime. That’s what the warning message is reporting: “no default was specified”.
For example, if strptime is unable to convert the sensor’s value (perhaps because the sensor is unavailable) this will report the current time and date as the default value.
{{strptime(states('sensor.octopi_last_boot'), '%Y-%m-%dT%H:%M:%S%z', now())}}
^
|
Whatever you put here
represents the default value
That’s just an example. What you want it to report as the default value is your choice.
@123 The eternal battle with autocorrect continues
@petro That looks good. Is strptime deprecated or just not advised? Using your code I no longer seem to get the default warning (yay!), but I do get a new one:
TemplateError('TypeError: argument 1 must be str, not None') while processing template 'Template("{{ relative_time( state_attr('sensor.samba_backup', 'last_backup') | as_datetime) }}")' for attribute '_attr_native_value' in entity 'sensor.samba_relative_backup'
The purpose of strptime is to convert a datetime string into a datetime object. For your example, the datetime string reported by sensor.octopi_last_boot is already in a format that is easily converted to a datetime object using as_datetime.
The last_backup attribute already contains a datetime object so attempting to use as_datetime causes the error message you received. Remove as_datetime from the template.
relative time requires a datetime object. Your attribute appears to be a string, not a datetime object. A datetime object would be fully fleshed out, meaning it would contain seconds.
What petro said. In this case, last_backup attribute doesn’t actually contain a datetime object. It contains a datetime string without all the necessary parts to be understood as a datetime object.
You will need to investigate why the template reports a value in the screenshot (15 hours) but failed previously with the TempleError message indicating that last_backup was None. If that happened at startup then it’s possible that sensor.samba_backup was unavailable or unknown.