Bulb Colour based on Range of Input Number

Hey All:

I’ve finally built an automation that checks Yahoo Weather for today’s high, compares it to tomorrow’s forecast high, and spits out the difference as an input_number (var_weather_difference). e.g. today it was 12C, tomorrow it’ll be 16C, so my input_number.var_weather_difference = 4. The min/max for input_number.var_weather_difference is between -10 and 10.

What I would like is for HA to convert that location in the range of input_number.var_weather_difference to mireds in the range of 153 and 500. So, if input_number.var_weather_difference is 0, I want to set a certain light to be at 326.5 mireds. Then, for each degree up or down, add or subtract 17.35 mireds until the light is either at 500 or 153, depending on which way we’ve gone. Yes, this light will be very orange if today is > 10C warmer than yesterday - that’s the point.

I’m still learning templating; can anyone point me in the right direction for a template to do this with?

Edit: I’ve gotten close… I think… but something’s not working right about this:

- id: ruleFrontLightsOninMorning
  alias: Turn on Front Lights in Morning
  - platform: time
    at: '06:30:00'
  - condition: sun
    before: sunrise
  - condition: state
    entity_id: input_select.family_status
    state: 'Home'
  - service: light.turn_on
      entity_id: light.front_entrance
      brightness_pct: 25
      color_temp: >
        {{((states('input_number.var_weather_difference')|int) * 17.35) + 326.5}}

Second Edit: Hey Genius - integers are needed. If someone stumbles on this, the correct color_temp template is:
{{(((states('input_number.var_weather_difference')|int) * 17.35) + 326.5)|int}}

Looks good. Only change I’d make is a few spaces and I’d remove unnecessary (). Either way, it has no bearing on the automation at hand, would just be easier to read in the future:

{{ (states('input_number.var_weather_difference') | int * 17.35 + 326.5) | int }}

Fair enough. I’m so bad at this that I abuse parentheses to make sure that it’s respecting the order of operations (or that I don’t have to try to remember junior high mathematics). Thanks for the help.

1 Like