Automation repeat every x seconds for x minutes

Hi,

I want to trigger an action in an automation every 5 seconds for 2 minutes long. Is this possible?
I am using DOODS (Dedicated Open Object Detection Service) and want it to scan every 5 seconds for objects when motion is detected on my camera. I already have the automation that triggers the action once:

- id: '1636979170910'
  alias: DOODS update image voortuin
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.ip_camera_motion
    to: 'on'
    from: 'off'
    for:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  condition: []
  action:
  - service: image_processing.scan
    target:
      entity_id: image_processing.doods_voortuin
  mode: single

But is it possible to scan every 5 seconds? Because sometimes the camera detects motion before an object is on my property, so it does not detect it…

Check out repeat, the docs can be found here. For a simple solution, you can add a five second delay as an action in the sequence and then just have it repeat 60 times.

Ah, so repeat count with delay should work? Can not find examples in automations. But if it should work i will keep trying haha

Here is an example of repeat, this turns up the volume on my Google Home to the target level

sequence:
  - repeat:
      sequence:
        - delay:
            milliseconds: 650
        - service: media_player.volume_up
          data:
            entity_id: '{{ media_target }}'
      until:
        - condition: template
          value_template: '{{ state_attr(media_target,''volume_level'') >= volume_target }}'
  - delay:
      seconds: 1
  - service: media_player.volume_set
    data:
      entity_id: '{{ media_target }}'
      volume_level: '{{ volume_target }}'

This one - changes the colour of my RGB strip:

sequence:
  - repeat:
      count: '060'
      sequence:
        - service: light.turn_on
          data_template:
            entity_id: '{{ light_target }}'
            color_name: >
              {% set colors =
              ['blue','green','orange','yellow','red','purple','white'] %} {%
              set c = colors|random %} {{ c }}
            brightness_pct: '{{ brightness_target|int }}'
            transition: 20
        - delay:
            hours: 0
            minutes: 0
            seconds: 30
            milliseconds: 0

Okay, so i want it to repeat it every 2 seconds, for 20 times, or until a state changes. I created this:

- id: '1636979170910'
  alias: DOODS update image voortuin
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.ip_camera_motion
    to: 'on'
    from: 'off'
  condition: []
  action:
	sequence:
	count: 20
      - repeat:
                sequence:
                  - service: image_processing.scan
                    data:
                      entity_id: image_processing.doods_voortuin
                   - delay: '00:00:02'  
                until:
                  - condition: state
                    entity_id: image_processing.doods_voortuin
                    state:  'off'

But is does not work, anyone an idea?

Your formatting is not correct. If you want to use both an state condition and a count, you need to use either the “until” or “while” form… use a repeat.index template instead of using “count”.

Try the following:

- id: '1636979170910'
  alias: DOODS update image voortuin
  description: ''
  trigger:
    - platform: state
      entity_id: binary_sensor.ip_camera_motion
      to: 'on'
      from: 'off'
  condition: []
  action:
    - repeat:
        sequence:
          - service: image_processing.scan
            data:
              entity_id: image_processing.doods_voortuin
          - delay: '00:00:02'  
        until: '{{ repeat.index >= 20 or is_state('image_processing.doods_voortuin', 'off') }}'

EDIT Corrected shorthand notation format for “until”

Great, i will try. is there a way to see if an automation is stopped or still running?

EDIT: I am getting this error in the logs:
Template variable error: ‘repeat’ is undefined when rendering ‘’{{ repeat.index >= 20 or is_state(‘image_processing.doods_voortuin’, ‘off’) }}’

EDIT: looks like the repeat is working, it stops after 30 times. I can test tonight is it also stops when the state changes.

When an automation’s action is in progress, the value of the automation’s current attribute is 1 (I believe that means one instance of the automation is in progress). When it’s idle, the value is 0. Therefore you can use that (check if the value is greater than zero) to determine if the automation is busy or not.

Just tested it, looks like it stops after x times. But it does not stop when the state changes…

EDIT: i think i found the issue, looks like it stops the automation. But triggers it directly when the trigger goes again. Need to create an condition so it does not keep running again.

Got it working now:

- id: '1636979321456'
  alias: DOODS update image voortuin v2
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.ip_camera_motion
    to: 'on'
    from: 'off'
  condition:
  - condition: template
    value_template: '{{ ( as_timestamp(now()) - as_timestamp(state_attr(''automation.doods_update_image_voortuin_v2'',
      ''last_triggered'')) |int(0) ) > 30 }}'
  action:
  - repeat:
      sequence:
      - service: image_processing.scan
        data:
          entity_id: image_processing.doods_voortuin
      - delay:
          hours: 0
          minutes: 0
          seconds: 0
          milliseconds: 500
      until:
      - condition: template
        value_template: '{{ repeat.index >= 40 or not is_state(''image_processing.doods_voortuin'',
          ''0'') }}'