Hi all,
I’ve got an issue I can’t seem to get to the bottom of with strptime.
I have a sensor that has a datetime format of yyyy-mm-dd hh-mm-ss as you can see from the screenshots below.
What I can’t work out is why I am getting an error with my code here:
{{ state_attr('sensor.obc_tides', 'last_tide_time') | as_datetime | as_timestamp }}
If you take a look at the screenshot, it’s complaining about the input. I copied that input and put it into the same template without the state_attr call as:
{{ '2024-02-23 13:41:00' | as_datetime }}
and it works! I’m presuming there is something wrong with the sensor config but can’t work out what it might be.
In this image you can see the values of each:
This is my sensor I’m using:
Any thoughts?
tom_l
February 23, 2024, 1:04am
2
As this is an attribute:
state_attr('sensor.obc_tides', 'last_tide_time')
It may already be a datetime object. Attributes can be types other than string (unlike states).
Be careful with what the template editor reports as object type too. It is interpreted and not always accurate.
What happens if you put this in the template editor:
{{ state_attr('sensor.obc_tides', 'last_tide_time')|as_timestamp }}
Ahh, I think you may be right as the attribute has the “T” when looking in the states page.
However I wasn’t expecting this to be the result of your code?
Sorry, that was me responding on my phone before realising what you fully meant.
Tried with:
{{ as_timestamp(state_attr('sensor.obc_tides', 'last_tide_time')) }}
and works perfectly, Thank you very much!