How to have an automation change lighting effects every 2 minutes?

I setup a switch to trigger this automation. I’ve been trying some ai yaml editors and even hired someone on fiver, but can’t seem to get this to work. When the switch is on - it turns on the light and the first effect, but doesn’t change. Maybe random isn’t possible and I should try a list of effects instead?

Yaml code:
alias: Cloud Animation
description: >-
Turns on a light with a random effect every 2 minutes while the switch is on.
Turns off light when switch turns off.
triggers:

  • entity_id: input_boolean.cloud_ceiling
    to: “on”
    trigger: state
    actions:
  • repeat:
    while:
    - condition: state
    entity_id: input_boolean.cloud_ceiling
    state: “on”
    for:
    hours: 0
    minutes: 0
    seconds: 1
    sequence:
    - action: light.turn_on
    metadata: {}
    data:
    effect: >-
    {{ [‘music-separation’, ‘music- piano keys’, ‘music- hopping’,
    ‘music -fountatain’, ‘music - rythem’] | random }}
    target:
    entity_id: light.cloud_ceiling
    enabled: true
    - delay: “00:02:00”
  • target:
    entity_id: light.cloud_ceiling
    action: light.turn_off
    data: {}
    enabled: true
    mode: restart

Please repost your code using the Preformatted Text option in the composer (icon </>). That will make your code readable.

Even with the broken formatting, though, I can see that you’re trying to use a state-based for condition in a repeat loop, which is probably at least one problem with your current approach. That won’t make the automation stop and wait 1 second to see if the input_boolean stays on; instead, the loop won’t ever happen because the loop condition fails.

Thanks, so should I remove this?

          for:
            hours: 0
            minutes: 0
            seconds: 1

Formatted Code:

alias: Cloud Animation
description: >-
  Turns on a light with a random effect every 2 minutes while the switch is on.
  Turns off light when switch turns off.
triggers:
  - entity_id: input_boolean.cloud_ceiling
    to: "on"
    trigger: state
actions:
  - repeat:
      while:
        - condition: state
          entity_id: input_boolean.cloud_ceiling
          state: "on"
          for:
            hours: 0
            minutes: 0
            seconds: 1
      sequence:
        - action: light.turn_on
          metadata: {}
          data:
            effect: >-
              {{ ['music-separation', 'music- piano keys', 'music- hopping',
              'music -fountatain', 'music - rythem'] | random }}
          target:
            entity_id: light.cloud_ceiling
          enabled: true
        - delay: "00:02:00"
  - target:
      entity_id: light.cloud_ceiling
    action: light.turn_off
    data: {}
    enabled: true
mode: restart

It should work if you remove it. Why did you have it there in the first place? If there was something you were trying to achieve, I’m sure there’s a way to do it.

Ok, removing that got the effects to start changing randomly. Except they are changing every 10 seconds instead of 2 minutes. I suppose something isn’t correct in the delay command?

Hmm. I don’t think it’s the delay.

Is something toggling the Boolean? What happens if you change the mode to “single” instead of “restart”?

I changed to single, still changing every 10 seconds. I’m manually triggering the boolean from HA. Will connect it to voice once I get it working.

Ah, the looks like another automation was still enabled causing the frequent changes. So now it works, except there is up to a 2 minutes delay turning off/on. Maybe I need another automation to fix that?

Got that working with 2 other automations to turn on and off

Because your automation uses a fixed delay of 2 minutes. If you turn off the Input Boolean while the automation is busy counting down the delay, it isn’t aware of it.

Instead of a delay, use a wait_template with a 2-minute timeout.

  - wait_template: "{{ is_state('input_boolean.cloud_ceiling', 'off') }}"
    timeout:
      minutes: 2

It waits up to 2 minutes for the Input Boolean to be turned off. If it’s not turned off, the wait_template simply times out at 2 minutes (thereby behaving like a delay). If the Input Boolean is turned within the 2 minutes, the wait_template exits immediately.