Writing here for the sake of others and for my own future reference.
My weather sensor has these state attributes (example):
temperature: 16.4
dew_point: 13.5
temperature_unit: °C
humidity: 83
cloud_coverage: 68.7 # <----- Notice this attribute
uv_index: 2.1
pressure: 1020.2
pressure_unit: hPa
wind_bearing: 303.6
wind_speed: 20.5
wind_speed_unit: km/h
visibility_unit: km
precipitation_unit: mm
attribution: >-
Weather forecast from met.no, delivered by the Norwegian Meteorological
Institute.
friendly_name: Forecast Home
supported_features: 3
Using this cloud_coverage
attribute I was able to
- Define Template Sensor that extracts the
cloud_coverage
attribute from weather.forecast_home
and makes it a sensor with a percentage value:
sensor:
# used to track the entity attribute cloud coverage of weather forecast
- platform: template
sensors:
cloud_coverage_percentage:
friendly_name: "Cloud Coverage Percentage"
unit_of_measurement: "%"
value_template: "{{ state_attr('weather.forecast_home', 'cloud_coverage') }}"
device_class: humidity # Although it's not humidity, this provides a % unit
- Statistics Sensor that calculates the average of the sensor I’ve just defined over a sliding 5-hour window. When checked, it gives you an average of last 5 hours:
- platform: statistics
unique_id: average_cloud_coverage_today_stats
name: Average Cloudy Percentage Today
entity_id: sensor.cloud_coverage_percentage
state_characteristic: mean # Calculate the average (mean)
sampling_size: 500 # Optional: Adjust sampling size if needed
max_age: 05:00:00 # Duration of 5 hours (10am to 3pm)
Now, the problem is that I only want to (possibly) turn on the water heater in the evening, so if I sample that statistics sensor at that time, it will give me the last 5 hours which include times that are out of my 10am-3pm time window. Therefor, I needed to sample the sensor at 3pm and store the value somewhere.
- Input Number to stores the average cloud coverage percentage captured at 3pm each day, used in the water heater automation condition:
input_number:
cloud_coverage_3pm_average_value:
name: "Cloud Coverage Average"
unit_of_measurement: "%"
min: 0
max: 100
step: 1
mode: box # or slider, or auto
So basically, I expose the cloud_coverage
from the weather entity, I sample it using statistics sensor for the last 5 hours. At 3pm, when the sun no longer shine on my water heater panels I have an automation set up to take the current value from the statistics sensor and store it in an input number.
That number could, for example, be equal to 54 which means that during the last 5 hours, there was an average of 54% cloud coverage.
Once I have the input number stored daily which tells me how much cloud coverage was during the sun hours, I can easily define an automation to turn on the water heater if cloud coverage was > 40% for example.