keithHill
(Keith Hill)
March 13, 2024, 5:58pm
1
I am trying to write an automation and I think I’m missing something in trying to do this comparison. Here is what I currently have:
now() >= state_attr('sensor.crew_soccer', 'date') - timedelta(hours=24)
I want the automation to trigger when it is within 24 hours and I’m guessing I have syntax wrong here and I can’t figure it out.
In order to accurately help you, we need to know whether the attribute is a datetime object or just a string. What is returned when you paste the following into the Template Editor?
{{ state_attr('sensor.crew_soccer', 'date') - timedelta(hours=24) }}
keithHill
(Keith Hill)
March 13, 2024, 6:32pm
4
sorry, still new to this. I had a formatting issue. So it looks like it’s stored as a string, so I need to convert it I guess.
now() > strptime(state_attr('sensor.crew_soccer', 'date'),"%Y-%m-%dT%H:%MZ") - timedelta(hours=24)
results in:
TypeError: can’t compare offset-naive and offset-aware datetimes
keithHill
(Keith Hill)
March 13, 2024, 6:58pm
5
I think I got it!
utcnow() > state_attr('sensor.crew_soccer', 'date')| as_datetime - timedelta(hours=24)