TemplateError('ValueError: Template error: as_timestamp got invalid input

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'

Template:

- sensor:
    - name: "bwm_flur_last_motion"
      state: "{{ as_timestamp(states.binary_sensor.bwm_flur.last_changed)|timestamp_custom('%d.%m.%Y %H:%M') }}"
      icon: mdi:clock-time-four-outline

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?

:face_with_raised_eyebrow:

I linked you directly to the solution. You need to define defaults for your functions and filters for when the required values are not available.

Sorry, but I don’t understand. Now I added “default = 0” to the template sensor and the error message is: “… but no default was specified”.

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', default = 0) }}' 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', default = 0) }}")' for attribute '_attr_native_value' in entity 'sensor.bwm_flur_last_motion'

You haven’t specified a default on as_timestamp.

{{ as_timestamp(states.binary_sensor.bwm_flur.last_changed, default=0)|timestamp_custom('%d.%m.%Y %H:%M', default=0) }}
1 Like

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:

  1. It’s a brand new entity whose state value has never changed yet. :man_shrugging:
  2. The binary_sensor’s name is misspelled and bwm_flur doesn’t exist. :man_facepalming:

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’

The result is ok:

2022-06-17 16:04:48.812905+00:00

Yes, this is the solution. The warning message is gone. Many thanks.
:+1:

It works because your binary_sensor did not, at one point, have a value for last_changed.

Because it certainly has one now:

2022-06-17 16:04:48.812905+00:00

Which means the following template will work without even specifying a default value:

{{ states.binary_sensor.bwm_flur.last_changed.timestamp() | timestamp_custom('%d.%m.%Y %H:%M') }}

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 integration creates binary_sensor.bwm_flur?

binary_sensor.bwm_flur is a Template Webhook trigger sensor. It will be updated when the motion device send a webhook to HA.

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.

If I use the code above I get this error message:

Logger: homeassistant.helpers.template
Source: helpers/template.py:1862
First occurred: 11:51:46 (2 occurrences)
Last logged: 11:51:46

Template variable error: 'None' has no attribute 'last_changed' when rendering '{{ states.binary_sensor.bwm_flur.last_changed.timestamp() | timestamp_custom('%d.%m.%Y %H:%M') }}'

There’s something peculiar about that sensor, or your testing procedure, because you have reported that this template:

{{ states.binary_sensor.bwm_flur.last_changed }}

reports a value like this:

2022-06-17 16:04:48.812905+00:00

yet this template:

{{ states.binary_sensor.bwm_flur.last_changed.timestamp() | timestamp_custom('%d.%m.%Y %H:%M') }}

reports an error message:

'None' has no attribute 'last_changed'

which means binary_sensor.bwm_flur doesn’t even exist (it’s ‘None’).

  • If last_changed has no value, the first template will display nothing and the second template will fail.
  • If last_changed has a value, the first template will display it and the second template will convert it.

Your results are a mix of the two which makes no sense.

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:

service: script.samsung_alarm
data: {"device": "mobile_app_sm_s906b", "time":"{{ (as_timestamp(states('sensor.fajr')) - (2 * 60)) | timestamp_local() }}", "message":"time to pray Fajr!"}

but I get an error saying:

Error handling message: Error rendering data template: ValueError: Template error: as_timestamp got invalid input '03:19' when rendering template '{{ (as_timestamp(states('sensor.fajr')) - (1 * 60)) | timestamp_local() }}' but no default was specified (unknown_error)

What am I doing wrong?