Trigger automation one week after a date

I’d like to trigger an automation one week after my vacuum last cleaned.

I already have a timestamp of the last time the vacuum ran with:

state_attr('vacuum.neato', 'clean_start')

I’ve tried using for to wait for a week after the vacuum docks, but this seems to get reset each time HA restarts:

- id: vacuum_reminder
  alias: "Notify if no vacuum for a week"
  trigger:
    platform: state
    entity_id: vacuum.neato
    from: "cleaning"
    to: "docked"
    for:
      days: 7
  action:
    service: notify.mobile_app_iphone
    data:
      title: "Time for a vacuum?"
      message: "It's been over a week since the vacuum last cleaned."

Instead, I assume I should compare the last clean date with sensor.time_date and if it’s been more than a week trigger the automation? Can anybody help me with that?

It seems a binary sensor will do the job here though I’d be interested in any other approaches.

I’ve created a binary sensor that compares the two time periods in seconds, and switches on if the time period is over 604800 (one week):

- platform: template
  sensors:
    week_since_vacuum:
      friendly_name: "Over a week since vacuum"
      value_template: "{{ as_timestamp(now()) - as_timestamp(state_attr('vacuum.neato', 'clean_start')) > 604800 }}"

I’m then using this binary sensor in the automation as the trigger:

- id: vacuum_reminder
  alias: "Notify if no vacuum for a week"
  trigger:
    platform: state
    entity_id: binary_sensor.week_since_vacuum
    from: "off"
    to: "on"
  action:
    service: notify.mobile_app_iphone
    data:
      title: "Time for a vacuum?"
      message: "It's been over a week since the vacuum last cleaned."

I assume this will work!

No, it won’t, because the template binary sensor will not update after vacuum.neato changes. I.e., it will not change just because now() changes. now() is not an entity so it does not cause state changed events.

You can use a template trigger with sensor.date_time.

trigger:
  platform: template
  value_template: >
    {{ as_timestamp(states('sensor.date_time').replace(',', '') ~ ':00')  -
       as_timestamp(state_attr('vacuum.neato', 'clean_start')) > 604800 }}

This assumes, of course, that state_attr('vacuum.neato', 'clean_start') can be properly interpreted by as_timestamp().

1 Like

Much appreciated - updated!