LED Random Effect - not working?

Some time back I mounted some addressable LED’s under my daughters bed, connected via a PIR so when our daughter got out of bed at night the lights would act as a night light. The code I created is average but it works.

Recently I’ve tried to create some new code, with the idea that when the PIR activates the LED’s they use a random lighting pattern. Despite some weeks of trying… I cannot get the code to work.

Here’s the code to activate the LED’s below…

light:
  - platform: neopixelbus
    variant: WS2812
    pin: D1
    num_leds: 54
    type: GRB
    name: "FastLED WS2812B Light"
    id: bdrm_led_lights
    default_transition_length: 7.5s

binary_sensor:
  - platform: gpio
    pin: D5
    id: bdrm_pir
    name: "Bdrm PIR Sensor"
    device_class: motion
    on_press:
      then:
      - if:
          condition:
            and:
              # If it's night time
              - sun.is_below_horizon:
              - lambda: |-
                  auto hour = id(my_time).now().hour;
                  return hour > 12 || hour < ${off_hour};
          then:
              #Turn on the LED lights
              - light.turn_on:
                  id: bdrm_led_lights
                  brightness: 40%
                  effects:
                    - random:
                    - random:
                        name: "Slow Random Effect"
                        transition_length: 30s
                        update_interval: 20s
                    - random:
                        name: "Fast Random Effect"
                        transition_length: 4s
                        update_interval: 5s
                    - pulse:
                    - pulse:
                        name: "Fast Pulse"
                        transition_length: 0.5s
                        update_interval: 0.5s
                    - pulse:
                        name: "Slow Pulse"
                        # transition_length: 1s      # defaults to 1s
                        update_interval: 2s
                    - addressable_rainbow:
                    - addressable_rainbow:
                        name: "Rainbow Effect"
                        speed: 10
                        width: 50
                    - addressable_color_wipe:
                    - addressable_color_wipe:
                        name: "Color Wipe Effect"
                        colors:
                          - red: 10%
                            green: 90%
                            blue: 25%
                            num_leds: 5
                          - red: 35%
                            green: 15%
                            blue: 60%
                            num_leds: 1
                        add_led_interval: 100ms
                        reverse: false
                    - addressable_fireworks:
                    - addressable_fireworks:
                        name: "Fireworks Effect"
                        update_interval: 32ms
                        spark_probability: 10%
                        use_random_color: true
                        fade_out_rate: 120
                    - addressable_scan:
                    - addressable_scan:
                        name: "Scan Effect"
                        move_interval: 100ms
                        scan_width: 5
                    - addressable_random_twinkle:
                    - addressable_random_twinkle:
                        name: Random "Twinkle Effect"
                        twinkle_probability: 15%
                        progress_interval: 32ms
              - logger.log: "LED lights are ON"
    on_release:
      - script.execute: turn_off

script:
  - id: turn_off
    mode: restart
    then:
      - delay: 90s
      - light.turn_off:
          id: bdrm_led_lights
          transition_length: 7.5s
      - logger.log: "LED lights are OFF"

ESPHome wont let me save the code because I receive error: [effects] is an invalid option for [light.turn_on]. Did you mean [effect]?

Perhaps there’s something I am missing, or I have completely gone down the wrong road?

Thanks, Mark

Hi.

Well you are messing it a litle:

You should first put the effects you want in the light component.

Exemple from the docs

And then you can call one of those effects on the automation :

on_press:
      - light_turn_on: 
          id: led_strip
          effect: "My Slow Random Effect"

Thanks for the feedback.

So what if I want a random light effect each time the on_press occurs?

If I understand the on_press function, you must call a specific light effect. Which I dont believe has an option for a random effect being chosen each time… unless I am missing something?

And when I say random… I mean a random choice of one of the light effects… such as one of the choices below.

                 effects:
                    - random:
                    - random:
                        name: "Slow Random Effect"
                        transition_length: 30s
                        update_interval: 20s
                    - random:
                        name: "Fast Random Effect"
                        transition_length: 4s
                        update_interval: 5s
                    - pulse:
                    - pulse:
                        name: "Fast Pulse"
                        transition_length: 0.5s
                        update_interval: 0.5s
                    - pulse:
                        name: "Slow Pulse"
                        # transition_length: 1s      # defaults to 1s
                        update_interval: 2s
                    - addressable_rainbow:
                    - addressable_rainbow:
                        name: "Rainbow Effect"
                        speed: 10
                        width: 50
                    - addressable_color_wipe:
                    - addressable_color_wipe:
                        name: "Color Wipe Effect"
                        colors:
                          - red: 10%
                            green: 90%
                            blue: 25%
                            num_leds: 5
                          - red: 35%
                            green: 15%
                            blue: 60%
                            num_leds: 1
                        add_led_interval: 100ms
                        reverse: false
                    - addressable_fireworks:
                    - addressable_fireworks:
                        name: "Fireworks Effect"
                        update_interval: 32ms
                        spark_probability: 10%
                        use_random_color: true
                        fade_out_rate: 120
                    - addressable_scan:
                    - addressable_scan:
                        name: "Scan Effect"
                        move_interval: 100ms
                        scan_width: 5
                    - addressable_random_twinkle:
                    - addressable_random_twinkle:
                        name: Random "Twinkle Effect"
                        twinkle_probability: 15%
                        progress_interval: 32ms

One approach might be:
Add the effects list under the light component, and give each effect an ordered number as name:

light:
  - platform: neopixelbus
    variant: WS2812
    pin: D1
    num_leds: 54
    type: GRB
    name: "FastLED WS2812B Light"
    id: bdrm_led_lights
    default_transition_length: 7.5s
    effects:
      - random:
          name: "1"
      - random:
          name: "2"
          transition_length: 30s
          update_interval: 20s
      - random:
          name: "3"
          transition_length: 4s
          update_interval: 5s
      - pulse:
          name: "4"
      - pulse:
          name: "5"
          transition_length: 0.5s
          update_interval: 0.5s
      - pulse:
          name: "6"
          # transition_length: 1s      # defaults to 1s
          update_interval: 2s
      - addressable_rainbow:
          name: "7"
      - addressable_rainbow:
          name: "8"
          speed: 10
          width: 50

The effect configuration variable under the light.turn_on Action is templatable, so create a template that generates a random number between 1 and the highest number applicable (assumed to be 8 for now):

on_...:
  then:
    - light.turn_on:
        id: bdrm_led_lights
        effect: !lambda |-
          return random number between 1 and 8

Note that this is all just a conceptual idea of how it might work.
The random number template still has to be figured out.