How to simplify this addressable light effect?

There must be an easier way to do this…

I have the following effect in ESPhome:
(I used the light.addressable_set to make sure it runs only once)
The led (ring) fills and empties all leds.

Would it be possible to template this or create a ‘for’ loop or something like that?

on_...
  - light.addressable_set:
      id: activity_led
      range_from: 0
      range_to: 0
      red: 100%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 1
      range_to: 1
      red: 100%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 2
      range_to: 2
      red: 100%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 3
      range_to: 3
      red: 100%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 4
      range_to: 4
      red: 100%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 5
      range_to: 5
      red: 100%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 6
      range_to: 6
      red: 100%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 7
      range_to: 7
      red: 100%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 8
      range_to: 8
      red: 100%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 9
      range_to: 9
      red: 100%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 10
      range_to: 10
      red: 100%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 11
      range_to: 11
      red: 100%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 0
      range_to: 0
      red: 0%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 1
      range_to: 1
      red: 0%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 2
      range_to: 2
      red: 0%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 3
      range_to: 3
      red: 0%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 4
      range_to: 4
      red: 0%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 5
      range_to: 5
      red: 0%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 6
      range_to: 6
      red: 0%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 7
      range_to: 7
      red: 0%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 8
      range_to: 8
      red: 0%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 9
      range_to: 9
      red: 0%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 10
      range_to: 10
      red: 0%
      green: 0%
      blue: 0%
  - delay: 50ms
  - light.addressable_set:
      id: activity_led
      range_from: 11
      range_to: 11
      red: 0%
      green: 0%
      blue: 0%

script?

1 Like

I would implement this as an Addressable Lambda.

Here two examples that might go into direction you are looking for:

      - addressable_lambda:
          name: "Wipe In"
          update_interval: 12ms
          lambda: |-
            static int x = 0;
            if (initial_run) {
              x = 0;
              it.all() = ESPColor::BLACK;
            }
            if (x < it.size()) {
              it[x] = current_color;
              x += 1;
            }

      - addressable_lambda:
          name: "Wipe Out"
          update_interval: 12ms
          lambda: |-
            static int x = 0;
            if (initial_run) {
              x = it.size();
            }
            if (x > 0) {
              x -= 1;
              it[x] = ESPColor::BLACK;
            }
1 Like

BTW, I read the following:
“ESPColor has been migrated to Color.”

Still works fine with ESPcolor, but I replaced it with Color just to be sure on the long run…

1 Like