Hello, I have a problem with a template. I mean in earlier HA versions there was no error message. The actually version is core-2022.6.6.
Unfortunately, I can’t get along with the template programming. Maybe someone can help me with this?
Log:
TemplateError('ValueError: Template error: as_timestamp got invalid input '' when rendering template '{{ as_timestamp(states.binary_sensor.bwm_flur.last_changed)|timestamp_custom('%d.%m.%Y %H:%M') }}' but no default was specified') while processing template 'Template("{{ as_timestamp(states.binary_sensor.bwm_flur.last_changed)|timestamp_custom('%d.%m.%Y %H:%M') }}")' for attribute '_attr_native_value' in entity 'sensor.bwm_flur_last_motion'
Thanks for the link. I also visited the docs for templating. Unfortunately I have not found a solution to my problem.
When I enter the code in the HA developer templating, I get the time displayed without errors. The template itself works too. But why do I get the described messages after restarting HA. Obviously there is something wrong with the code. I was hoping to get help here?
The root cause of this issue appears to be that binary_sensor.bwm_flur has no value for last_changed. As a consequence, the template’s as_timestamp is unable to produce a timestamp. In addition, as_timestamp doesn’t contain a default value so the final result is that Home Assistant reports it as an error.
In previous versions, Home Assistant would simply warn about the missing default value but not any more (now it’s reported as an error).
I can’t think of many reasons why an entity would lack a last_changed value other than:
It’s a brand new entity whose state value has never changed yet.
The binary_sensor’s name is misspelled and bwm_flur doesn’t exist.
Copy-paste this into the Template Editor and let us know what it produces:
{{ states.binary_sensor.bwm_flur.last_changed }}
If it reports this then the entity doesn’t exist:
UndefinedError: ‘None’ has no attribute ‘last_changed’
Basically, your original template would have worked the moment the binary_sensor acquired a last_changed value. Once it gets one, it is stored and, going forward, always has a last_changed value.
Which means it had no value for last_updated before it received its first webhook from the motion detector.
FWIW, it’s the same situation for a new automation. It won’t a last_triggered value before its first trigger. After that first trigger, it will always have a last_triggered value.
Can I just ask:
I have the following sensor.fajr which reports time i.e. in this format 03:19. When I do: {{ states.sensor.fajr.timestamp() | timestamp_custom('%H:%M') }} I get UndefinedError: 'homeassistant.helpers.template.TemplateState object' has no attribute 'timestamp'.
When I do {{ states.sensor.fajr }} I get:
Result type: string
<template TemplateState(<state sensor.fajr=03:19; icon=mdi:clock, friendly_name=Fajr @ 2023-05-04T00:05:17.240375+01:00>)>
This template does not listen for any events and will not update automatically.
I am trying to subtract 5 mins from the time shown in the sensor.fajr. I tried to use timestamp by doing this as an example: