Change Light Color based on Outside Temperature

True, but I was unable to locate the exact color spectrum mathematics script that would handle the multiple color transitions that I am using above. It is based off the the color spectrum below, which is also used on my weather website. I have seen ones for sun values which are very cool, too. I plan on using a crystal lamp with my rgb light for this outside weather temperature color project…

spectrum

1 Like

The color spectrum example you posted is a fairly standard arrangement. In fact it reminded me of what’s used in a mapping app called Locus Map. It can display tracks using colors that represent either:

speed, change of speed, altitude, slope, GPS accuracy, heart rate or pedaling cadence

For example, a track running over flat terrain may be shown in green, then yellow as the slope increases, through orange to finally red for the steepest slopes. For downhill stretches, the colors change from pale blue, to dark blue, violet, etc.

https://docs.locusmap.eu/doku.php?id=manual:user_guide:tracks:management#coloring_mode

So the algorithm to correlate a parameter (like speed, altitude, temperature, etc) to a color value is in common use. The trick is finding it because most internet searches zero in on color temperature which is a different subject.

I love these kinds of posts. I too am all for coding efficiency, but at some point, if the ‘best / tersest / cleanest’ solution takes 20 times longer to create than the ‘ugly / longer / initial’ solution - is it really worth trying to get to perfection?

A guy I interviewed once told me that it takes as long to get a project from 80-100% completion/perfection as it does to get it from 0% to 80% completion. So would you rather have two, functional projects 80% complete, or one 100%?

Also, for the record, you could have one 80% complete and then spend the rest of the time w/ your family / watching football / drinking beer / some other less hair-pulling exercise… :wink:

2 Likes

Everything returned from templates are strings. rgb_color really needs to accept comma separated values. Instead it only accepts a yaml or json list. This is the same issue as the homeassistant.turn_on entity_id field. Just doesn’t accept comma separated.

EDIT: I feel like there needs to be a base validation class for yaml lists that accepts comma separated values that accepts expected types. It would have to be restricted to simple types: string (possibly entity_id), ints, floats, etc. And it would need to be used during templates. Not sure how templates resolve that currently anyways.

It depends on what you plan to derive from the exercise.

If it’s expensive in terms of time and/or money, and you have neither available, go with what you know. The result may be fugly but only you see what’s under the hood.

On the other hand, if you have the time to invest in learning something new, the journey to the end-result may not only be more rewarding but it may pay dividends in the future. At a later date, one may find other applications for the new-found knowledge that will save time and effort. In addition, sharing that knowledge with others improves everyone’s game.

Perhaps you can help me understand how that jibes with the documentation’s description of the from_json filter:

The from_json filter operates similarly, but in the other direction, de-serializing a JSON string back into an object.

I guess the answer is: and then that object is ultimately flattened into a string because there’s no way out of a Jinja template other than being a string. :slightly_smiling_face:

That’s just for taking a string and turning it into json so that you can use it as a json object. Example would be putting json in a state objects state, then using a template sensor to get individual data out. It doesn’t actually convert it after the template is resolved.

OK, my perfectionist nature (I didn’t say I always adhere to my 80% rule…) condenses to this:

{% set temp = states('sensor.outdoor_temperature') | int %}

{% set temp_rgb = { 2:'255->232->244',3:'255->209->234',4:'255->184->222',5:'255->161->211',6:'255->138->202',7:'255->115->190',8:'255->89->178',9:'255->66->167',10:'255->43->156',11:'255->20->146',12:'245->18->158',13:'235->15->169',14:'224->13->179',15:'214->13->191',16:'207->10->200',17:'198->8->212',18:'186->5->222',19:'175->3->232',20:'167->0->245',21:'157->0->255',22:'140->0->255',23:'123->0->255',24:'111->0->255',25:'93->0->255',26:'76->0->255',27:'64->0->255',28:'47->0->255',29:'30->0->255',30:'17->0->255',31:'0->0->255',32:'0->25->252',33:'0->50->250',34:'0->75->250',35:'0->103->247',36:'0->126->245',37:'0->155->245',38:'0->178->242',39:'0->206->242',40:'0->228->240',41:'0->255->238',42:'5->250->217',43:'10->245->198',44:'15->242->182',45:'20->237->161',46:'26->232->143',47:'31->227->122',48:'36->222->101',49:'41->219->83',50:'46->214->66',51:'51->209->46',52:'61->214->41',53:'66->219->36',54:'75->222->31',55:'88->227->28',56:'96->232->23',57:'106->237->18',58:'112->242->13',59:'122->245->8',60:'132->250->5',61:'140->255->0',62:'153->255->0',63:'162->255->0',64:'174->255->0',65:'187->255->0',66:'195->255->0',67:'208->255->0',68:'221->255->0',69:'234->255->0',70:'242->255->0',71:'255->255->0',72:'255->247->0',73:'255->238->0',74:'255->230->0',75:'255->221->0',76:'255->213->0',77:'255->204->0',78:'255->200->0',79:'255->191->0',80:'255->183->0',81:'255->174->0',82:'255->162->0',83:'255->149->0',84:'255->136->0',85:'255->119->0',86:'255->106->0',87:'255->94->0',88:'255->81->0',89:'255->68->0',90:'255->55->0',91:'255->43->0',92:'255->38->0',93:'255->34->0',94:'255->30->0',95:'255->21->0',96:'255->17->0',97:'255->13->0',98:'255->9->0',99:'255->4->0'} %}
{% set output = temp_rgb[temp] %} 

{% if temp < 2 %}
 255 -> 255 -> 255
{% elif temp > 99 %}
 43 -> 255 -> 113
{% else %}
 {{ output }}
{% endif %}

I think I converted / created the array correctly

All good points.

Your code looks interesting and takes it from being lots of vertical code to a large horizontal one. :slight_smile: Lots of ways to do things and the happy balance probably ends up somewhere between time spent and clean/concise code. Doing the best to meet the two together.

Been enjoying the weather temperature light here. Waiting for my crystal lamp to arrive for the final setup, but it is in my office lamp which is looking really cool. As you can tell by the color of the light, it is pretty cold here right now. In the low 30s (F).

1 Like

Always willing to learn; how is this value handled by rgb_color?

No idea, tried to quickly figure out what result Mark’s data_template was exporting and put it in a similar format.

I haven’t implemented this fully, but did created a sensor after performing a few quick find/replaces to update the array values from r -> g -> b into [r,g,b] (no promises this is any more helpful, :rofl:)

Thanks, now I understand why it looks the way it does: you tried to create a literal copy of his template. However, you didn’t take into consideration the actual purpose of the hyphen and greater-than symbols (pop-culture reference to A Canticle for Liebowitz).

  • To indicate a list item in YAML, you use a hyphen.
  • To indicate an initial line continuation in Jinja2, you use a greater-than symbol.

After the Jinja2 interpreter evaluates Mark’s template the result will be a valid list in YAML. For example, if the temperature is 30 degrees:

      rgb_color:
        - 17
        - 0
        - 255

That’s a valid list in YAML and is the equivalent of this:

      rgb_color: [17, 0, 255]

In contrast, this:

     rgb_color:  17 -> 0 -> 255

is not valid YAML for a list and the greater-than symbols will be handled literally. The whole thing will be treated as a string and will cause an error.

To understand what rgb_color requires, I encourage you to review my earlier post showing the two formats that are known to work.

No, that won’t work for the reasons that have been given in several posts above.

I use this

script:
luz_velador:
alias: luces x temperatura
description: cambio de luz de velador x temperatura del velador
sequence:
- service: light.turn_on
data_template:
entity_id: light.dormitorio_luz_de_noche
brightness: 200
color_name: >
{% if states.sensor.dormitorio_temperatura.state|int >= 35 %} red
{% elif states.sensor.dormitorio_temperatura.state|int >= 30 %} deeppink
{% elif states.sensor.dormitorio_temperatura.state|int <= 15 %} cyan
{% elif states.sensor.dormitorio_temperatura.state|int <= 10 %} deepskyblue
{% else %} white
{% endif %}

Thanks for the automation. I used this to make a similar one with a bit different color gradient and Celsius. I also added a condition to only change the color when the light is on. Here is the code if someone is interested:

This is the gradient used from -35 °C to 35 °C:
gradient

- id: 'Outside_Temp_Color_Change'
  alias: Outside Temp Color Change
  initial_state: 'on'
  condition:
    - condition: state
      entity_id: light.balcony_light
      state: 'on'
  trigger:
    platform: state
    entity_id: sensor.met_no_temperature
  action:
    service: light.turn_on
    entity_id: light.balcony_light
    data_template:
      rgb_color:
        - >
          {% if states.sensor.met_no_temperature.state | int <= -35 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -34 %} 240
          {% elif states.sensor.met_no_temperature.state | int == -33 %} 225
          {% elif states.sensor.met_no_temperature.state | int == -32 %} 210
          {% elif states.sensor.met_no_temperature.state | int == -31 %} 195
          {% elif states.sensor.met_no_temperature.state | int == -30 %} 180
          {% elif states.sensor.met_no_temperature.state | int == -29 %} 165
          {% elif states.sensor.met_no_temperature.state | int == -28 %} 150
          {% elif states.sensor.met_no_temperature.state | int == -27 %} 135
          {% elif states.sensor.met_no_temperature.state | int == -26 %} 120
          {% elif states.sensor.met_no_temperature.state | int == -25 %} 105
          {% elif states.sensor.met_no_temperature.state | int == -24 %} 90
          {% elif states.sensor.met_no_temperature.state | int == -23 %} 75
          {% elif states.sensor.met_no_temperature.state | int == -22 %} 60
          {% elif states.sensor.met_no_temperature.state | int == -21 %} 45
          {% elif states.sensor.met_no_temperature.state | int == -20 %} 30
          {% elif states.sensor.met_no_temperature.state | int == -19 %} 15
          {% elif states.sensor.met_no_temperature.state | int == -18 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -17 %} 14
          {% elif states.sensor.met_no_temperature.state | int == -16 %} 28
          {% elif states.sensor.met_no_temperature.state | int == -15 %} 43
          {% elif states.sensor.met_no_temperature.state | int == -14 %} 57
          {% elif states.sensor.met_no_temperature.state | int == -13 %} 71
          {% elif states.sensor.met_no_temperature.state | int == -12 %} 85
          {% elif states.sensor.met_no_temperature.state | int == -11 %} 99
          {% elif states.sensor.met_no_temperature.state | int == -10 %} 113
          {% elif states.sensor.met_no_temperature.state | int == -9 %} 128
          {% elif states.sensor.met_no_temperature.state | int == -8 %} 142
          {% elif states.sensor.met_no_temperature.state | int == -7 %} 156
          {% elif states.sensor.met_no_temperature.state | int == -6 %} 170
          {% elif states.sensor.met_no_temperature.state | int == -5 %} 184
          {% elif states.sensor.met_no_temperature.state | int == -4 %} 198
          {% elif states.sensor.met_no_temperature.state | int == -3 %} 213
          {% elif states.sensor.met_no_temperature.state | int == -2 %} 227
          {% elif states.sensor.met_no_temperature.state | int == -1 %} 241
          {% elif states.sensor.met_no_temperature.state | int == 0 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 1 %} 247
          {% elif states.sensor.met_no_temperature.state | int == 2 %} 239
          {% elif states.sensor.met_no_temperature.state | int == 3 %} 231
          {% elif states.sensor.met_no_temperature.state | int == 4 %} 223
          {% elif states.sensor.met_no_temperature.state | int == 5 %} 215
          {% elif states.sensor.met_no_temperature.state | int == 6 %} 207
          {% elif states.sensor.met_no_temperature.state | int == 7 %} 199
          {% elif states.sensor.met_no_temperature.state | int == 8 %} 191
          {% elif states.sensor.met_no_temperature.state | int == 9 %} 183
          {% elif states.sensor.met_no_temperature.state | int == 10 %} 175
          {% elif states.sensor.met_no_temperature.state | int == 11 %} 183
          {% elif states.sensor.met_no_temperature.state | int == 12 %} 191
          {% elif states.sensor.met_no_temperature.state | int == 13 %} 199
          {% elif states.sensor.met_no_temperature.state | int == 14 %} 207
          {% elif states.sensor.met_no_temperature.state | int == 15 %} 215
          {% elif states.sensor.met_no_temperature.state | int == 16 %} 223
          {% elif states.sensor.met_no_temperature.state | int == 17 %} 231
          {% elif states.sensor.met_no_temperature.state | int == 18 %} 239
          {% elif states.sensor.met_no_temperature.state | int == 19 %} 247
          {% elif states.sensor.met_no_temperature.state | int == 20 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 21 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 22 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 23 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 24 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 25 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 26 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 27 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 28 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 29 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 30 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 31 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 32 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 33 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 34 %} 255
          {% elif states.sensor.met_no_temperature.state | int >= 35 %} 255
          {% else %} 43
          {% endif %}
        - >
          {% if states.sensor.met_no_temperature.state | int <= -35 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -34 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -33 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -32 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -31 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -30 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -29 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -28 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -27 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -26 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -25 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -24 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -23 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -22 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -21 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -20 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -19 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -18 %} 0
          {% elif states.sensor.met_no_temperature.state | int == -17 %} 14
          {% elif states.sensor.met_no_temperature.state | int == -16 %} 28
          {% elif states.sensor.met_no_temperature.state | int == -15 %} 43
          {% elif states.sensor.met_no_temperature.state | int == -14 %} 57
          {% elif states.sensor.met_no_temperature.state | int == -13 %} 71
          {% elif states.sensor.met_no_temperature.state | int == -12 %} 85
          {% elif states.sensor.met_no_temperature.state | int == -11 %} 99
          {% elif states.sensor.met_no_temperature.state | int == -10 %} 113
          {% elif states.sensor.met_no_temperature.state | int == -9 %} 128
          {% elif states.sensor.met_no_temperature.state | int == -8 %} 142
          {% elif states.sensor.met_no_temperature.state | int == -7 %} 156
          {% elif states.sensor.met_no_temperature.state | int == -6 %} 170
          {% elif states.sensor.met_no_temperature.state | int == -5 %} 184
          {% elif states.sensor.met_no_temperature.state | int == -4 %} 198
          {% elif states.sensor.met_no_temperature.state | int == -3 %} 213
          {% elif states.sensor.met_no_temperature.state | int == -2 %} 227
          {% elif states.sensor.met_no_temperature.state | int == -1 %} 241
          {% elif states.sensor.met_no_temperature.state | int == 0 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 1 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 2 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 3 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 4 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 5 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 6 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 7 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 8 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 9 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 10 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 11 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 12 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 13 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 14 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 15 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 16 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 17 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 18 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 19 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 20 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 21 %} 238
          {% elif states.sensor.met_no_temperature.state | int == 22 %} 221
          {% elif states.sensor.met_no_temperature.state | int == 23 %} 204
          {% elif states.sensor.met_no_temperature.state | int == 24 %} 187
          {% elif states.sensor.met_no_temperature.state | int == 25 %} 170
          {% elif states.sensor.met_no_temperature.state | int == 26 %} 153
          {% elif states.sensor.met_no_temperature.state | int == 27 %} 136
          {% elif states.sensor.met_no_temperature.state | int == 28 %} 119
          {% elif states.sensor.met_no_temperature.state | int == 29 %} 102
          {% elif states.sensor.met_no_temperature.state | int == 30 %} 85
          {% elif states.sensor.met_no_temperature.state | int == 31 %} 68
          {% elif states.sensor.met_no_temperature.state | int == 32 %} 51
          {% elif states.sensor.met_no_temperature.state | int == 33 %} 34
          {% elif states.sensor.met_no_temperature.state | int == 34 %} 17
          {% elif states.sensor.met_no_temperature.state | int >= 35 %} 0
          {% else %} 255
          {% endif %}
        - >
          {% if states.sensor.met_no_temperature.state | int <= -35 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -34 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -33 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -32 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -31 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -30 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -29 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -28 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -27 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -26 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -25 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -24 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -23 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -22 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -21 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -20 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -19 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -18 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -17 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -16 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -15 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -14 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -13 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -12 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -11 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -10 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -9 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -8 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -7 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -6 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -5 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -4 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -3 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -2 %} 255
          {% elif states.sensor.met_no_temperature.state | int == -1 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 0 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 1 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 2 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 3 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 4 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 5 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 6 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 7 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 8 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 9 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 10 %} 255
          {% elif states.sensor.met_no_temperature.state | int == 11 %} 230
          {% elif states.sensor.met_no_temperature.state | int == 12 %} 204
          {% elif states.sensor.met_no_temperature.state | int == 13 %} 179
          {% elif states.sensor.met_no_temperature.state | int == 14 %} 153
          {% elif states.sensor.met_no_temperature.state | int == 15 %} 128
          {% elif states.sensor.met_no_temperature.state | int == 16 %} 102
          {% elif states.sensor.met_no_temperature.state | int == 17 %} 77
          {% elif states.sensor.met_no_temperature.state | int == 18 %} 51
          {% elif states.sensor.met_no_temperature.state | int == 19 %} 26
          {% elif states.sensor.met_no_temperature.state | int == 20 %} 0
          {% elif states.sensor.met_no_temperature.state | int == 21 %} 0
          {% elif states.sensor.met_no_temperature.state | int == 22 %} 0
          {% elif states.sensor.met_no_temperature.state | int == 23 %} 0
          {% elif states.sensor.met_no_temperature.state | int == 24 %} 0
          {% elif states.sensor.met_no_temperature.state | int == 25 %} 0
          {% elif states.sensor.met_no_temperature.state | int == 26 %} 0
          {% elif states.sensor.met_no_temperature.state | int == 27 %} 0
          {% elif states.sensor.met_no_temperature.state | int == 28 %} 0
          {% elif states.sensor.met_no_temperature.state | int == 29 %} 0
          {% elif states.sensor.met_no_temperature.state | int == 30 %} 0
          {% elif states.sensor.met_no_temperature.state | int == 31 %} 0
          {% elif states.sensor.met_no_temperature.state | int == 32 %} 0
          {% elif states.sensor.met_no_temperature.state | int == 33 %} 0
          {% elif states.sensor.met_no_temperature.state | int == 34 %} 0
          {% elif states.sensor.met_no_temperature.state | int >= 35 %} 0
          {% else %} 113
          {% endif %}
1 Like

If you’re interested, you can cross-reference temperature to color using a map (dictionary) instead of a long chain of if-elif statements. The result is a substantial reduction in the automation’s code.

- id: 'Outside_Temp_Color_Change'
  alias: Outside Temp Color Change
  trigger:
  - platform: state
    entity_id: sensor.met_no_temperature
  condition: "{{is_state('light.balcony_light', 'on') }}"
  action:
  - variables:
      t: '{{ trigger.to_state.state | int }}'
      red_map: {-34: 240, -33: 225, -32: 210, -31: 195,
         -30: 180, -29: 165, -28: 150, -27: 135, -26: 120, -25: 105, -24: 90, -23: 75, -22: 60, -21: 45,
         -20: 30,  -19: 15, -18: 0, -17: 14, -16: 28, -15: 43, -14: 57, -13: 71, -12: 85, -11: 99,
         -10: 113,  -9: 128, -8: 142, -7: 156, -6: 170, -5: 184, -4: 198, -3: 213, -2: 227, -1: 241,
           0: 255,   1: 247, 2: 239, 3: 231, 4: 223, 5: 215, 6: 207, 7: 199, 8: 191, 9: 183,
          10: 175,  11: 183, 12: 191, 13: 199, 14: 207, 15: 215, 16: 223, 17: 231, 18: 239, 19: 247} 
      green_map: {-17: 14, -16: 28, -15: 43, -14: 57, -13: 71, -12: 85, -11: 99,
         -10: 113, -9: 128, -8: 142, -7: 156, -6: 170, -5: 184, -4: 198, -3: 213, -2: 227, -1: 241,
           0: 255,  1: 255, 2: 255, 3: 255, 4: 255, 5: 255, 6: 255, 7: 255, 8: 255, 9: 255,
          10: 255, 11: 255, 12: 255, 13: 255, 14: 255, 15: 255, 16: 255, 17: 255, 18: 255, 19: 255,
          20: 255, 21: 238, 22: 221, 23: 204, 24: 187, 25: 170, 26: 153, 27: 136, 28: 119, 29: 102,
          30: 85, 31: 68, 32: 51, 33: 34, 34: 17}
      blue_map: {11: 230, 12: 204, 13: 179, 14: 153, 15: 128, 16: 102, 17: 77, 18: 51, 19: 26}
  - service: light.turn_on
    target:
      entity_id: light.balcony_light
    data:
      rgb_color: 
      - '{{ 255 if t <= -35 or t >= 20 else red_map[t] }}'
      - '{{ 0 if t <= -18 or t >= 35 else green_map[t] }}'
      - '{{ 255 if t <= 10 else 0 if t >= 20 else blue_map[t] }}'
3 Likes

@Zermo thanks for the code, this is exactly what I wanted to do. However, I would like to tweak the colors used. How did you generate your gradient?

Gradients are generated through the UI, he used CSS to create the linear gradient.

@petro Thanks for the quick reply. Where do I find the gradient generator in the UI? I had a look around and did not find it.

There is no such tool. It’ll be 100% custom.

This is a gradient slider that I use with the custom:slider-entity-row.

      - type: custom:slider-entity-row
        entity: input_number.quick_strip_hue
        full_row: true
        hide_state: true
        style:
          .: |
            ha-slider { 
              background: linear-gradient(to right, red 0%, #ff0 17%, lime 33%, cyan 50%, blue 66%, #f0f 83%, red 100%);
              border-radius: 10px;
            }

Keep in mind that this uses the custom card slider-entity-row and the custom card card-mod.

I also have automations that calculate the color value based on the sliders position. You’ll have to derive the equations by hand like zermo did.

Here is the finished product - still using my “Weather Temp Light” in my home office. It uses a TP-Link smart bulb.