and want to use device_class=timestamp, but I always get this error:
ValueError: Invalid datetime: sensor.infinisolar_energy_first_time has timestamp device class but provides state 2022-02-04T17:00:00+01:00:<class 'str'> resulting in ''str' object has no attribute 'tzinfo''
Did you use an LLM for this? This is the third or fourth use of astimezone() I’ve seen in the past week. It’s exactly the kind of thing that ChatGPT and the other bullshit engines do… provide answers that look feasible but are nonsense in context.
The timestamp sensor device class accepts datetime objects, so there’s no need for any further conversion after the strptime(). If the hour represented in the value is your local time, add the as_local filter:
No I got that from another topic of this forum.
I tried your template an got almost the same error:
ValueError: Invalid datetime: sensor.infinisolar_energy_first_time has timestamp device class but provides state 2022-02-04 17:00:00+01:00:<class 'str'> resulting in ''str' object has no attribute 'tzinfo''
Astimezone is a valid function on datetime objects. The code likely came from me. It only works in specific scenarios because we can’t create tz objects in jinja at the moment. It’s needed for specific calcs too.
What integration are you using? Some do not support the ability to make a timestamp sensor. It requires special code and each integration needs to implement it.
That still won’t work for snmp, there’s an open issue. It assumes the stored value is a datetime object but it doesn’t convert the template result to a datetime object.
I’ll probably fix it if I remember next week. It’s a pretty easy fix