Automation condition match with timestamp of a sensor

Hi all,

I’m still very new to using Home Assistant so I can really use a little bit of help with creating a slightly more complex automation rule.

I installed an integration from my local trash collect schedule (Avri) so I now have three sensors for each trash type filled with a value of the next collect date.

What I want is to run a specific action on the evening BEFORE that specific date on a specific time.
So I need to have some sort of condition comparing the current day with the one in the sensor and subtract a whole day from it.

How would I accomplish such task?

Thanks for your help.

What is the format of the collection date? Is it in ISO format like this: 2020-11-18

The condition will use a template to convert the collection date from a string value to a datetime object. It adds a day to the current date (as a datetime object) and compares it to the collection date. If they match then today is the day before the collection day.

Thanks for the quick reply.

Yes the current value for one of the sensors is currently: “2020-11-23”

How would such a template condition look like?

Paste this into the Template Editor to understand how the strptime function converts a date represented as a string to a datetime object:

{{ '2020-11-23' }}
{{ '2020-11-23' is string }}
{{ strptime('2020-11-23', '%Y-%m-%d').date() }}
{{ strptime('2020-11-23', '%Y-%m-%d').date() is string }}

Screenshot from 2020-11-16 09-25-45

The following template takes today’s date and adds one day to it (the result is a datetime object) then compares it to the value of sensor.collection (after it has been converted to a datetime object).

{{ now().date() + timedelta(days=1) == strptime(states('sensor.collection'), '%Y-%m-%d').date() }}

The template will evaluate to true only if today’s date is one day before the collection date. It can be used as a Template Condition in an automation:

- alias: 'Example 1'
  trigger:
    platform: time
    at: '18:00:00'
  condition:
    condition: template
    value_template: >
      {{ now().date() + timedelta(days=1) == strptime(states('sensor.collection'), '%Y-%m-%d').date() }}
  action:
    service: notify.whatever
    data:
      title: 'Recycling tomorrow!'
      message: "Put out the recycling this evening for tomorrow's collection."

Alternately, you can use the new abbreviated style of defining a Template Condition:

- alias: 'Example 2'
  trigger:
    platform: time
    at: '18:00:00'
  condition: "{{ now().date() + timedelta(days=1) == strptime(states('sensor.collection'), '%Y-%m-%d').date() }}"
  action:
    service: notify.whatever
    data:
      title: 'Recycling tomorrow!'
      message: "Put out the recycling this evening for tomorrow's collection."

There is also the possibility of using a Template Trigger. It would not need a condition because the trigger would do both:

  • check if the current time is equal to the scheduled time
  • check if today’s date is one day before the collection date

It would look something like this:

- alias: 'Example 3'
  trigger:
    platform: template
    value_template: >
       {{ now().hour == 18 and now().minute == 0 and now().date() + timedelta(days=1) == strptime(states('sensor.collection'), '%Y-%m-%d').date() }}
  action:
    service: notify.whatever
    data:
      title: 'Recycling tomorrow!'
      message: "Put out the recycling this evening for tomorrow's collection."

Personally, I prefer Example 2.

2 Likes

Thank you soo much!

Not only for laying out the complete solution but also because I now have a much better understanding of how a “template” conditions actually works!

I implemented this right away and looking forward to my lights reminding me to put out the trash! :slight_smile:

1 Like

You’re welcome!

Please consider marking my post (above) with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has an accepted solution. This helps users find answers to similar questions.

1 Like