How to get random values within a for_each loop?

For context, I have six small RGB light bulbs on the front of my house, I thought it would be fun to have each individual bulb randomly alternate between red and green over the holidays.

(I should probably admit that I stole this idea from a friend’s neighbor this weekend.)

I used the random helper to create binary “light sensor” and a wrote a YAML script that does basically this:

trigger: every second of the day
action: for_each (RGB light bulb in the group of outside lights)
  if the sensor detects light,
    turn the light red
  else
    turn the light green

Note that the “if” is nested within the “for_each”. My hope was that every second, each individual light would have a 50/50 chance of toggling between red and green, uncorrelated with the other lights.

What really happens: every second, all six lights will turn red, or all six lights will turn green.

It appears that the random sensor is queried once during the script and that value is cached for the duration of the script.

Is there a way to get the random sensor’s value to be queried every time the “if” condition is executed?

Thanks in advance!

The actual YAML:

alias: Christmas Lighting
description: ""
triggers:
  - trigger: time_pattern
    seconds: /2
conditions: []
actions:
  - sequence:
      - repeat:
          for_each: >-
            {{ expand('light.outside_garage_lights') |
            map(attribute='entity_id') | list }}
          sequence:
            - if:
                - condition: state
                  entity_id: binary_sensor.random_binary
                  state: "on"
              then:
                - action: light.turn_on
                  metadata: {}
                  data:
                    rgb_color:
                      - 255
                      - 0
                      - 0
                  target:
                    entity_id: "{{ repeat.item }}"
              else:
                - action: light.turn_on
                  metadata: {}
                  data:
                    rgb_color:
                      - 0
                      - 255
                      - 0
                  target:
                    entity_id: "{{ repeat.item }}"
mode: parallel
max: 5

My guess would be no. The sensor itself is probably rate limited, and its state only updates something like once per second. If you open the Developer Tools panel and States tab, you should be able to see the random sensor updates in real time.

I would instead use the random filter in Jinja, something like this:

alias: Christmas Lighting
description: ""
triggers:
  - trigger: time_pattern
    seconds: /2
conditions: []
actions:
  - sequence:
      - repeat:
          for_each: >-
            {{ expand('light.outside_garage_lights') |
            map(attribute='entity_id') | list }}
          sequence:
            - action: light.turn_on
              metadata: {}
              data:
                rgb_color: >-
                  {{ [[255, 0, 0], [0, 255, 0]] | random }}
              target:
                entity_id: "{{ repeat.item }}"
mode: parallel
max: 5

Thanks! That gives the results that I was hoping for.