Hi, I have the following template which should check the date returned by ‘sensor.bin_home_waste’ as a date and if that date is for tomorrow, return true:
{% set binDate = strptime(state_attr('sensor.bin_home_waste','next_collection'),"%Y-%m-%d") %}
{{ ((binDate|as_local - timedelta(days=1)).isoformat()) == today_at().isoformat() }}
I did have this working but now having issues as strptime requires a default value:
I have tried adding now() as the default value but that always then returns false because it is obviously return the time, now!
Any suggestions on how to get this working?
Thanks.
It’s only returning the time now() as the default, because your state attribute for sensor.bin_home_waste (next_collection) is returning something that can’t be converted to a datetime.
Perhaps you should have a look in the states page of developer tools and see what the state attribute actually is.
The state of the sensor is showing as the following:
I can’t see anything wrong and pasting it directly into the template editor works.
{% set d = "2024-01-19" %}
{% set binDate = strptime(d,"%Y-%m-%d") %}
{{ binDate }}
{{ ((binDate|as_local - timedelta(days=1)).isoformat()) == today_at().isoformat() }}
But I wonder if you could try -
{% set binDate = strptime(state_attr('sensor.bin_home_waste','next_collection')|trim,"%Y-%m-%d") %}
That seems to have worked, thanks:
I might have overcomplicated anyway as noticed that the sensor itself (not the attributes) changes to ‘Tomorrow’ when -1 on the collection day so could just build a notification for when this sensor equals ‘Tomorrow’.
Thanks for the help.
1 Like
Overcomplicating things is pretty much the norm in Home Automation haha. Don’t beat yourself up though, because you you’ve learned a more complicated way of doing things, which might not be necessary right now, but could be useful to you again in the future. Additionally - you have learned that trim might be necessary in case there are /n or /r embedded in the string - which you can’t detect easily.
1 Like
as_datetime()
will accept a %Y-%m-%d
formatted string…
{% set binDate = state_attr('sensor.bin_home_waste', 'next_collection') %}
{{ binDate|as_datetime|as_local - timedelta(days=1) == today_at() }}
1 Like