Using a time difference between now and a state/sensor value for condition (start Roborock if last cleaning is 3 hours ago)


I integrated my Roborock into Home Assistant. My automation should start when I leave home and last cleaning was ended at least 3 hours ago.

I have a sensor called “sensor.roborock_last_clean_end” which returns a datetime(?). How can I use this to check if its value is older than 3 hours? I want to use this as a condition to not clean my home every hour (if I leave my home every hour :smiley: )


1 Like

Copy-paste the following template into the Template Editor and confirm it reports the correct result. It can be used in a Template Condition.

{{ now() - states('sensor.roborock_last_clean_end') | as_datetime | as_local > timedelta(hours=3) }}

It’s working in the template editor. It returns true or false. This is only for testing purposes I think? But which type of condition do I add now to my automation and how to call this snippet?

As mentioned in my first post, you can use it in a Template Condition.

  - condition: template
    value_template: "{{ now() - states('sensor.roborock_last_clean_end') | as_datetime | as_local > timedelta(hours=3) }}"

If you use the Visual Automation Editor to compose your automations, just select “Template” in the Conditions section and paste the suggested template.


Thank you. So I was in the correct section first. But I got an error after clicking the test button:

But I could save this automation.

The same snippet returns true or false in Developer Tools > Template.

That’s because there’s a known problem with the ‘Test’ button; it doesn’t evaluate templates correctly. Until it’s fixed, avoid using it and rely on the Template Editor to validate a template.

1 Like

great, thanks @123 that worked perfectly for me

What should the code look like if I want to know how many hours have passed? The above code works very well for true false. I can’t calculate the number of hours since the last cleaning.