Better Selection For Automation of Light Preset (WLED)

I’ve got a string of LEDs run by an ESP8266 with WLED 0.14.0. In HA I have an automation to turn them on and off every day at sunset(ish) and 11pm respectively. I also have a good chunk of preset patterns/effects that I’d like to select from when turning them on each day. However, I want to change which preset is selected based on the (roughly) month for the given holiday.

For example, in December, I want to have one of several Christmas presets randomly selected. If I was to illustrate this with a JSON map, it would look something like this:

{
   1: ['rainbow'],
   2: ['valentines1', 'valentines2'],
   3: ['rainbow'],
   4: ['rainbow'],
   5: ['rainbow'],
   6: ['rainbow'],
   7: ['fourth1', 'fourth2'],
   8: ['rainbow'],
   9: ['rainbow'],
  10: ['spooky1', 'spooky2'],
  11: ['rainbow'],
  12: ['xmas1', 'xmas2', 'xmas3'],
} 

Where the object has entries for each of the 12 months and for each of those entries, a set of presets to randomly choose from.

Currently I have this automation which, while it works, seems like there is likely a much better way to write this to be a bit more concise and easier to update/work with.

What tools should I be looking for to template out this decision better?

Thanks!

alias: WLED Porch onoff
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: "-00:15:00"
    id: ontime
  - platform: time
    at: "23:00:00"
    id: offtime
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: ontime
        sequence:
          - type: turn_on
            device_id: 5909944fa51dc9d1e443e63aa05789d1
            entity_id: light.wledporch_master
            domain: light
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ now().month in [12] }}"
                sequence:
                  - service: select.select_option
                    target:
                      entity_id: select.wledporch_preset
                    data:
                      option: "{{ ['xmas tri wipe', 'xmas twinkles'] | random }}"
              - conditions:
                  - condition: template
                    value_template: "{{ now().month in [10] }}"
                sequence:
                  - service: select.select_option
                    target:
                      entity_id: select.wledporch_preset
                    data:
                      option: halloween
              - conditions:
                  - condition: template
                    value_template: "{{ now().month in [7] }}"
                sequence:
                  - service: select.select_option
                    target:
                      entity_id: select.wledporch_preset
                    data:
                      option: fourth
              - conditions:
                  - condition: template
                    value_template: "{{ now().month in [11] }}"
                sequence:
                  - service: select.select_option
                    target:
                      entity_id: select.wledporch_preset
                    data:
                      option: thanksgiving
              - conditions:
                  - condition: template
                    value_template: "{{ now().month in [2] }}"
                sequence:
                  - service: select.select_option
                    target:
                      entity_id: select.wledporch_preset
                    data:
                      option: valentines
              - conditions:
                  - condition: template
                    value_template: "{{ now().month in [1,3,4,5,6,8,9] }}"
                sequence:
                  - service: select.select_option
                    target:
                      entity_id: select.wledporch_preset
                    data:
                      option: rainbowtime
      - conditions:
          - condition: trigger
            id: offtime
        sequence:
          - type: turn_off
            device_id: 5909944fa51dc9d1e443e63aa05789d1
            entity_id: light.wledporch_master
            domain: light
mode: single

1 Like

If you’re interested, here’s another way to achieve the same result.

alias: WLED Porch onoff
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: "-00:15:00"
    id: 'on' 
  - platform: time
    at: "23:00:00"
    id: 'off' 
condition: []
action:
  - service: 'light.turn_{{ trigger.id }}'
    target:
      entity_id: light.wledporch_master
  - condition: "{{ trigger.id == 'on'}}"
  - service: select.select_option
    target:
      entity_id: select.wledporch_preset
    data:
      option: >
        {{ {1: 'rainbowtime',
            2: 'valentines',
            3: 'rainbowtime',
            4: 'rainbowtime',
            5: 'rainbowtime',
            6: 'rainbowtime',
            7: 'fourth',
            8: 'rainbowtime',
            9: 'rainbowtime',
            10: 'halloween',
            11: 'thanksgiving',
            12: ['xmas tri wipe', 'xmas twinkles'] | random
           }.get(now().month, 1) }}
mode: single
3 Likes

I wasn’t aware you could chain actions in a row like this and have the condition, like, halt actions when it doesn’t pass. Is that accurate to have it not be part of a chose ?

That said, the code there looks pretty much exactly like what I’d want. Thanks, this looks a million miles better than my wall-o-text :smiley:

Reading!

Conditions can also be part of an action. You can combine multiple service calls and conditions in a single action, and they will be processed in the order you put them in. If the result of a condition is false, the action will stop there so any service calls after that condition will not be executed.

Thanks!

1 Like