Use RGB light to show energy usage

Created an automation to look at solar production and house consumption and use this to create a smooth gradient going from green to purple:
[0, 255, 0], # Green
[255, 255, 0], # Yellow
[255, 165, 0], # Orange
[255, 0, 0], # Red
[128, 0, 128] # Purple

I’ll post the yaml from the automation below, feel free to adapt or turn into a blueprint:

alias: moodlight-smooth
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.solar_power
  - platform: state
    entity_id:
      - sensor.house_consumption
condition:
  - condition: device
    type: is_on
    device_id: b047855b83e3f24f3705a659b20f496f
    entity_id: light.calex_moodlight_5_8w_rgb_cct
    domain: light
action:
  - service: light.turn_on
    target:
      entity_id: light.calex_moodlight_5_8w_rgb_cct
    data:
      rgb_color: >
        {% set colors = [
          [0, 255, 0],
          [255, 255, 0],
          [255, 165, 0],
          [255, 0, 0],
          [128, 0, 128]
        ] %} {% set min_val = -3000 %} {% set max_val = 2500 %} {% set house =
        states("sensor.house_consumption") | float %} {% set solar =
        states("sensor.solar_power") | float %} {% set value = house - solar %}
        {% set normalized = (value - min_val) / (max_val - min_val) %} {% set
        num_colors = colors | length %} {% set segment = (num_colors - 1) *
        normalized %} {% set index = segment | int %} {% set weight = segment -
        index %}

        {% if index < num_colors - 1 %}
          {% set r = (colors[index][0] * (1 - weight) + colors[index + 1][0] * weight) | int %}
          {% set g = (colors[index][1] * (1 - weight) + colors[index + 1][1] * weight) | int %}
          {% set b = (colors[index][2] * (1 - weight) + colors[index + 1][2] * weight) | int %}
        {% else %}
          {% set r, g, b = colors[-1] %}
        {% endif %}

        [{{ r }}, {{ g }}, {{ b }}]
mode: single

Make adaptions to the jinja template to use your own sensors and lower and upper limits. Play around with the template in your dev tools to get it right for your own setup:

{% set colors = [ [0, 255, 0], [255, 255, 0], [255, 165, 0], [255, 0, 0], [128, 0, 128]      ] %} 
{% set min_val = -3000 %} **#changethis to max solar**
{% set max_val = 2500 %}  **#changethis to value from where you want to see purple**
{% set house =  states("sensor.house_consumption") | float %} **#change this to your actual sensor**
{% set solar =  states("sensor.solar_power") | float %} {% set value = house - solar %} **#change this to your actual sensor**
{% set normalized = (value - min_val) / (max_val - min_val) %} 
{% set  num_colors = colors | length %} 
{% set segment = (num_colors - 1) *  normalized %} {% set index = segment | int %}
{% set weight = segment -  index %}
{% if index < num_colors - 1 %}
      {% set r = (colors[index][0] * (1 - weight) + colors[index + 1][0] * weight) | int %}
      {% set g = (colors[index][1] * (1 - weight) + colors[index + 1][1] * weight) | int %}
      {% set b = (colors[index][2] * (1 - weight) + colors[index + 1][2] * weight) | int %}
{% else %}
       {% set r, g, b = colors[-1] %}
{% endif %}
        [{{ r }}, {{ g }}, {{ b }}]
5 Likes

Thank for you for sharing, works great.

1 Like

I tried turning it into a blueprint with selectable color gradient

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Not fully working yet as I am yet to find a way to use the selected enitities in the data template section of the automation. Any suggestions from blueprint masters more than welcome

1 Like

got the blueprint working now. Also posted it on blueprint exchange in case anyone finds it usefull…

1 Like