Color values for lights using variables

So… Setting rgb_color using variables in data_template apparently doesn’t work because data_template variables are treated as strings, and rgb_color needs a list of integers.

I’ve seen suggestions to use profiles, which requires me to set up a color scheme in light_profiles.csv - which requires a set of xyz color values, whereas I have not seen any suggestions on how to convert colors to that color system. The ones I have found have not generated any resemblance of the RGB color I put in.

I bet using color_name would work nicely, as they are strings. But unfortunately, CSS3 has only named 147 colors.

So how on earth do we change color values dynamically with variables?

This works for me

      - service: light.turn_on
        data_template:
          entity_id: light.milight_gu10
          rgb_color:
            - '{{ states.sensor.random_rgb.state.split(",")[0] }}'
            - '{{ states.sensor.random_rgb.state.split(",")[1] }}'
            - '{{ states.sensor.random_rgb.state.split(",")[2] }}'
          brightness: 140

So what kind of sensor is the random_rgb sensor you’re using?

It’s a file sensor that reads a simple text file.

  - platform: file
    name: Random RGB
    file_path: /home/homeassi/.homeassistant/random_rgb.txt

The values could also be generated with a random sensor, but that gives some crazy colors some times.
I created a file ‘color_wheel.txt’

126,0,255
190,0,255
255,0,250
255,0,190
255,0,126
255,0,67
255,0,0
255,67,0
255,126,0
255,190,0
250,255,0
190,255,0
126,255,0
68,255,0
0,255,0
0,255,67
0,255,126
0,255,190
0,250,255
0,190,255
0,126,255
0,67,255
0,0,255
67,0,255

and a cron job every 5 minutes that randomly picks a line from this file and writes it to the file that the sensor reads.

*/5 * * * *	/usr/bin/shuf -n 1 /home/homeassi/.homeassistant/color_wheel.txt > /home/homeassi/.homeassistant/random_rgb.txt

That looks promising for my use case, then. I tried using custom attributes of a sensor to keep my rgb values, but HA only treated them as none whatever I did. Searching for a solution gave me the idea that it was impossible. I’ll see if I can transfer your solution. Thanks!

If you do, please post back.

I’ve posted and currently have 4 tabs open for when I get a few hours at home so I can try to turn my lights on at the correct colour temperature, but have only got as far as making a nodered flow that sets some input booleans. I’ve not had a chance to us them ask inputs for turning the lights on yet.

I think I found the source of all my troubles.

I thought it’d be a nice idea to parse the RGB value from whatever source within the data_template. But apparently using multiple lines in rgb_value changes the value type from list to string and fails (source).

OK. I finally cracked this.

How HA treats every variable in data_template as a f***ing string was the most annoying culprit here.

First a little backstory on my use case. I have a Deconz zigbee dongle that delivers a daylight sensor. This sensor tracks the daylight phase. Up until now I’ve used this sensor to trigger different scenes, but there are many annoyances with using scenes. Such as lack of transitions or the possibility to adjust one or many lights at the same time.

So…

  1. My Deconz deliver the daylight phase
  2. I created a platform sensor with attributes containing rgb values for each phase (+ variations - ie, different moods on some bulbs, different season etc)
  3. I created a script to parse the daylight phase and deliver the correct rgb color as a variable to a second script. This due to the f***-up of rgb_color: >
  4. I created a script to actually set the current rgb value to the bulbs of my choice with the transition of my choice

The attributes sensor containing all the RGB values (you don’t need to encapsulate the attributes in “”, but I chose to):

# Daylight color temperatures
sensor
  - platform: template
    sensors:
      daylight_color_temps:
        value_template: "{{ states.sensor.daylight.state }}"
        attribute_templates:
          sunrise_start_v1: "255, 220, 184"
          sunrise_start_v2: "255, 184, 184"
          sunrise_end_v1: "255, 228, 189"
          sunrise_end_v2: "255, 194, 189"
          golden_hour_1_v1: "255, 236, 195"
          golden_hour_1_v2: "255, 205, 194"
          solar_noon_v1: "255, 244, 200"
          solar_noon_v2: "238, 255, 199"
          golden_hour_2_v1: "253, 229, 183"
          golden_hour_2_v2: "252, 192, 182"
          sunset_start_v1: "250, 214, 165"
          sunset_start_v2: "250, 172, 165"
          sunset_end_v1: "253, 94, 83"
          sunset_end_v2: "252, 83, 157"
          dusk_v1: "120, 99, 97"
          dusk_v2: "111, 136, 146"
          nautical_dusk_v1: "96, 80, 87"
          nautical_dusk_v2: "97, 82, 80"
          night_start_v1: "72, 61, 76"
          night_start_v2: "65, 61, 76"
          nadir_v1: "72, 52, 117"
          nadir_v2: "52, 65, 117"
          night_end_v1: "118, 94, 134"
          nautical_dawn_v1: "164, 136, 151"
          nautical_dawn_v2: "162, 135, 163"
          dawn_v1: "209, 178, 167"
          dawn_v2: "209, 167, 177"
          alarm_v1: "192,0,0"
          alarm_v2": "15,0,191"

The parsing script (with fallback values just so it wouldn’t fail):

register_light:
  sequence:
  - service: script.turn_on
    entity_id: script.set_light
    data_template:
      variables:
        entities: "{{ entities | default('light.light_hue_livingroom_tv_1, light.light_hue_livingroom_tv_2', true) }}"
        transition: "{{ transition | default(3, true) }}"
        color: >
          {%- set period = period | default("sunrise_start") -%}
          {%- set version = version | default("v1") -%}
          {%- set attrib = period ~ "_" ~ version -%}
          "{{ state_attr("sensor.daylight_color_temps", attrib) }}"

The last script that actually sets the color:

set_light:
  sequence:
  - service: light.turn_on
    data_template:
      entity_id: '{{ entities | default("light.light_hue_livingroom_tv_1", true) }}'
      transition: '{{ transition | default(0, true) }}'
      rgb_color: 
        - '{{ color.split(",")[0].strip() | default(100, true) | int }}'
        - '{{ color.split(",")[1].strip() | default(50, true) | int }}'
        - '{{ color.split(",")[2].strip() | default(200, true) | int }}'
1 Like