In the process of learning Home Assistant.
I would like to set a color of an RGB light based on a temperature sensor.
For now, just trying to control the amount of red.
Using the HSB (hue/saturation/brightness) colour space makes it easy to map a temperature range to a hue, keeping the saturation and brightness constant. e.g. starting at a hue of 180° on the wheel and heading up to 360° with an equation if you don’t mind cyan = cold, red = hot and blue/purple representing a middle temperature.
Or going the other way round the hue wheel, blue, cyan, green, yellow, orange, red (cold → hot), which I find better.
e.g. this maps 16°C → 24°C to blue, cyan, green, yellow, orange, red and clamps temperatures below and above that range to blue or red respectively. Purple = faulty sensor.
I attempted to write a script using your example but it keeps on throwing error messages when loading the YAML configuration. I made the following script in the scripts.yaml file that i call using an automation (which is triggered by a temperature change of mytempsensor). I use a paulmann bulb: Paulmann 291.52 control via MQTT | Zigbee2MQTT which specifies (as far as i can tell) using “color” and then specifying hue and saturation instead of “color_hs”.
temp_to_hue:
alias: "Adjust Light Based on Temperature"
trigger:
- platform: state
entity_id: sensor.mytempsensor
condition: []
action:
- service: light.turn_on
target:
entity_id: light.mylight
data:
brightness: 254
color: >
{% set temp = states('sensor.mytempsensor') | float(default=1000) %}
{% if temp < 1000 %}
{% set h = -30 * temp + 720 %}
{% set hlim = ([0,h,240]|sort)[1]|int %}
["hue":"{{ hlim }}","saturation":"100"]
{% else %}
["hue":"300","saturation":"100"]
{% endif %}
mode: single
I notice there is a trigger in the script as well, is that necessary as i already use the same trigger (temp change) in the automation?