Only run automation every X hours

Working on an automation to vacuum the house only when I’m gone and it hasn’t run in X hours. Everything works fine other then the timeframe.

My condition is validating “condition pass” regardless of the time being within 6 hours or not. Is there something I’m missing?

alias: "House Empty Vacuum "
description: "Vacuum when the house is empty, only every few hours"
trigger:
  - platform: state
    entity_id:
      - input_boolean.home_occupied
    to: "off"
    from: "on"
    alias: House Empty
condition:
  - condition: time
    after: input_datetime.vacuum_time_helper
action:
  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.vacuum_time_helper
    data:
      datetime: "{{ now() + timedelta(hours = 6) }} "
  - alias: Clean Kitchen, Dining, and Entry
    service: vacuum.send_command
    data:
      command: spot_area
      params:
        rooms: 6,5,4
        cleanings: 1
    target:
      entity_id: vacuum.rozzum
mode: single

As the docs state:
“Please note that the time condition only takes the time into account. If a referenced sensor or helper entity contains a timestamp with a date, the date part is fully ignored.”

I don’t know at which times your automation is triggered, but as this method doesn’t take the date into account, the values will roll over in some scenarios.

Example:
Last time the automation fired: 10 PM
Next time you want it to fire: 5 AM
The automation didn’t run for: 7 hours, so over 6 hours delta

It will now do this: IF 5 AM > 10 PM. This will not work as 5 AM obviously comes before 10 PM

You have two options for template conditions instead that check for time delta:

First option (the last time the automation was triggered):

{{now() - state_attr('automation.automation_name', 'last_triggered') > timedelta(hours=6)}}

Second option (template to check the time and date difference (like you intended))

{{now() >= states('input_datetime.vacuum_time_helper') | as_datetime | as_local}}

I’d prefer the first because that doesn’t need the input_datetime helper and is less clutter in the automation (don’t need to adjust the date time helper). Note, this only works if this automation is only used for this purpose and doesn’t have multiple triggers.