Calculate average cloud coverage in template

Hi
I am trying to calculate average cloud coverage with template for next hours but something must be wrong. This is clearly not my expertise. I don’t have any errors except the result is always “unknown”

- trigger:
    - platform: time_pattern
      hours: /1  # This triggers the automation at the start of every hour
  action:
    - service: weather.get_forecasts
      target:
        entity_id: weather.openweathermap
      data:
        type: hourly
  sensor:
    - name: "Average Cloud Cover Next 8 Hours"
      icon: mdi:weather-cloudy-clock
      unique_id: 6b3b12e1-4694-421c-a09e-e1c86f2c5afd
      unit_of_measurement: '%'
      state: >
        {% set hours = 8 %}
        {% set cloud_covers = [] %}
        {% for i in range(hours) %}
          {% set forecast = state_attr('weather.openweathermap', 'forecast')[i] %}
          {% if forecast %}
            {% set clouds = forecast.cloud_coverage | default(0) %}
            {% set cloud_covers = cloud_covers + [clouds] %}
          {% else %}
            {% set cloud_covers = cloud_covers + [0] %}
          {% endif %}
        {% endfor %}
        {{ (cloud_covers | sum / hours) | round(2) }}
      attributes:
        friendly_name: "Average Cloud Coverage Next 8 Hours"

Go into developer tools / templates and test it there piece by piece until you find out what is wrong.

That’s a bit more difficult in this case, as you need the service response

@catdogmaus you can run the service call in developer tools > services and then convert the service response shown there using a website like this.

Then use that in developer tools > templates

However, your main issue is that you didn’t provide the response_variable parameter in your service call, so your service response data is not available in the sensor part.

Besides that, there is no need for a for loop to get the desired result

- trigger:
    - platform: time_pattern
      hours: /1  # This triggers the automation at the start of every hour
  action:
    - service: weather.get_forecasts
      target:
        entity_id: weather.openweathermap
      data:
        type: hourly
      response_variable: forecast
  sensor:
    - name: "Average Cloud Cover Next 8 Hours"
      icon: mdi:weather-cloudy-clock
      unique_id: 6b3b12e1-4694-421c-a09e-e1c86f2c5afd
      unit_of_measurement: '%'
      state: >
        {{
          forecast['weather.openweathermap'].forecast[:7]
            | map(attribute='cloud_coverage')
            | list
            | average 
        }}

In my version with response_variable I still get “None has no element 7”
As I understand this means that state_attr('weather.openweathermap', 'forecast') is returning None What I don’t understand is why.

yours results “forecast is undefined”

What version?

Use exactly what TheFes gave you.

The forecast attribute doesn’t exist anymore, hence the service call (get_forecasts).

UndefinedError: ‘forecast’ is undefined

Are you now trying the template in developer tools?

Where are you getting this from? It’s not in the solution you were given.

Exactly __

Solution I was given gives forecast’ is undefined error in developer tools.
Solution that I myself try to resolve gives just zeros and also None has no element 7 error in developer tools

You cannot test this in dev tools, because service calls aren’t supported in there.

I need a crystal ball to understand that.

The solution I gave you gives forecast is undefined because that it the service response coming from the action section.

If you want to test it in developer tools > templates, you need to:

  1. Go to developer tools > services
  2. set it to YAML mode
  3. paste the following service call
service: weather.get_forecasts
target:
  entity_id: weather.openweathermap
data:
  type: hourly
response_variable: forecast
  1. run the service call
  2. paste the output from the service response in the following website https://onlineyamltools.com/convert-yaml-to-json
  3. copy the Json output
  4. put the following at the top of developer tools > templates (replace <paste Json output> including the pointy brackets with the actual Json output you copied in step 6)
{% set forecast = 
<paste Json output>
%}
  1. scroll down and test your template
2 Likes

Stupid me, I had the silly idea that templet editor is actually meant to test the templet.
By the way, website you referred to does not work. I never got anything in json side.

So I just saved templet with studio code and finally got some results. :slightly_smiling_face: :thinking:

It does. It’s just that with the deprecation of the forecast attribute there isn’t a way to make service calls in the template editor. It’s the same for simulating automation triggers: You need to create a variable with the data.

2 Likes