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