Show a calculated value based on a sensor value

I am very happy with HA in spite of my non-tech abilities (I’ve never touched a YAML file). Can anyone please help me with a template which converts solar lux from a sensor I have into kW solar energy produced from my roof panels, please? I simply don’t understand what to do to set up a template. The sensor value I have shows up in HA in lux, and I know exactly how this equates to my solar panel production: it corresponds to the formula y=-3.366E-7x-squared + 0.1023x + 3.8362, where y is the solar panel output in watts & x is the solar lux. If that’s too complicated, then I can approximate it to y=0.0696x+503.3. I’d be very grateful for some assistance on this, please, so I can estimate my solar production and show it in HA.

You can use this link to create a new template (choose “template a sensor” in the resulting popup).

The template would look something like

{{ 3.366e-7 ** 2 + 0.1023 * states('sensor.your_lux') | int + 3.8362 }}

and remember the template in Developer tools is your friend

great place to get your logic right

Many thanks, atiflyer - it sort of works, but the formula needs a sensor squared as well as a ‘+ sensor’ value (polynomial equation). I have updated the equation to show in kW rather than watts and it is
-3.438e - 10sensor squared +0.0001sensor +0.0246
I’ve tried with
{{ -3.438e-10 * states(‘sensor.gw2000a_solar_lux’) * states(‘sensor.gw2000a_solar_lux’) + 0.0001 * states(‘sensor.gw2000a_solar_lux’) | int + 0.0246 }}
but this is apparently incorrect.

You Kiwis are awesome but this template editor is at present beyond my understanding. I have no experience with coding :frowning:

Copy-paste the following into the Template Editor (Developer tools → Template) and confirm it reports the correct value.

{% set lux = states('sensor.gw2000a_solar_lux') | float(0) %}
{{ (-3.438e-10 * lux**2) + (0.0001 * lux) + 0.0246 }}

If it does, you can use it in your Template Sensor.

Amazing - thank you so much, it works! I really have appreciated the help I’ve received on this forum, I would never have been able to come up with a solution unaided. I don’t think my brain is wired for coding.

You’re welcome!

Before you go, please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which indicates to others that the topic has been solved. This helps other users find answers to similar questions.

I’ve tagged you with the solution :slight_smile: Thanks again!

Hi again, I’ve found a more reliable comparison to my solar production by using sensor.gw2000a_solar_radiation rather than sensor.gw2000a_solar_lux. My production in kW is simply the solar radiation value x 0.0097. I’ve tried configuring a template but I am doing something wrong, even with this simple formula. Can you please help? I thought {{ 0.0097 * states(‘sensor.gw2000a_solar_radiation’) }} would work - but it doesn’t.

{{ 0.0097 * states('sensor.gw2000a_solar_radiation') | float(0) }}

Also, I would appreciate it if you could mark my post above with the Solution tag. You marked your own post with the tag.

Thank you!

Many thanks - it works!.. but I don’t see any way of adding a “solved” tag to your original reply. As you have already gathered, I’m not too good at this computer stuff :frowning: