Template: light to "(...) | random" color? // rgb_color to xy_color?

Hi everybody,


While writing this, I realized that I need xy_color instead of rgb_color. Is there a way to convert rgb_color to xy_color in Home Assistant to automatically “translate” the color values I already have? I searched for a rgb to xy color solution only, but the pages I found want to translate to xyz color, and Home Assistant does not want this z value…


I have created a template sensor to chose a random value from a list of colors; this sensor gets update every 2 minutes. However, when I try setting a light to the sensor’s value, it will not turn on.

What am I doing wrong?

lights/random_color.yaml

sensor:
  - platform: template
    sensors:
      random_color:
        friendly_name: "Random Color"
        value_template: >
          {{ (
          [0, 232, 0],
          [0, 232, 185],
          [131, 0, 232],
          [193, 0, 232],
          [232, 0, 185],
          [255, 111, 67],
          [255, 166, 10],
          [255, 240, 97],
          [152, 255, 61]
          ) | random}}

automation:
  - alias: "Reload Random Color"
    trigger:
      - platform: time_pattern
        minutes: "/2" # alle zwei Minuten
    action:
      service: homeassistant.update_entity
      entity_id: sensor.random_color

lights/bad_oben.yaml

    action:
      - service: light.turn_on
        entity_id: light.zwischenzimmer_aurora_light
        data_template:
          brightness: 180
          rgb_color: "{{ states('sensor.random_color') }}"

The sensor does return a valid value that changes every 2 minutes

…so I assume that my workflow is correct and the only problem is that I need a different color format. Is this correct? And if so, what do you suggest I do?

Thank you for your ideas :slight_smile:

i have this automation for my bed lights…

- id: RGB Lampe - Farbwechsel 5min
  alias: "RGB Lampe LEDs Farbwechsel 5min"
  initial_state: 'on'
  trigger:
    platform: time_pattern
    minutes: "/5"
  condition:
    condition: template
    value_template: '{{ is_state("light.lampe_rgb_schlafzimmer", "on") }}'
  action:
  - service: light.turn_on
    entity_id: light.lampe_rgb_schlafzimmer
    data_template:
      hs_color:
        - "{{ range(360)|random }}"
        - "{{ range(80,101)|random }}"
      brightness_pct: 100

this works fine for me

2 Likes

Thank you oder auch vielen Dank :slight_smile:

What kind of lights do you use? It seems to me like the ones that cause problems really do rely on xy_color and they don’t work because I provide them with rgb_color, which they cannot handle.

I use only MagicHome Modules, and Tasmota flasht Lights

A another script i use when the rainsensor is on.

wetterwolke_dachsensor_nass:
  alias: "Wetterwolke Dachsensor Nass"
  sequence:
  - service: light.turn_on
    entity_id: light.wolke_wohnzimmer
    data_template:
      rgb_color:
        - "0"
        - "{{ range(0,150)|random }}"
        - "{{ range(0,255)|random }}"
      brightness_pct: 100

This script use only blue colors.

3 Likes

Offtopic first, because that is a damn nice idea: what sensor do you use? I tried using the Xiaomi SJCGQ11LM just to display whether or not it was raining outside, but it didn’t work reliably, perhaps because it requires a minimum amount of water to touch it before it triggers? And I read that those generic rain sensors (example) “decay” over time and then cannot be used. What is your experience there?

Back to topic

Do any of you either have a way to control lights that only accept xy_color input or to have Home Assistant convert rgb_color directly to xy_color? I have looked into color conversion via AppDaemon recently for a project, but don’t have any experience with xy_color and if there was a solution already, I wouldn’t have to cook up more spaghetti code and use a solution by somebody more experienced :wink:

https://community.home-assistant.io/t/portable-rain-drop-sensor-made-with-xiaomi-door-sensor/74090

i use this for over 1 year and have no problems with it

I wanted the thank you for example in your first post. For my RGB led strip, this worked perfectly to change the color after a certain amount of time. (RGB values as shown worked for me).