Collect the sun rate throughout the sunny hours, by using cloud coverage that is sampled e.g. once hour; e.g, sun_duration = sun_duration + 100 - cloud_coveratge.
Use this cumulative information to decide how long should I heat the water at the evening.
I’m failing in the very basics.
I created a script that:
Defines variable sun_duration and set it to 0.
Waits for a specific hour (this is just a test script to make it work), and at this hour add the current data to sun_duration.
I would repeat this, and then wait to a specific hour and operate the heater
Wait for a calculated time (using sun_duration) and then turn the heater off
However, since all states are strings, you would also need to convert it to a number using “int” or “float” (not sure if your values have decimals). You should also provide a default value in case the sensor is unavailable by passing the default to int/float. So:
Your variable won’t persist after the completion of your script.
It sounds like you’re attempting to do a poor man’s integral; why not just create a sensor to do an actual Riemann sum integral? In the logic you proposed, if the cloud cover changed at 12:59pm, when the scripts runs one minute later it will assume the new cloud cover number has been constant for the previous hour. The Riemann sum integral, on the other hand, will update upon state changes as opposed to time intervals. However you may need an intermediate “sun percentage” template sensor which is simply {{ 100 - states('sensor.openweathermap_cloud_coverage') | float(0) }}
All that being said, an easier route might be to use an integration developed for solar panel users (Forecast.Solar) and just set it up like you actually have a solar panel installation. The key sensor you’d be interested in is Estimated Energy Production - Today (in kWh).
Though the Reimann sum integral sounds compelling, the Forecast.Solar option sounds like a perfect option.
I managed to create an automation that takes the expected power, and feed it into a linear equation that decides on the boiler operation time. Now only need to tweak a bit with the parameters (condition, and slope).
Thanks a lot!
(waited before I comment back to you, as I wanted to see if I manage to create the automation or I need more help)