Vacuum Room by Room, help needed checking last time room was vacuumed

I’ve just written an automation so that when the house is empty, the vacuum with clean a predefined set of rooms. So in the example below it will clean the toilet, porch and kitchen for me. Before cleaning each room it will check it has 20% battery left and after cleaning the room it will update a date time helper for me so that I know what the room was last automatically cleaned

I’m struggling however to add in conditions that it only cleans a specific room if it’s not been cleaned within the last x hours.

There is an automation that will return the vacuum to dock of we come back home. So if it has cleaned the toilet and porch, then we return home. If we leave again for period later on, the automation should check the last time the toilet and porch were cleaned and ignore these, since they are already clean, but it should start from the kitchen and clean this for us.

alias: Empty Home - Activate Vacuum
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.home_status
    from: "on"
    to: "off"
    for: "00:01:00"
action:
  - variables:
      rooms:
        - toilet
        - porch
        - kitchen
  - repeat:
      while:
        - condition: template
          value_template: >-
            {{ repeat.index <= rooms|count and
            state_attr('vacuum.roborock_s5_max','battery_level')|int(0) > 20 }}
      sequence:
        - service: input_datetime.set_datetime
          target:
            entity_id: input_datetime.roborock_last_clean_{{ rooms[repeat.index - 1] }}
          data:
            timestamp: "{{ now().timestamp() }}"
        - service: script.roborock_clean_room
          data:
            room: "{{ rooms[repeat.index - 1] }}"
        - wait_template: "{{ is_state('vacuum.roborock_s5_max', 'returning') }}"
mode: single

Any help much appreciated on this issue
Thank you

After a few more tweaks, I have got my automation just how I want it so im sharing the final code below

I added in some extra bits and bobs - it will now try and trigger hourly, as well as when we go out. However, the condition that we are out is added also so it doesnt bother us if at home. I did this so that if we went out early and it had cleaned witin the last 12 hours, it will not run, but will then recheck on the hour.

I also updated an input_text with the rooms cleaned and then format and send this when we come home so that we know which rooms have been cleaned.

alias: Empty Home - Activate Vacuum
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.home_status
    from: "on"
    to: "off"
    for: "00:01:00"
  - platform: time_pattern
    minutes: "0"
action:
  - repeat:
      for_each:
        - living_room
        - dining_room
        - snug
        - kitchen
        - utility
        - toilet
        - porch
        - hallway
      sequence:
        - condition: state
          entity_id: binary_sensor.home_status
          state: "off"
        - condition: template
          value_template: >-
            {{ state_attr('vacuum.roborock_s5_max','battery_level') | int(0) >
            20 }}
        - condition: template
          value_template: >-
            {{ as_timestamp(now()) - (60*60*12) >
            as_timestamp(states('input_datetime.roborock_last_clean_' ~
            repeat.item))  }}
        - service: notify.persistent_notification
          data:
            message: Vacuum Cleaning Automation
            title: Cleaning {{ repeat.item }}
          enabled: false
        - service: script.roborock_clean_room
          data:
            room: "{{ repeat.item }}"
        - wait_template: "{{ is_state('vacuum.roborock_s5_max', 'returning') }}"
        - service: input_datetime.set_datetime
          target:
            entity_id: input_datetime.roborock_last_clean_{{ repeat.item }}
          data:
            timestamp: "{{ now().timestamp() }}"
        - service: input_text.set_value
          data:
            value: >-
              {{ states('input_text.roborock_current_clean') }}, {{ repeat.item
              }}
          target:
            entity_id: input_text.roborock_current_clean
mode: single

2 Likes