Time related automation problem

I’m new to HA and a little puzzled what can be done and how. I’d like to automate my Roborock robot vacuum to clean a room if it has not been cleaned for three days. I started developing bottom up and configured template sensors for rooms. I had in mind that I could store timestamp of the last time of the room has been cleaned in the template sensor attributes and change the timestamp next time when the robot cleans the room. The problem is that it seems impossible to change the template sensor attribute via automation.

I would like also to automation to be just date dependent instead of hourly comparison. I mean the automation would just check if the room has been cleaned during the last three days and if not, then executing the script in certain time. Also I would like it to collect all the rooms to a list and clean them on the same action during the day so the robot would not need to run back to dock and start doing vacuuming all over again.

Now I’m a little bit lost how I could achieve this kind of automation when my initial plan has failed. Any tips how to pick another route on my plan?

Here’s an example what I’ve created as my templates: # KITCHEN - binary_sensor: - name: "Kitchen Cleaning" state: "{ - Pastebin.com
And here’s part of the automation: - id: '1677329873565' alias: Vacuum description: '' trigger: - platf - Pastebin.com

The state loads before the attributes, so you need to either do your calculations in both or supply a default so that the state doesn’t error out. Also your sensor will be static because days_since is static and everything that follows is built on that.

What data does your vacuum supply that could be used to define the last_cleaned variable? For example, does it have a room id that changes when it completes a room which you could use to set the value of a helper of template sensor?

  - binary_sensor:
    - name: "Kitchen Cleaning"
      state: "{{ this.attributes.cleaning_need | default(true) }}"
      attributes: 
        room_id: "14"
        name: "keittiö"
        days_since: >
          {% set last_cleaned = ???? %}
          {% set days_since = (today_at() - last_cleaned | as_datetime ).days %}
        cleaning_need: >
          {{this.attributes.days_since >= 3}}

In your automation, you are performing a lot of pointless calculations. Once you have selected for the state being ‘on’, there is no reason to run multiple expand() and map() functions. If you meant to use that data later, you should use trigger variables to store it so it is available throughout the whole automation and doesn’t need to be recalculated.

- id: '1677329873565'
  alias: Vacuum
  description: ''
  trigger:
  - platform: time
    at: '20:37:00'
    variables:
      on_rooms: >
        {{ ["binary_sensor.kitchen_cleaning", "binary_sensor.living_room_cleaning",
        "binary_sensor.bedroom_cleaning", "binary_sensor.playroom_cleaning", 
        "binary_sensor.work_room_cleaning", "binary_sensor.corridor_cleaning"] 
        | select("is_state", "on") | list }}
      count: "{{ on_rooms | count }}"
  condition:
  - condition: template
    value_template: "{{ count >= 1 }}"
  action:

Thanks for the reply! Yes, the sensor timestamp is currently static for only development purposes. Would like it to be dynamic but currently I have no idea how I could achieve it. The vacuum cleaner only provides timestamp of last cleaning end but does not provide room id’s or anything else usable. But the cleaner could be started with the following automation configuration example:

automation:
  - alias: "Vacuum kitchen and living room"
    trigger:
    - event: start
      platform: homeassistant
    condition: []
    action:
    - service: xiaomi_miio.vacuum_clean_segment
      target:
        entity_id: vacuum.xiaomi_vacuum
      data:
        segments: [14,15]

I had in mind that the those room id’s could be used again later in automation to trigger new timestamp for the room template sensors. Just don’t know how to implement it.

The variables suggestion looks nice. Have to dig in little further as this is the first time I see use of them.