Thank you! That works brilliantly.
I’m using {{ states('sensor.my_date')[-11:] }}
to get the 10/06/23
and it returns the string correctly.
Substituting it into your template thus…
{{ (strptime(states('sensor.my_date')[-11:], '%m/%d/%Y', today_at()) | as_local - today_at()).days }}
… it just returns 0
as the result.
Am I expecting too much of it?
Does it need to be done in two steps?
The original string takes the form "196 hrs or 10/06/2024"
This is what I’m pasting into the Template Editor:
{{ states('sensor.my_date')[:3] }}
{{ states('sensor.my_date')[-11:] }}
{{ (strptime(states('sensor.my_date')[-11:], '%m/%d/%Y', today_at()) | as_local - today_at()).days }}
{{ (strptime('10/06/2023', '%m/%d/%Y', today_at()) | as_local - today_at()).days }}
And that returns this:
196
10/06/2024
0
314
Which highlights another issue.
When the 196 hrs changes to 99 hrs the {{ states('sensor.my_date')[:3] }}
it will return "99 ", as there are only two digits. Same problem when it reaches 9.
Is there a way to extract the number up to the first “space”?