Only start vacuum if last cleaned is X hours ago

hey guys,

so i´m trying to make an automation thats is triggered by time (12 or 3 pm) to start my vacuum. but it should only start at 3 if it did not run at 12.

so i was thinking that using the “last cleaned” entity would be the most easy way, but just saying “if it has not run X hours ago”, however it gives back a date and time.

any ideas how i could do this the best way?

alias: Vacuum
description: ""
trigger:
  - platform: time
    at: "12:00:00"
  - platform: time
    at: "15:00:00"
condition:
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
    enabled: false
  - condition: state
    state: not_home
    entity_id: group.home
  - condition: numeric_state
    entity_id: sensor.roborock_q_revo_letztes_reinigungsende
 # is over X hours ago #
action:
  - service: vacuum.start
    data: {}
    target:
      entity_id: vacuum.roborock_q_revo
  - service: notify.mobile_app_iphone_david
    data:
      title: Automatisierung
      message: Roborock gestartet
    enabled: false
mode: single

You could instead use a template condition and calculate the amount of time since it last ran:

{% set duration = as_timestamp(now()) - as_timestamp(states('sensor.roborock_q_revo_letztes_reinigungsende')) %}
{{ duration | timestamp_custom('%H') | int > 12 }}

This condition would pass if it last vacuumed over 12 hours ago.

2 Likes

will give it a try. thanks :slightly_smiling_face:

so it worked once. somehow it does not work anymore.

state of sensor.roborock_q_revo_letztes_reinigungsende is 2024-02-16T12:05:22+00:00 so over 12 hours ago. however if i test the template in the automation it says the condition is not met.

edit: could it be, that it only checks the timestamp and not consider the date itself?

You will probably need a sensor based on that template. You can do it using template helper or some integration like measureit or just write in your yaml file.
The best option will be using template helper or this integration as you don’t need restart home assistant.

I’m doing the same but I decided to use a numeric helper for it. Then every hour I increase the counter with 1, but only if a different setting (long_away) is not active. That way the counter doesn’t increase when we are away from home for a few days (that is only triggered after x hours away with a certain distance, so the robot would have cleaned atleast once after leaving).

And for the activation it’s in the same automation but different trigger, I don’t care what time it starts, as long as no one is home, so that’s a condition and minimal 16 (hours) on the numeric helper.
And at night only once at 2am if the helper is bigger then 25 it’s forced to start, even if we are home. (That way it cleans normally once a day, but maximum the night after if we didn’t leave the house).

My bad, I thought you were always on the same day. Using timestamp_custom with a time delta is a bit of a hack. Instead you could check duration / 60 / 60 > 12. This converts the time delta in seconds to hours.

changed it to {{ duration // 60 | int > 180 }} yesterday. looks good so far. thanks for your help :slight_smile:

Do you might like to share your complete automation code?

sure:

alias: Vacuum
description: ""
trigger:
  - platform: time
    at: "12:00:00"
  - platform: time
    at: "15:00:00"
condition:
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
  - condition: state
    state: not_home
    entity_id: group.home
  - condition: template
    value_template: >-
      {% set duration =  as_timestamp(now()) -
      as_timestamp(states('sensor.roborock_q_revo_letzte_reinigung_ende')) %}

      {{ duration // 60 | int > 180 }}
    enabled: true
action:
  - service: vacuum.start
    data: {}
    target:
      entity_id: vacuum.roborock_q_revo
  - service: notify.mobile_app_iphone_david
    data:
      title: Home Assistant
      message: 🧹 Roborock gestartet
    enabled: false
mode: single

1 Like

Wow, thank you. That was a swift reply!

1 Like