# Excess Energy as a percentage for brightness value for smart light

Hey all, I have a smart light on the living room wall that I’ve configured in Home Assistant to turn on if there’s excess energy from my solar panels and turn off if there isn’t. I do this by using a simple “Excess Energy” helper that subtracts power consumption from generated power and supplies a figure in Watts.

I then use two simple “If it’s above zero turn the light on” and “If it’s below zero turn the light off” automations to turn the smart light on or off, and it’s mostly seamless.

I’d like to enhance this by setting the brightness of the smartlight to indicate how much excess power there is. What I’m trying to work out is how to convert the above graph to brightness values ranging from 0-100%. The values on the Excess Energy helper range from a negative number (which would have to be 0% brightness) to somewhere just shy of 6000W (anything 6000 or above would be 100%)

So, my question is how do I perform that conversion mathematically to convert a value between, say, -8000 and +8000, bounded by 0 and +6000, into a percentage between 0 and 100 ?

The easiest way is with this:

``````compensation:
power_brightness:
source: sensor.excess_energy
lower_limit: true
upper_limit: true
data_points:
- [0, 0]
- [6000, 100]
``````

Otherwise using a template sensor to do it all at once:

``````template:
- sensor:
- name: Power Brightness
state: "{{ ([0,(100*(states('sensor.solaredge_current_power')|float - states('sensor.solaredge_power_consumption')|float)/6000)|round(0)|int,100]|sort)[1] }}"
availability: "{{ has_value('sensor.solaredge_current_power') and has_value('sensor.solaredge_power_consumption') }}"``````
1 Like