Combine automation repeat until and count

It is possible to have an automation repeat based on a condition but only once? I can use repeat until to repeat based on the condition. Or I can use repeat count (1) but I’m not sure how to combine the two.

The reason for doing this is sometimes my IKEA lights don’t transition to the correct brightness/colour - what this should do it is check the light attributes against an input number and run the automation again if the light does not have the right attributes. However I don’t want it running forever if the light just won’t update for whatever reason, so I want to put a hard stop on how many times it can repeat.

Below is my automation with only the repeat until element, how do I add a limit to how times it can repeat?

alias: Colour Brightness - State On - Lamps
description: ''
trigger:
  - platform: state
    entity_id: nook_lamp
    to: 'on'
action:
  - repeat:
      until:
        - condition: template
          value_template: >-
            '{{ (state_attr('light.nook_lamp','color_temp')|int ==
            states('input_number.lamps_colour')|int) and
            (state_attr('light.nook_lamp','brightness')|int ==
            (states('input_number.lamps_brightness')|int*2.55)|int) }}'
      sequence:
        - service: light.turn_on
          target:
            entity_id: '{{ trigger.entity_id }}'
          data:
            transition: 1
            color_temp: '{{ states(''input_number.lamps_colour'') | int }}'
        - delay:
            hours: 0
            minutes: 0
            seconds: 1
            milliseconds: 0
        - service: light.turn_on
          target:
            entity_id: '{{ trigger.entity_id }}'
          data:
            transition: 1
            brightness_pct: '{{ states(''input_number.lamps_brightness'') | int }}'
mode: parallel
max: 10
2 Likes

This limits the number of repetitions to no more than 5.

action:
  - repeat:
      until:
        - condition: template
          value_template: >-
            {{ (state_attr('light.nook_lamp','color_temp')|int == states('input_number.lamps_colour')|int and
                state_attr('light.nook_lamp','brightness')|int == (states('input_number.lamps_brightness')|int*2.55)|int) or
                repeat.index == 5 }}
4 Likes

Perfect thank you

Hi, where can I find more details on how to convert basic (created by the UI) until action to a templated one with repeat.index cap? In my case I’m running the loop until 3 binary devices have a certain state. It looks like this:

    until:
        - type: is_not_open
          condition: device
          device_id: 28af7c2cf53d7552d80550f1e672048c
          entity_id: 5887007fe9926204e40f543998583061
          domain: binary_sensor
        - type: is_not_open
          condition: device
          device_id: d4ea118cdb4018f3d7876ca1d0489b9b
          entity_id: fe59051dd0b1ea508616f8698c97d659
          domain: binary_sensor
        - type: is_not_open
          condition: device
          device_id: 1fc55a0f8d36bcfbabda48a382c1c08c
          entity_id: 0ddbc37fab126d922c42ee799a047eac
          domain: binary_sensor
          enabled: false

You’re using Device Conditions and they don’t support templates.

If you need to use templates in a condition, consider using a Template Condition.