Change light color based on temperature

I have seen examples online to change a hue or brightness based on the temperature outside, but I am trying to do something different. This automation below works until I have a power loss. I can recognize the power loss in my state of the temperature reading, but then I can only change the color to blue to recognize the power loss. Is there are an easier way to do this with a value template sensor so that I can right an easier code to change the light color based on temperature rather than using triggers for the automation?

See my automation below:

alias: Fountain Box LED Strip Temp ▲
description: Change the light in the box when sensor read an unsafe temp
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.fountain_temp
    id: warninglow
    above: 66
    below: 67.9
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - platform: numeric_state
    entity_id:
      - sensor.fountain_temp
    id: dangerhigh
    above: 84.01
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - platform: numeric_state
    entity_id:
      - sensor.fountain_temp
    id: dangerlow
    below: 65.99
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - platform: numeric_state
    entity_id:
      - sensor.fountain_temp
    above: 68
    below: 82
    id: goodtemp
  - platform: numeric_state
    entity_id:
      - sensor.fountain_temp
    id: warning high
    above: 82.01
    below: 84
  - platform: state
    entity_id:
      - sensor.fountain_temp
    from: unavailable
    to: null
    for:
      hours: 0
      minutes: 5
      seconds: 0
    id: Offline to Active
condition:
  - condition: state
    entity_id: input_boolean.automations_kill_switch
    state: "off"
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - warninglow
              - warning high
        sequence:
          - service: light.turn_on
            data:
              color_name: yellow
              brightness_pct: 50
            target:
              entity_id: light.fountain_box_light
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id: dangerhigh
              - condition: trigger
                id: dangerlow
        sequence:
          - service: light.turn_on
            data:
              color_name: red
              brightness_pct: 50
            target:
              entity_id: light.fountain_box_light
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id: goodtemp
        sequence:
          - service: light.turn_on
            data:
              color_name: green
              brightness: 125
      - conditions:
          - condition: trigger
            id:
              - Offline to Active
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              color_name: blue
              brightness_pct: 50
            target:
              entity_id: light.fountain_box_light
    default:
      - service: light.turn_on
        data:
          color_name: green
          brightness_pct: 50
        target:
          entity_id: light.fountain_box_light
mode: single

Assuming when there is an outage your home assistant server loses power as well:

alias: Fountain Box LED Strip Temp ▲
description: Change the light in the box when sensor read an unsafe temp
trigger:
  - platform: state
    entity_id:
      - sensor.fountain_temp
    not_to:
      - unavailable
      - unknown
  - platform: homeassistant
    event: start
condition:
  - condition: state
    entity_id: input_boolean.automations_kill_switch
    state: "off"
  - condition: not
    conditions:
      - condition: state
        entity_id: sensor.fountain_temp
        state: 
          - 'unavailable'
          - 'unknown'
action:
  - service: light.turn_on
    metadata: {}
    data:
      color_name: >
        {% set temp = states('sensor.fountain_temp')|float(0) %}
        {% if temp < 66 %}}
          red
        {% elif 66 <= temp < 68 %}
          yellow
        {% elif 68 <= temp < 82 %}
          green
        {% elif 82 <= temp < 84  %}
          yellow
        {% elif temp > 84 %}
          red
        {% endif %}
      brightness_pct: >
        {{ 125 if 68 <= states('sensor.fountain_temp')|float(0) < 82 else 50 }}
    target:
      entity_id: light.fountain_box_light
mode: single

If your home assistant server is on a UPS replace the homeassistant start trigger with a state trigger that detects when power is returned.

2 Likes

Thanks, that works much better. I knew there was an easier way!

1 Like