Automation Repat until doesn't stop

Hi,
I created an automation for my roomba. It sends a notification with notify if the dust bin is not cleared until one day prior to the scheduled cleaning day defined in the roomba app.

For example, if cleaning days are set to Monday and Friday, the automation should check the dustbin status on Sunday and Thursday.
Even though, the stop conditions are defined, it spams notifications and ignored stop conditions. I could not figure out where it is wrong.
According to code, it should not send any notification if the time is past 02:00 AM on those days.
Another problem that I am not sure is the trigger. Until I restarted Home Assistant, this automation did not trigger at all.

alias: Roomba Reminder
description: ''
trigger:
  - platform: template
    value_template: >-
      {{ is_state('binary_sensor.roomba_binfull', 'on') and (
      ((as_timestamp(now()) -
      as_timestamp(states.binary_sensor.roomba_binfull.last_changed))/60) | int
      > 1) }}
condition:
  - condition: time
    after: '20:00:00'
    before: '23:59:59'
    weekday:
      - thu
      - sun
action:
  - service: notify.notify
    data:
      message: Clean the dust bin.
      title: Robot Roomba Status
  - repeat:
      until:
        - condition: or
          conditions:
            - condition: state
              entity_id: binary_sensor.roomba_binfull
              state: 'off'
              for: '00:01:00'
            - condition: time
              weekday:
                - thu
                - sun
              after: '20:00:00'
              before: '01:00:00'
      sequence:
        - delay:
            hours: 1
            minutes: 0
            seconds: 0
            milliseconds: 0
        - service: notify.notify
          data:
            message: Still not clean.
            title: Robot Roomba Status
mode: single

I believe your time condition is incorrect.

time is relative to midnight and those two times are and ‘and’.

So there is never any time that it will be after 2200 but before midnight AND after midnight but before 0100 at the same time. so the condition will always be false.

I think the trigger is OK. I think.

What makes you think it isn’t OK?

Well, the documentation says that

Time condition windows can span across the midnight threshold if both after and before keys are used.

So, I figured it should work.

Hmm…not sure when that changed but it looks like you are right.

but you might try it anyway just to be sure? :man_shrugging:

I’m not exactly sure what you’re trying to achieve with your trigger template. I think it will fire two minutes after binary_sensor.roomba_binfull changes to on. If that’s what you want, why not use a more traditional trigger like this:

trigger:
  - platform: state
    entity_id: binary_sensor.roomba_binfull
    to: 'on'
    for: '00:02:00'

Next, the actions will only run if two minutes after the sensor goes on it just happens to be between 8:00 PM and midnight on Thursday or Sunday.

I don’t think that’s what you want. In fact, I think you have the trigger and condition reversed.

Using this will work if I don’t want to add time condition. But adding specific dates on top of this notation will not work.
For example, Roomba finishes cleaning on Monday and its dustbin is full but it needs to remind me to clean it on Thursday night because Friday is the cleaning day. Since time condition is not valid, home assistant will not trigger this automation but if you use a template calculating the difference with last changed, it will trigger. If there is a smarter way to do it, I would prefer it but I can’t think of any.

You might be right here. Let me try switching the trigger with the condition.

You need to think of when you want the automation to run. And, at that time, or those times, if it should run based on other conditions. Remember, the trigger determines when the automation should run, and the conditions determine if the automation should run (when a trigger fires.)

So, again, first think of when you want the automation to run. You said, “For example, if cleaning days are set to Monday and Friday, the automation should check the dustbin status on Sunday and Thursday.” That sounds like the when/trigger; i.e., some time on Sunday and Thursday.

Then think (at those time) what should determine if it should run. You said, “if the dust bin is not cleared.” That sounds like the if/condition.

Putting those together, use a trigger that fires at a given time on Sunday and Thursday. Then use a condition that the bin is not cleared.

Unfortunately, the time trigger does not directly support day of week. So you need to use a time trigger with a time condition to achieve it:

trigger:
  - platform: time
    at: '20:00:00'
condition:
  - condition: time
    weekday:
      - sun
      - thu

Now you can add the “dust bin is not cleared” condition:

trigger:
  - platform: time
    at: '20:00:00'
condition:
  - condition: time
    weekday:
      - sun
      - thu
  - condition: state
    entity_id: binary_sensor.roomba_binfull
    state: 'on'

With this, the automation will run at 8 p.m. on Sunday and Thursday, if, at that time, the dust bin is not cleared.

For the action part, sounds like you want it to send a notification every hour until either the dust bin is cleared, or it’s after 1:00 a.m. (Or to put that another way, send the notification while the dust bin is not cleared and it’s between 8:00 p.m. and 1:00 a.m.) So:

action:
  - repeat:
      while:
        - condition: state
          entity_id: binary_sensor.roomba_binfull
          state: 'on'
        - condition: time
          after: '20:00:00'
          before: '01:00:00'
      sequence:
        - service: notify.notify
          data:
            message: Still not clean.
            title: Robot Roomba Status
        - delay:
            hours: 1

That absolutely makes sense. I guess I did not completely understand the Home Assistant automation logic. So, I have to reverse the logic like you suggested. Thanks for the great explanation. I will try the new method.
I’ve changed the delay with “wait_template” though. It appeared more fitting and provided early exit from the loop, if the conditions are satisfied.
I’ve not tested this yet but I will let you know after testing.
Full automation below:

alias: Roomba Reminder (Duplicate)
description: ''
trigger:
  - platform: time
    at: '20:00:00'
condition:
  - condition: time
    weekday:
      - thu
      - sun
  - condition: state
    entity_id: binary_sensor.roomba_binfull
    state: 'on'
    for: '00:01:00'
action:
  - repeat:
      while:
        - condition: state
          entity_id: binary_sensor.roomba_binfull
          state: 'on'
          for: '00:01:00'
        - condition: time
          after: '20:00:00'
          before: '01:00:00'
          weekday:
            - thu
            - sun
      sequence:
        - service: notify.notify
          data:
            title: Robot Roomba Status
            message: Tomorrow is robot cleaning day. Roomba dust bin needs to be cleaned.
        - wait_template: '{{ is_state(''binary_sensor.roomba_binfull'', ''off'') }}'
          timeout: '01:00:00'
          continue_on_timeout: true
mode: single

Yes, that will work, but it’s not really necessary in this case. The automation won’t run again for almost three days (at least), so does it really matter if it exits when the bin is cleaned as opposed to maybe up to an hour later? In this particular case, I’d say simplicity is better. Now, if the automation was going to do something when the bin was cleaned, that would be another story and the wait_template would be important. Either way, though, it’s fine.