Average temperature calculation based on the forecast

Hello everyone,
I’m trying to create an automation in which the forecast for the temperature of the next 12 hours is read from met.no. An average should be formed from these values. If this value is above a value x (helper), a switch should be flipped.

The background is that I want to use either with the radiator or with the air conditioning. This should be determined twice a day based on the outside temperature.

I’m fairly new in yaml and can’t get any further.

In my search I came across:

{{ (state_attr('weather.home', 'forecast')|selectattr('datetime', 'eq', (today_at('7:00').astimezone(utcnow().tzinfo) + timedelta (days=1)).isoformat())|list|first).temperature }}

Thanks in advance.

Hi there,

This is similar to what I’ve been looking for as well - I want to calculate the average temp forecast for the next 3 hours. I’ve figured out the following, although it’s a little clunky. I haven’t worked it into anything useful yet. So far I’ve just been playing around with a script that creates a persistent notification so I can see the output immediately.
I’m not a templating wizard, so there’s likely a better way to do this but for what it’s worth…

I called the weather.get_forecasts service for the ‘weather.church_st’ entity, forecast type of Hourly and the response variable ‘forecast_var’

Then I used the following template in the persistent notification action:

{{ average(forecast_var['weather.church_st'].forecast[0].temperature, 
    forecast_var['weather.church_st'].forecast[1].temperature, 
    forecast_var['weather.church_st'].forecast[2].temperature) }}

The ‘forecast[0]’, ‘forecast[1]’ and ‘forecast[2]’ looks at the first 3 hours (indexed starting at 0) and averages those temperatures.

Hope this helps!
Charlene

{{ forecast_var['weather.church_st'].forecast[:3]
  | map(attribute='temperature') | average | round(1) }}

If you want, say, 12 hours instead of the first 3 hours of temperature data, change [:3] to [:12] in the template.

2 Likes