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.
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?
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.
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).