Advanced help - creating variabiles or attributes in template sensor, make available to automtion

Hi. I’m trying to generate an RGB value by independently generating R, G, and B. I have a working code implemented as a template sensor right now. In order to turn lights on to an RGB value I need 3 different “attributes” that ideally I could somehow reference from the single sensor. Is this even possible? In other OO languages I’d just create an array and pass that, but I don’t think jinja supports complex objects like this… ?

Template sensor looks like this:

  - sensor:
    - name: "Random RGB Color"
      state: >
        {% set R1 = range(0, 255)|random|int %} 
        {% set G1 = range(0, 255)|random|int %} 
        {% set B1 = range(0, 255)|random|int %} 
        {% if R1 >= G1 and R1 >= B1 %}
          {% set R = 255 %}
          {% else %} {% set R = R1 %}
        {% endif %}
        {% if G1 >= R and G1 >= B1 %}
          {% set G = 255 %}
          {% else %} {% set G = G1 %}
        {% endif %}
        {% if B1 >= R and B1 >= G %}
          {% set B = 255 %}
          {% else %} {% set B = B1 %}
        {% endif %}
        attributes:
          R: {{ R }}
          G: {{ G }}
          B: {{ B }}

and the automation ideally would look something like this (of course this doesn’t work - this is just for the concept)

service: light.turn_on
data:
  rgb_color:
    - {{ state_attr('sensor.random_rgb_color','R' }}
    - {{ state_attr('sensor.random_rgb_color','G' }}
    - {{ state_attr('sensor.random_rgb_color','B' }}
target:
  entity_id: light.house_christmas_lights

Any thoughts on how to achieve this?
thanks

Variables are local. Variables you define in the state template are not available in the attribute template(s). Also you did not actually define a state for the sensor (required). Try this:

- sensor:
    - name: "Random RGB Color"
      state: >
        {% set R1 = range(0, 255)|random|int %} 
        {% set G1 = range(0, 255)|random|int %} 
        {% set B1 = range(0, 255)|random|int %} 
        {% if R1 >= G1 and R1 >= B1 %}
          {% set R = 255 %}
          {% else %} {% set R = R1 %}
        {% endif %}
        {% if G1 >= R and G1 >= B1 %}
          {% set G = 255 %}
          {% else %} {% set G = G1 %}
        {% endif %}
        {% if B1 >= R and B1 >= G %}
          {% set B = 255 %}
          {% else %} {% set B = B1 %}
        {% endif %}
        {{ [R,G,B] }}
service: light.turn_on
data:
  rgb_color: "{{ states('sensor.random_rgb_color') }}"
target:
  entity_id: light.house_christmas_lights

The template will return a string that looks like a list, but I think the service can handle that now.

BRILLIANT! It totally works - your syntax is perfect, and the service can definitely handle it. Massive thank you.

I found the sensor was never updating after Home Assistant started so I had to add a trigger to occasionally update the value. So I added this to the top of script. It does nothing in the actual script except force a re-calc of the RGB values.

{% set sun_trig = states(‘sun.sun’) %}

Your knowledge is absolutely amazing, and thank you for your always being willing to help everyone!

1 Like