Cumulative information during the day to make a conclusion at the end of the day

Hi,

I’m trying to make a rather simple automation:

  1. 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.
  2. 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:

  1. Defines variable sun_duration and set it to 0.
  2. 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.
  3. I would repeat this, and then wait to a specific hour and operate the heater
  4. Wait for a calculated time (using sun_duration) and then turn the heater off

I’m failing in the simple variable addition:

  - variables:
      sun_duration: "{{ sun_duration  + 100 - sensor.openweathermap_cloud_coverage}}"

Seemingly “sensor” is unknown by HA… How to use that?

Is variable the right tool? I read an explanation about its scope, and I’m not sure it’s usable for my needs.

Appreciate any help here.

It should be

states('sensor.openweathermap_cloud_coverage')

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:

sun_duration: "{{ sun_duration  + 100 - states('sensor.openweathermap_cloud_coverage') | int(0) }}"
1 Like

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).

1 Like

Thanks a lot, now it’s being accepted.

Wow, thanks for the handful of advice.

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)

Glad I could help!

1 Like