Date check in an if else in an automation

I’m trying to send a notification to my phone the day before the actual date in a sensor.
So I have a sensor with a date value like this 2022-12-26 and I want to get the day before a notification.
But I’m wondering how I should trigger this automation script and how I can check when we are the day before the date in the sensor.
I think I can’t really do all of this just with the GUI and will probably need the yaml but that is still pretty new for me.
All advice/suggestions are welcome.

Seems you need a template solution for that … share the sensor data please as state./attribs in formatted(!) way

{{ states(‘sensor.pmd’) }}
Is this how I should do it?
if so this is the value I get: 2022-12-26

{{ as_datetime(states(‘sensor.pmd’)) - timedelta(days = 1) }}

That returns also the time like this
2022-12-25 00:00:00
Is there a function to only return the date?

Create an automation with a Time Trigger set to whatever time you want to be notified then use the following template in a Template Condition.

{{ (states('sensor.pmd') | as_datetime | as_local - timedelta(days=1)).date() == now().date() }}

I’m getting this message when using the first .date
<built-in method date of datetime.datetime object at 0x7f0842565ef0>
So because of that the == does not match with the now().date
Any idea what this message means?
Sorry for all the trouble, this yaml thing is confusing and don’t find that much information online, only older posts on this forum.

The first version I posted had a mistake. Look at the template again and you’ll see that it’s now called date()

Ah the brackets were missing.
It’s indeed working, tried with a bigger timedelta.
Thank you very much.

1 Like