Change Light Color based on Outside Temperature

Tried it exactly as you have it listed and forced the trigger so I didn’t have to wait for the temp change. Light doesn’t come on and getting this in the log:

Log Details (ERROR)

Sun Dec 15 2019 10:14:52 GMT-0500 (Eastern Standard Time)

Error rendering data template: UndefinedError: ‘trigger’ is undefined

The error message you received is expected because you manually executed the automation. When you do that, the automation’s trigger and condition (if any) are ignored and only the action is evaluated. That means, once again, the Trigger State object is undefined.

From the documentation: Testing Your Automation

Please note that if you click on Trigger of an automation in the frontend, only the action part will be executed by Home Assistant. That means you can’t test your trigger or condition part that way. It also means that if your automation uses some data from triggers, it won’t work properly as well just because trigger is not defined in this scenario.

To trigger the automation, you will have to cause sensor.outdoor_temperature to change its state. Rather than haul the sensor indoors, you can use the States page to set its state.

Tried again - forced the temp change in the states page for the entity. Light doesn’t come on and getting this error:

Error while executing automation automation.outside_temp_color_change. Invalid data for call_service at pos 1: None for dictionary value @ data[‘rgb_color’]

When you stated

What were the test conditions that produced no errors?

The error in the log didn’t show up until I forced the state of the sensor. The condition with no error was because I never trigger the temp change.

Looks like the end of the road for using the from_json filter for rgb_color. :man_shrugging:

If you search for posts containing rgb_color, you’ll find may threads encountering the same issue:
rgb_color expects to receive a list. Attempts to use templates to create a list typically fail because Jinja2 always outputs a string. Even though the template’s output looks like a list, it’s still a string (and unacceptable to rgb_color).

I thought using from_json might work, but nope.

The standard way to mitigate it is to ensure the list is hard-coded in YAML and only its values are templated. There are two ways to do that and your latest example is one of them.

    data_template:
      rgb_color:
        - {{ template for RED }}
        - {{ template for BLUE }}
        - {{ template for GREEN}}

and the other is:

    data_template:
      rgb_color: [ {{ template for RED }}, {{ template for BLUE }}, {{ template for GREEN}} ]
1 Like

Finally finished the “Weather Outdoor Temperature Light - Automation”. The complete rgb color spectrum for the temperatures (in degrees F) are here:

https://www.strangeplanet.fr/work/gradient-generator/?c=100:FFFFFF:FF1493:9D00FF:0000FF:00FFEE:32D22D:8CFF00:FFFF00:FFAE00:FF2B00:FF0000

Converted the HTML colors to rgb_colors here: https://www.w3schools.com/colors/colors_hwb.asp

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

Good lord. My phone does not like that code block lol.

2 Likes

FWIW, there are alternative ways to do this using mathematics (the Internet abounds with color fading and color cycling examples, often employed in light-strip controllers). However, seeing that you’ve already hard-coded each degree to a color, the alternatives are left for others to explore.

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:)