Vacuum cleaner - ignore automation if it has been a while since a completed clean

I’m continuing to develop an automation (with some help from @pnbruckner - thanks) for my Xiaomi S50 vacuum cleaner.
So far I have it set up so that

  • It detects when both occupants have left the house (using life360) and if it hasn’t yet run that day it kicks off a clean and sends a notification to my phone (using pushover)
  • If it detects that either of us get home while it is cleaning it returns to base and sends a notification.

The idea is to keep the house clean but for us to never see it in action. To take this to the next level I would like HA to determine that if it has been for example 3 days since a full, noninterrupted clean it will ignore the instruction to return to base when someone is home and continue cleaning anyway. I’m thinking there would be two different approaches for getting the information on when the last full clean was:

  • When the “return to base - someone has returned home” automation is triggered, write a timestamp to a variable if the variable is null. If it is not null, check if it is more than 3 days ago and if so null it out and abort the automation.
  • I could also define a full clean as a clean of more than 70 square meters. The vacuum cleaner has a property “cleaned_area” so I could use some sort of technique to determine the last time this value was more than 70 square meters.

I don’t have the syntax or insight to figure out what to do next.

My current automation is below:

- alias: 'Start cleaning when everyone is away'
  trigger:
   platform: state
   entity_id: group.people
   to: 'not_home'
  condition:  
    condition: and
    conditions:
    - condition: template
      value_template: >
        {% set last = state_attr('automation.start_cleaning_when_everyone_is_away',
                                 'last_triggered') %}
        {{ last is none or
           as_timestamp(last)|timestamp_custom('%j')|int !=
           now().strftime('%j')|int }}
  action: 
    - service: vacuum.set_fan_speed
      data:
       entity_id: vacuum.xiaomi_vacuum_cleaner
       fan_speed: 100  
    - service: vacuum.start
      entity_id: vacuum.xiaomi_vacuum_cleaner
    - service: notify.pushovernotify
      data: 
       message: "Vacuum started cleaning - everyone away and hasn't run today"
       title: "Vacuum started cleaning - everyone away and hasn't run today"

- alias: 'Return to base - someone has returned home'
  trigger: 
   platform: state
   entity_id: device_tracker.life360_jane, device_tracker.life360_luke
   to: 'home'
  condition:
    - condition: state
      entity_id: "vacuum.xiaomi_vacuum_cleaner"
      state: "cleaning"
  action:
   
    - service: vacuum.return_to_base
      entity_id: vacuum.xiaomi_vacuum_cleaner
    - service: notify.pushovernotify
      data: 
       message: "Vacuum returning to base - someone returned home"
       title: "Vacuum returning to base - someone returned home"

I think it’s quite straightforward to set this up with inputs and a sensor (I just ordered my vacuum and have the same plan as you):

  • input_boolean ‘fullly_cleaned’. This will store the clean status
  • input_datetime ‘last_full_clean’. This will store the last date of a full clean.
  • input_number ‘max_days’. This will store how many days allowed to don’t fully clean. This can be added to a card as well so you can adjust it on the fly with a slider if needed.
  • a sensor based on a value template. This value template will return on or off (true or false) if a full clean needed using the first two input values. It should calculate the day difference between the dates and compare with the third inputs value.

Having these, you only need to set the values in your automation of the first two the inputs when a full clean is done and read the sensor value before you normally send the vacuum back to its base (condition) .

And that’s it. Your dynamic sensor does the calculation for you.

Sorry for not providing actual code now (I will after I’ve set up mine).

Just in curious, have you finished with that?