Help with storing automation color name

I have modified an automation today so that the automation uses variables for color names only once a day. I had it in the monthly options, but that fired a color change each instance. I might be getting ahead of myself, but this current version of the automation isn’t storing the color name anymore. Any chance there is a better way of doing this?

Thanks in advance!!!

alias: Garage - Backdoor Light
description: Turn light on backdoor when motion in garage
triggers:
  - entity_id:
      - binary_sensor.lumi_lumi_sensor_magnet_aq2_on_off
    to: "on"
    id: Backdoor Open
    trigger: state
  - entity_id:
      - binary_sensor.motion_gar
    to: "on"
    id: Garage Motion
    trigger: state
  - trigger: state
    entity_id:
      - sensor.wled_garage_ip_3
    from: unavailable
    to: 192.168.1.55
    id: Unavail Trigger
  - trigger: time
    at: "01:00:00"
    weekday:
      - sun
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
    alias: ▲Color Daily
    id: ▲Color Daily
conditions:
  - condition: state
    entity_id: input_boolean.automations_kill_switch
    state: "off"
actions:
  - variables:
      month: "{{ now().month }}"
      day: "{{ now().day }}"
  - choose:
      - conditions:
          - condition: trigger
            id:
              - ▲Color Daily
        sequence:
          - variables:
              feb_color_name: >
                {{ ['Crimson', 'DeepPink', 'Fuchsia', 'Magenta',
                'MediumVioletRed', 'Orchid', 'Violet', 'Red', 'HotPink' ] |
                random }}'
              mar_color_name: >
                {{ ['Chartreuse', 'DarkGreen', 'DarkOliveGreen', 'DarkSeaGreen',
                'ForestGreen', 'Green', 'GreenYellow', 'LawnGreen',
                'LightGreen', 'Lime', 'MediumSeaGreen', 'MediumSpringGreen',
                'PaleGreen', 'SeaGreen', 'YellowGreen' ] | random }}
              jul_color_name: |
                {{ ['Crimson', 'Red', 'White', 'Blue', 'DarkBlue' ] | random }}
              sep_color_name: |
                {{ ['Red', 'White', 'Green' ] | random }}
              oct_color_name: >
                {{ ['BlueViolet', 'Coral', 'DarkMagenta', 'DarkOrchid',
                'DarkOrange', 'DarkViolet', 'Orange Red', 'Purple',
                'RebbecaPurple', 'Violet', 'Orange', 'MediumPurple', 'Indigo' ]
                | random }}
              nov_color_name: >
                {{ ['Brown', 'BurlyWood', 'Chocolate', 'DarkGoldenRod',
                'DarKhaki', 'DarkOrange', 'GoldenRod', 'Gold', 'Khaki',
                'LemonChiffon', 'LightGoldenRodYellow', 'LightYellow',
                'Moccasin', 'NavajoWhite', 'PaleGoldenRod', 'PapayaWhip',
                'Peru', 'SandyBrown', 'Sienna', 'Tan', 'Wheat', 'Yellow' ] |
                random }}
              dec_color_name: |
                {{ ['Red', 'White', 'Green' ] | random }}     
      - conditions:
          - condition: template
            value_template: "{{ (2,1) <= (month, day) <= (2,14) }}"
            alias: February
          - condition: trigger
            id:
              - Backdoor Open
              - Garage Motion
        sequence:
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 100
              color_name: "{{ feb_color_name }}"
            target:
              entity_id: light.wled_garage
          - action: input_text.set_value
            target:
              entity_id: input_text.garagehdoorcolor
            data:
              value: "{{ feb_color_name }}"
          - delay:
              hours: 0
              minutes: 3
              seconds: 0
              milliseconds: 0
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.wled_garage
      - conditions:
          - condition: template
            value_template: "{{ (3,1) <= (month, day) <= (3,17) }}"
            alias: March
          - condition: trigger
            id:
              - Backdoor Open
              - Garage Motion
        sequence:
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 100
              color_name: "{{ mar_color_name }}"
            target:
              entity_id: light.wled_garage
          - action: input_text.set_value
            target:
              entity_id: input_text.garagehdoorcolor
            data:
              value: "{{ mar_color_name }}"
          - delay:
              hours: 0
              minutes: 3
              seconds: 0
              milliseconds: 0
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.wled_garage
      - conditions:
          - condition: template
            value_template: "{{ (7,1) <= (now().month, now().day) <= (7,11) }}"
            alias: July
          - condition: trigger
            id:
              - Backdoor Open
              - Garage Motion
        sequence:
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 100
              color_name: "{{ jul_color_name }}"
            target:
              entity_id: light.wled_garage
          - action: input_text.set_value
            target:
              entity_id: input_text.garagehdoorcolor
            data:
              value: "{{ jul_color_name }}"
          - delay:
              hours: 0
              minutes: 3
              seconds: 0
              milliseconds: 0
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.wled_garage
      - conditions:
          - condition: template
            value_template: "{{ (9,1) <= (month, day) <= (9,16) }}"
            alias: September
          - condition: trigger
            id:
              - Backdoor Open
              - Garage Motion
        sequence:
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 100
              color_name: "{{ sep_color_name }}"
            target:
              entity_id: light.wled_garage
          - action: input_text.set_value
            target:
              entity_id: input_text.garagehdoorcolor
            data:
              value: "{{ sep_color_name }}"
          - delay:
              hours: 0
              minutes: 3
              seconds: 0
              milliseconds: 0
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.wled_garage
      - conditions:
          - condition: template
            value_template: "{{ (10,1) <= (month, day) <= (11,1) }}"
            alias: October
          - condition: trigger
            id:
              - Backdoor Open
              - Garage Motion
        sequence:
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 100
              color_name: "{{ oct_color_name }}"
            target:
              entity_id: light.wled_garage
          - action: input_text.set_value
            target:
              entity_id: input_text.garagehdoorcolor
            data:
              value: "{{ oct_color_name }}"
          - delay:
              hours: 0
              minutes: 3
              seconds: 0
              milliseconds: 0
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.wled_garage
      - conditions:
          - condition: template
            value_template: "{{ (11,2) <= (month, day) <= (11,30) }}"
            alias: November
          - condition: trigger
            id:
              - Backdoor Open
              - Garage Motion
        sequence:
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 100
              color_name: "{{ nov_color_name }}"
            target:
              entity_id: light.wled_garage
          - action: input_text.set_value
            target:
              entity_id: input_text.garagehdoorcolor
            data:
              value: "{{ nov_color_name }}"
          - delay:
              hours: 0
              minutes: 3
              seconds: 0
              milliseconds: 0
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.wled_garage
      - conditions:
          - condition: template
            value_template: "{{ (12,1) <= (month, day) <= (12,31) }}"
            alias: December
          - condition: trigger
            id:
              - Backdoor Open
              - Garage Motion
        sequence:
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 100
              color_name: "{{ dec_color_name }}"
            target:
              entity_id: light.wled_garage
          - action: input_text.set_value
            target:
              entity_id: input_text.garagehdoorcolor
            data:
              value: "{{ dec_color_name }}"
          - delay:
              hours: 0
              minutes: 3
              seconds: 0
              milliseconds: 0
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.wled_garage
      - conditions:
          - condition: trigger
            id:
              - Unavail Trigger
        sequence:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.wled_garage
      - conditions:
          - condition: trigger
            id:
              - Backdoor Open
              - Garage Motion
          - condition: sun
            before: sunset
        sequence:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.wled_garage
        alias: Turn Off if triggered before sunset
    default:
      - action: light.turn_on
        metadata: {}
        data:
          brightness_pct: 100
          color_name: blue
        target:
          entity_id: light.wled_garage
      - delay:
          hours: 0
          minutes: 3
          seconds: 0
          milliseconds: 0
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: light.wled_garage
mode: single

The only place all those color variables have a value is when the trigger is “▲Color Daily”. But all the actions that need the values from those variables to set the Input Text require different triggers… so the variables they have access to all have the value none.

Beyond that, you’ve got a lot of repetitive action configuration and there is an issue with the order of the options within the Choose action:

  • The repetition can be fixed by fixing the color storage. Once the day’s color is available from the Input Text, there’s no reason to have individual options in the Choose for each month.

  • When the automation runs, the options are checked from the top of the list to the bottom. So we need to move the Choose option “Turn Off if triggered before sunset” above the option that turns on the lights so it has a chance to check all its conditions before the “turn on” option gets chosen based on the trigger ID alone.

alias: Garage - Backdoor Light
description: Turn light on backdoor when motion in garage
triggers:
  - entity_id:
      - binary_sensor.lumi_lumi_sensor_magnet_aq2_on_off
    to: "on"
    id: Backdoor Open
    trigger: state
  - entity_id:
      - binary_sensor.motion_gar
    to: "on"
    id: Garage Motion
    trigger: state
  - trigger: state
    entity_id:
      - sensor.wled_garage_ip_3
    from: unavailable
    to: 192.168.1.55
    id: Unavail Trigger
  - trigger: time
    at: "01:00:00"
    alias: ▲Color Daily
    id: ▲Color Daily
conditions:
  - condition: state
    entity_id: input_boolean.automations_kill_switch
    state: "off"
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - ▲Color Daily
        sequence:
          - variables:
              month: "{{ now().month }}"
              day: "{{ now().day }}"
              md_tup: "{{ (month, day) }}"
              months:
                feb: "{{ (2,1) <= md_tup <= (2,14) }}"
                mar: "{{ (3,1) <= md_tup <= (3,17) }}"
                jul: "{{ (7,1) <= md_tup <= (7,11) }}"
                sep: "{{ (9,1) <= md_tup <= (9,16) }}"
                oct: "{{ (10,1) <= md_tup <= (11,1) }}"
                nov: "{{ (11,2) <= md_tup <= (11,30) }}"
                dec: "{{ (12,1) <= md_tup <= (12,31) }}"
              current_month: |
                {{ months.items()|selectattr(1, 'eq', true)|map(attribute=0)|first|default('default',true) }}
              colors:
                feb: >
                  {{ ['Crimson', 'DeepPink', 'Fuchsia', 'Magenta',
                  'MediumVioletRed', 'Orchid', 'Violet', 'Red', 'HotPink' ] |
                  random }}'
                mar: >
                  {{ ['Chartreuse', 'DarkGreen', 'DarkOliveGreen', 'DarkSeaGreen',
                  'ForestGreen', 'Green', 'GreenYellow', 'LawnGreen',
                  'LightGreen', 'Lime', 'MediumSeaGreen', 'MediumSpringGreen',
                  'PaleGreen', 'SeaGreen', 'YellowGreen' ] | random }}
                jul: |
                  {{ ['Crimson', 'Red', 'White', 'Blue', 'DarkBlue' ] | random }}
                sep: |
                  {{ ['Red', 'White', 'Green' ] | random }}
                oct: >
                  {{ ['BlueViolet', 'Coral', 'DarkMagenta', 'DarkOrchid',
                  'DarkOrange', 'DarkViolet', 'Orange Red', 'Purple',
                  'RebbecaPurple', 'Violet', 'Orange', 'MediumPurple', 'Indigo' ]
                  | random }}
                nov: >
                  {{ ['Brown', 'BurlyWood', 'Chocolate', 'DarkGoldenRod',
                  'DarKhaki', 'DarkOrange', 'GoldenRod', 'Gold', 'Khaki',
                  'LemonChiffon', 'LightGoldenRodYellow', 'LightYellow',
                  'Moccasin', 'NavajoWhite', 'PaleGoldenRod', 'PapayaWhip',
                  'Peru', 'SandyBrown', 'Sienna', 'Tan', 'Wheat', 'Yellow' ] |
                  random }}
                dec: |
                  {{ ['Red', 'White', 'Green' ] | random }}
                default: blue
            - action: input_text.set_value
              target:
                entity_id: input_text.garagehdoorcolor
              data:
                value: "{{ colors.get(current_month, 'blue') }}"
      - conditions:
          - condition: trigger
            id:
              - Backdoor Open
              - Garage Motion
          - condition: sun
            before: sunset
        sequence:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.wled_garage
        alias: Turn Off if triggered before sunset
      - conditions:
          - condition: trigger
            id:
              - Backdoor Open
              - Garage Motion
        sequence:
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 100
              color_name: "{{ states('input_text.garagehdoorcolor')|default('blue',true) }}"
            target:
              entity_id: light.wled_garage
          - delay:
              hours: 0
              minutes: 3
              seconds: 0
              milliseconds: 0
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.wled_garage
      - conditions:
          - condition: trigger
            id:
              - Unavail Trigger
        sequence:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.wled_garage
mode: single

Man I learn so much from this community!!! I thought I was doing great in my automations and organizing them this way and boom! There is a better method. I will test this one out. Thanks so much!!!

One question I do have is that if I am reading this right, you are only setting a color change once a month or is this trigger once a day?

The color is set daily, since that’s what you described in the original post.

I am following the majority of the automation you built but want to understand this so I can apply it to other automations if you have the time.

The part I am getting a little lost is this section where you are defining “current month”. Couldn’t we have just used “months” to call out the color later in the automation?

              month: "{{ now().month }}"
              day: "{{ now().day }}"
              md_tup: "{{ (month, day) }}"
              months:
                feb: "{{ (2,1) <= md_tup <= (2,14) }}"
                mar: "{{ (3,1) <= md_tup <= (3,17) }}"
                jul: "{{ (7,1) <= md_tup <= (7,11) }}"
                sep: "{{ (9,1) <= md_tup <= (9,16) }}"
                oct: "{{ (10,1) <= md_tup <= (11,1) }}"
                nov: "{{ (11,2) <= md_tup <= (11,30) }}"
                dec: "{{ (12,1) <= md_tup <= (12,31) }}"
              current_month: >
                {{ months.items()|selectattr(1, 'eq',
                true)|map(attribute=0)|first|default('default',true) }}

Maybe explain what is this section doing where you are defining “current month”. Is the documentation behind this I can look at to help understand? I am a little lost on that one. Great solution!

What do you mean by “later in the automation”?


What I’m doing there is just setting it up so that at the end we know which item to pick from the colors mapping.

The current_month variable template breaks down as follows:

{{ months.items()

Here we are taking the mapping months we built previously and using the items() method to return those key-value pairs as a list of tuples (IIRC it’s not technically a list, but its close enough for this discussion). Each tuple will contain 2 items/attributes. The first is the key and the second is the rendered value of the template.

So from {"feb": false,...} to [("feb", false), ...].

Like other lists and tuples, they are indexed starting at 0. So the original key is attribute 0 and the value is attribute 1.

| selectattr(1, 'eq', true)

Here we iterate through all the tuples, selecting only those whose original value template (attribute index 1) rendered as true.

| map(attribute=0)

The map() filter here is being used to look up the attribute that represents the original key, which is the month abbreviations we want to select the correct color. Those whose month templates returned true will all be passed to…

| first

… the first() filter which will return the first item of the sequence. If none of them were true, it will return none.

| default('default',true) }}

In the case that none is returned, we set a default with the default_value parameter set to true. I used the string “default”, but it could be anything so long as you add it to the colors mapping that follows.

Jinja2 - Template Designer Documentation

Home Assistant - Building Templates

  • I didn’t actually use any HA-specific functions in the part you are asking about, but those can be found at the link above.

The other piece I used was the Python dictionary method items(). Jinja2 is built on Python, so a number of Python methods are available to use directly on their respective data types. As far I as know, which methods are available isn’t really documented anywhere… you just have to try them and see what works.

Thanks for taking the time to explain this for me. You can pick up so much in this Community website. I do appreciate the help! I will break this down and attempt to use this same logic in my other light automations. Thanks again and have a great holiday!

I ended up using your code in another automation and can’t thank you enough. It is a lot easier to read / code. Super helpful!

alias: Bathroom - RGB Socket On Off 12-2025
description: ""
triggers:
  - at: "06:00:00"
    id: "off"
    trigger: time
    alias: Turn on
  - at: "19:00:00"
    id: "on"
    trigger: time
    alias: Turn Off
conditions:
  - condition: state
    entity_id: input_boolean.automations_kill_switch
    state: "off"
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - "on"
        sequence:
          - variables:
              month: "{{ now().month }}"
              day: "{{ now().day }}"
              md_tup: "{{ (month, day) }}"
              months:
                feb: "{{ (2,1) <= md_tup <= (2,14) }}"
                mar: "{{ (3,1) <= md_tup <= (3,17) }}"
                jul: "{{ (7,1) <= md_tup <= (7,11) }}"
                sep: "{{ (9,1) <= md_tup <= (9,16) }}"
                oct: "{{ (10,1) <= md_tup <= (11,1) }}"
                nov: "{{ (11,2) <= md_tup <= (11,30) }}"
                dec: "{{ (12,1) <= md_tup <= (12,31) }}"
              current_month: >
                {{ months.items()|selectattr(1, 'eq',
                true)|map(attribute=0)|first|default('default',true) }}
              colors:
                feb: >
                  {{ ['Crimson', 'DeepPink', 'Fuchsia', 'Magenta',
                  'MediumVioletRed', 'Orchid', 'Violet', 'Red', 'HotPink' ] |
                  random }}'
                mar: >
                  {{ ['Chartreuse', 'DarkGreen', 'DarkOliveGreen',
                  'DarkSeaGreen', 'ForestGreen', 'Green', 'GreenYellow',
                  'LawnGreen', 'LightGreen', 'Lime', 'MediumSeaGreen',
                  'MediumSpringGreen', 'PaleGreen', 'SeaGreen', 'YellowGreen' ]
                  | random }}
                jul: >
                  {{ ['Crimson', 'Red', 'White', 'Blue', 'DarkBlue' ] | random
                  }}
                sep: |
                  {{ ['Red', 'White', 'Green' ] | random }}
                oct: >
                  {{ ['BlueViolet', 'Coral', 'DarkMagenta', 'DarkOrchid',
                  'DarkOrange', 'DarkViolet', 'Orange Red', 'Purple',
                  'RebbecaPurple', 'Violet', 'Orange', 'MediumPurple', 'Indigo'
                  ] | random }}
                nov: >
                  {{ ['Brown', 'BurlyWood', 'Chocolate', 'DarkGoldenRod',
                  'DarKhaki', 'DarkOrange', 'GoldenRod', 'Gold', 'Khaki',
                  'LemonChiffon', 'LightGoldenRodYellow', 'LightYellow',
                  'Moccasin', 'NavajoWhite', 'PaleGoldenRod', 'PapayaWhip',
                  'Peru', 'SandyBrown', 'Sienna', 'Tan', 'Wheat', 'Yellow' ] |
                  random }}
                dec: |
                  {{ ['Red', 'White', 'Green' ] | random }}
                default: blue
          - action: input_text.set_value
            target:
              entity_id: input_text.rgbccolorame
            data:
              value: "{{ colors[current_month] }}"
          - target:
              entity_id: light.rgb_smart_plug
            data:
              brightness_pct: 100
              color_name: "{{ states('input_text.rgbccolorame')|default('blue',true) }}"
            action: light.turn_on
        alias: Turn on
      - conditions:
          - condition: trigger
            id: "off"
        sequence:
          - data: {}
            target:
              entity_id: light.rgb_smart_plug
            action: light.turn_off
        alias: "Off"
mode: single