Datetime need help to formating it for device_class: "date" or "timestamp"

I am using multiscrape to read a timestamp form a website:

      - unique_id: isr_zeitstempel_update
        name: "Zeitstempel_update"
        select: "*"
        value_template: >
          {% set time_1 = value | regex_findall_index(".*Zeit:[\s]+([\d\.:\s]+)\n", index=0) | string %}
          {% set time = strptime(time_1.strip(), "%d.%m.%Y %H:%M:%S") %}
          {{ (time).isoformat() }}
        device_class: "date"

I managed to create a proper datetime object using strptime and would like to have this information in my equipment.
Even tough I am using “.isoformat()” I get an error message:

sensor.isr_zeitstempel_update rendered invalid date 2023-10-23T21:16:01

How do I need convert my datetime object in order to have it accepted by my sensor?

I think the main issue is that you have set the device class to date, which expects just a date string, but you provided a datetime object. Try changing device class to timestamp.

good idea.

changed it to:

      - unique_id: isr_zeitstempel_update
        name: "Zeitstempel_update"
        select: "*"
        value_template: >
          {% set time_1 = value | regex_findall_index(".*Zeit:[\s]+([\d\.:\s]+)\n", index=0) | string %}
          {% set time = strptime(time_1.strip(), "%d.%m.%Y %H:%M:%S") %}
          {{ as_timestamp(time) }}
        device_class: "timestamp"

Error message:

sensor.isr_zeitstempel_update rendered invalid timestamp: 1698090668.0

Played around a little bit and I am totally confused.

In the documentation of the device_clases (Sensor - Home Assistant) find following:

  • date: Date string (ISO 8601)
  • timestamp: Datetime object or timestamp string (ISO 8601)

If I understand it correctly, device_class date requires a ISO formattet string: 2023-07-30T20:03:49.253717+00:00

I tried to build this string manually:

{% set test = as_timestamp(time) | timestamp_custom("%Y-%m-%dT%H:%M:%S.%f%z") %}

This string created looks like this:

2023-07-30T20:03:49.253717+0000

Unfortunately the “:” is missing in the time zone part. So how should I create such a ISO string?

OK - looking at the timestamp option. According to the documentation I provide a datetime-object or a timestamp.

So I tried the following:

# to use the datetime-object
{% set time = strptime(time_1.strip(), "%d.%m.%Y %H:%M:%S") %}
{{ time}}

# to provide the timestamp
{{ as_timestamp(time) }}

Both are not working, always complaining about the wrong input format.

I am really wondering what the right format is to get these device-classes up and running?

Any thoughts, help or ideas?

Now I am totally lost.

Finally I tried to provide the following code:

{{ time.isoformat(sep='T',timespec='microseconds')+"+02:00" }}

Which exactly returns the format as documented - BUT

sensor.isr_zeitstempel_update rendered invalid date # 2023-10-24T21:36:21.000000+02:00

What the heck is invalid with that time string?

        value_template: >
          {% set time_1 = value | regex_findall_index(".*Zeit:[\s]+([\d\.:\s]+)\n", index=0) | string %}
          {{ (strptime(time_1.strip(), "%d.%m.%Y %H:%M:%S") | as_local).isoformat() }}

Finally - try and error and lots of research in Google lead me to the solution.

The issue is that strptime is NOT parsing the provided timezone correctly.

Here is my test code:

{% set time_1 = "23.10.2023 20:23:43" %}

{% set time = strptime(time_1.strip()+" CET", "%d.%m.%Y %H:%M:%S %Z").astimezone()  %}
{{ time }}

time.tzinfo delivers “CEST” for my timezone.
If I remove “.astimezone()” from the second line the time.tzinfo delivers “null”.

Normally I would expect strptime to set the timezone correctly as it is provided in the string.

Anyway - it is working now an if anybody stumbles upon the same issue - this is the solution for a device_class: timestamp

@123
tried that also - results in the following error message:

[homeassistant.components.sensor.helpers] sensor.isr_zeitstempel_update rendered invalid date 2023-10-24T22:27:01+02:00

The template I posted above works for any timezone (because it employs as_local). Your version has a hard-coded timezone and fails to work in my timezone and is likely to report an incorrect time for you if your timezone observes DST.

The template is meant to be used with device_class: timestamp and uses the standard method isoformat() for formatting the datetime object. It’s the same method I have suggested to other users who have reported successful results.

Understood your point and thanks for the clarification.

However - as you see in the error message in my previous post, the string delivered from your suggested code looks pretty good and, according to the manual, is correct.

For whatever reason, multiscrape in combination with the device_class: timestamp does not accept this value.

The most confusing thing though is that the values from both templates are literally the same with different results.

I’ll test it again…

I overlooked the fact you had mentioned that this sensor is using the Multiscrape custom integration.

Looking at its documentation, the device_class option is missing. Perhaps it’s not supported. Report the issue to Multiscrape’s author.

So tested around and found that Multiscrape is working with device_class options even its not documented.

This is the working code:

          {% set time_1 = value | regex_findall_index(".*6800.Fehler.-[\s]+Historie.1.Datum\/Zeit:[\s]+([\d\.:\s]+)\n", index=0) | string %}
          {{ (strptime(time_1.strip(), "%d.%m.%Y %H:%M:%S") | as_local).isoformat() }}

At the end the code is exactly the same which was not working previously. There was a reboot of the HA Host in-between the tests - probably there was an issue with HA itself which has been fixed with the reboot.

Given that you’re using the template I had suggested, namely the one that works with any time zone (not just CET) and supports DST offset, please consider marking my post above with the Solution tag. It will direct other users to the correct solution to the original problem (error message due to incorrect template and device_class values).