For a "poor man's ambilight with smart bulp": Is there a way to calculate the average of a color (over time) in Home Assistant? Aka smooth colors?

Use case

I use Hypherion/HypherHDR (a fork of Hypherion) with a custom HACS Hyper-HA integration to get the average color of my TV screen.

Now, the aim is to do a poor mans ambilight with just a smart light that shines somewhat the correct light.

Of course, this is more of a fun project and to test out whether that is actually useful or how it works. Of course, I totally see and would recommend to use a proper LED stripe and directly let it control via that. But well… here we go for just a ambient room I may say. :upside_down_face:
But well… maybe with the right tuning it turns out to be useful. :sweat_smile:

Approach

The integration exposes the “average screen color” already as a HEX value:

I guess that is most useful to use here.

Code so far

With some AI help and trying out stuff, I was able to generate some working automation:

alias: Simple ambilight
description: ''
triggers:
  - trigger: state
    entity_id:
      - sensor.yourdevice_hyperhdr_averagecolor
    for:
      hours: 0
      minutes: 0
      seconds: 0.5 # specifically throttled, as I noticed fast enough light changes are unrealistic anyway, so "smoother" values are better 
conditions:
  - condition: state
    entity_id: light.yourlight
    state:
      - 'on'
actions:
  - action: light.turn_on
    data:
      transition: 0.25 # again for smoothness, but should still ensure fast updates
      rgb_color: >
        {% set color =
        states('sensor.yourdevice_hyperhdr_averagecolor')
          | replace('#', '') %}
        {{ [
          color[0:2] | int(16),
          color[2:4] | int(16),
          color[4:6] | int(16)
        ] }}
    target:
      entity_id: light.yourlight
mode: restart # useful to prevent stale color updates

This works surprisingly well already, though one can often feel it “lags behind” the TV image. As indicated above, I already added some “smoothing”. To properly do that over longer time spans without wild color changes, “smoothing” over a longer time span would be needed. Aka, mathematically speaking, calculating the average of the color over time?

In my imagination, this would capture the “scene” over a longer period of time, maybe even dunno, the last, 10 seconds or so? Then a nature documentary in a jungle would be primarily green while something else would be different.
As said, it’s kind of an experiment. :sweat_smile:

Question

Now obviously Home Assistant does have an average function: That said, again obviously, calculating an average of a color obviously is harder, but possible. Apparently, depending on the color space/representation you use, there are also a lot of different algorithms.


Also cross-posted to the HA integration discussions forum I use to get their ideas/opinions on that.

Wait, also, does something in HA “invert” (or average) RGB values (already?). I just noticed when manually testing the colors do not quite fit “togeher”:

Now, this is clearly orange in the stream/gradient and #9a3c31 is also correctly reported as such and also seems to be it.

However when you look the the history of the sensor, it s suddenly blue:

I just figured out when clicking on it, you also see the “wrong” color value as the light value:

#0c0b1a is that dark blue.

So then, can this be configured?