Sum values in array

I want to know the Solar Forecast between midnight and midday adding up the hourly values from the Solcast Forecast which are in the entity sensor.solcast_pv_forecast_forecast_today state attributes e.g.

detailedHourly:
  - period_start: '2023-10-26T00:00:00+01:00'
    pv_estimate: 0
    pv_estimate10: 0
    pv_estimate90: 0
  - period_start: '2023-10-26T01:00:00+01:00'
    pv_estimate: 0
    pv_estimate10: 0
    pv_estimate90: 0

I can pick out individual values for example the 12th hour thus:

{{ "%.3f"|format(state_attr('sensor.solcast_pv_forecast_forecast_today','detailedHourly')[12]['pv_estimate']|float)}}

However when I try to sum these up in a for loop, the values don’t add. My initial attempt using “+=” convention was completely rejected in the template editor

{%- set forecast_to_12_today = 0|float %}
{%- for values in [0,1,2,3,4,5,6,7,8,9,10,11] %}
{%- set forecast_to_12_today += "%.3f"|format(state_attr('sensor.solcast_pv_forecast_forecast_today','detailedHourly')[values|int]['pv_estimate'])|float %}
{{ forecast_to_12_today}}
{%- endfor %}

and even when I went to brute force the value resets each loop instead of incrementing.

{%- set forecast_to_12_today = 0|float %}
{%- for values in [0,1,2,3,4,5,6,7,8,9,10,11] %}
{%- set forecast_to_12_today = forecast_to_12_today + "%.3f"|format(state_attr('sensor.solcast_pv_forecast_forecast_today','detailedHourly')[values|int]['pv_estimate'])|float %}
{{ forecast_to_12_today}}
{%- endfor %}

Any help appreciated. TIA.

Copy-paste the following template into the Template Editor and confirm it reports the correct value:

{{ state_attr('sensor.solcast_pv_forecast_forecast_today', 'detailedHourly')
  | map(attribute='pv_estimate') | sum | round(3) }}

The template assumes pv_estimate contains an int or float value. If it’s actually a numeric string then the template will need an additional filter (to convert the strings to float).


NOTE

Regarding your for-loop example, changes to a variable inside a for-loop are not preserved outside of the for-loop unless the variable employs namespace.

1 Like

Thanks. The Template Editor reported OK. Your tip about namespace worked. Here is the final code:

{% set forecast_to_12_today = namespace(y=0) %}
{% for values in [6,7,8,9,10,11] %}
{%- set forecast_to_12_today.y = forecast_to_12_today.y + "%.3f"|format(state_attr('sensor.solcast_pv_forecast_forecast_today','detailedHourly')[values|int]['pv_estimate'])|float %}
{%- endfor %}
{{ forecast_to_12_today.y}}

Glad to hear it reported the correct value.

That’s still an unnecessarily long way to compute the sum of the last 6 items in the list. Here’s what I had suggested previously but constrained to sum only the last six pv_estimate values.

{{ state_attr('sensor.solcast_pv_forecast_forecast_today', 'detailedHourly')[6:]
  | map(attribute='pv_estimate') | sum | round(3) }}

Thanks again. I get it now.
I actually want to sum up the values of solar PV generation in the morning.
There are 24 hourly values in the forecast: 0 through to 23. with 0 being the forecast from 00:00 to 01:00 and the values from 00:00 to 05:00 generally being zero. So in my long-hand version I just added values 6 to 11.
Using your approach I don’t need to worry about values 0 to 5 so this gets the same answer.

{{ state_attr('sensor.solcast_pv_forecast_forecast_tomorrow', 'detailedHourly')[:12]
| map(attribute='pv_estimate') | sum | round(3) }}

Glad to hear my suggested template solved the problem.

Please consider marking my post above with the Solution tag. It will automatically place a check mark next to the topic’s title. This helps other users find answers to similar questions.