Change LED colour based on sensor value

Hi Team
I have quite limited experience with home assistant.
I was wondering if anyone is aware of a custom integration which changes the colour of an led light strip based on a sensor value? Or if anyone much more intelligent than myself would be interest in creating?

I have my solar inverter connected to HA.
I also have an LED strip around my tv currently running Hyperion through a raspberry pi zero.

I would like 3 colours of varying intensity based on the value of import/export from the inverter.
Example

Red = import
White = power neutral
Green = export

Ideally the light would be reactive and the red colour would get darker the more energy is being imported and visa versa. Would obviously need to set the max import/export values.

Was hoping adaptive lighting could be altered to suit my use case but it doesn’t look like it.

Thanks in advance

Because you did not supply any entity ids I’m going to make some up, be sure to replace them with your own.

The easiest way to do this is in the HS colour space (hue & saturation). Set your hue (red or green) based on import or export. Set your saturation based on the % of your maximum power. I’m going to assume you have different maximum import and export powers.

I’m also going to assume you have one power sensor that is positive for import and negative for export.

trigger:
  - platform: state
    entity_id: sensor.power
    to:   # null 'to' triggers on any state change, ignores attribute changes
condition:
  - condition: state  # only if the light is on
    entity_id: light.tv_hyperion
    state: 'on'
action:
  - service: light.trun_on
    target:
      entity_id: light.tv_hyperion
    data:
      brightness: 255
      hs_color: >
        {% set pwr_max_import = 8000 %}  {# change this to your requirement #}
        {% set pwr_max_export = 3000 %}  {# change this to your requirement #}
        {% set hue = 0 if states('sensor.power')|float(0) >= 0 else 90 %} {# red for import, green for export #}
        {% set max_pwr = pwr_max_import if states('sensor.power')|float(0) >= 0 else pwr_max_export %}
        {% set sat = (100 * states('sensor.power')|float(0) / max_pwr)|int(0) %}
        {{ [hue,sat] }}
1 Like

Wow Tom thanks for doing that!

For reference if you are willing to help I have listed the entity names below. Turns out it’s actually 2 different sensors, one export/one import. If you aren’t too busy I would greatly appreciate if you could help me out with entity names? Otherwise I will have a try myself.

light.lounge_ambilight
sensor.active_import_from_grid
sensor.active_export_to_grid

If inverter is not exporting then value goes to 0w and stays in that position until exporting begins again and visa versa for import.

Max import would be 10kw each way.

Thanks again.

Having two sensors changes things a bit. Try this:

trigger:
  - platform: state
    entity_id: sensor.active_import_from_grid
    to:   # null 'to' triggers on any state change, ignores attribute changes
  - platform: state
    entity_id: sensor.active_export_to_grid
    to:   # null 'to' triggers on any state change, ignores attribute changes
condition:
  - condition: state  # only if the light is on
    entity_id: light.lounge_ambilight
    state: 'on'
  - condition: template # make sure the power sensors have numeric values
    value_template: "{{ has_value('sensor.active_import_from_grid') and has_value('sensor.active_export_to_grid') }}"
action:
  - service: light.turn_on
    target:
      entity_id: light.lounge_ambilight
    data:
      brightness: 255
      hs_color: >
        {% set pwr_max_import = 8000 %}  {# change this to your requirement #}
        {% set pwr_max_export = 3000 %}  {# change this to your requirement #}
        {% if states('sensor.active_export_to_grid')|float(0) > 0 %}
          {% set power = -1 * states('sensor.active_export_to_grid')|float(0) %}
        {% else %}
          {% set power = states('sensor.active_import_from_grid')|float(0) %}
        {% endif %}
        {% set hue = 0 if power >= 0 else 90 %} {# red for import, green for export #}
        {% set max_pwr = pwr_max_import if power >= 0 else pwr_max_export %}
        {% set sat = (100 * power|abs / max_pwr)|int(0) %}
        {{ [hue,sat] }}

Don’t forget to change the maximum power import and export settings.

You probably need another condition in there so that this does not mess up your ambilight if you are using it for a movie.

2 Likes

Hi Tom
This worked perfectly!
Thanks :pray:

1 Like

Thanks for this code and is it possible to do something similar please?

I would like in case of production the color varies from green, yellow, orange and red (from smallest to largest export value / 0 to 6000 kw / sensor.energy_exporting).

And when I’m importing, the LED strip (light.i_vitrine_salon) turns off (sensor.energy_importing).

Sorry, I’m still having a little trouble with yaml :smile:

Thanks in advance


Bonjour

Merci pour ce code et est-ce possible de faire quelque chose de similaire s’il vous plaît ?

J’aimerais qu’en cas de production la couleur varie de vert, jaune, orange et rouge (du plus petit à la plus grosse valeur d’exportation / 0 à 6000 kw / sensor.energy_exporting).

Et quand je suis en importation, que la strip led (light.i_vitrine_salon) s’éteins (sensor.energy_importing).

Désolé, j’ai encore un peu de mal avec yaml :smile:

D’avance merci