Calculate average of attribute values of a sensor

I have a sensor witch retrieves a list of values, stored in the attribute ‘items’ of the sensor:

items: 
- close: 22.003
  onexchtradevolumeeex: 4635192
  tradedatetimegmt: 11/1/2022 12:00:00 PM
- close: 48.824
  onexchtradevolumeeex: 5513904
  tradedatetimegmt: 11/2/2022 12:00:00 PM
- close: 67.881
  onexchtradevolumeeex: 4505784
  tradedatetimegmt: 11/3/2022 12:00:00 PM
- close: 56.635
  onexchtradevolumeeex: 4210128
  tradedatetimegmt: 11/4/2022 12:00:00 PM
- close: 63.169
  onexchtradevolumeeex: 4400400
  tradedatetimegmt: 11/7/2022 12:00:00 PM
- close: 89.805
  onexchtradevolumeeex: 4716624
  tradedatetimegmt: 11/8/2022 12:00:00 PM
- close: 87.229
  onexchtradevolumeeex: 4696248
  tradedatetimegmt: 11/9/2022 12:00:00 PM
- close: 74.787
  onexchtradevolumeeex: 4548360
  tradedatetimegmt: 11/10/2022 12:00:00 PM
- close: 67.287
  onexchtradevolumeeex: 4065336
  tradedatetimegmt: 11/11/2022 12:00:00 PM
- close: 107.908
  onexchtradevolumeeex: 4216032
  tradedatetimegmt: 11/14/2022 12:00:00 PM
- close: 115.537
  onexchtradevolumeeex: 4705080
  tradedatetimegmt: 11/15/2022 12:00:00 PM
- close: 100.282
  onexchtradevolumeeex: 3668712
  tradedatetimegmt: 11/16/2022 12:00:00 PM
- close: 106.792
  onexchtradevolumeeex: 3949200
  tradedatetimegmt: 11/17/2022 12:00:00 PM
- close: 109.878
  onexchtradevolumeeex: 3442416
  tradedatetimegmt: 11/18/2022 12:00:00 PM
- close: 112.064
  onexchtradevolumeeex: 4087272
  tradedatetimegmt: 11/21/2022 12:00:00 PM
- close: 116.692
  onexchtradevolumeeex: 3816576
  tradedatetimegmt: 11/22/2022 12:00:00 PM
- close: 126.385
  onexchtradevolumeeex: 4285560
  tradedatetimegmt: 11/23/2022 12:00:00 PM
- close: 118.847
  onexchtradevolumeeex: 3800760
  tradedatetimegmt: 11/24/2022 12:00:00 PM
- close: 125.577
  onexchtradevolumeeex: 3215904
  tradedatetimegmt: 11/25/2022 12:00:00 PM
- close: 124.582
  onexchtradevolumeeex: 4498776
  tradedatetimegmt: 11/28/2022 12:00:00 PM
- close: 133.091
  onexchtradevolumeeex: 4168440
  tradedatetimegmt: 11/29/2022 12:00:00 PM
- close: 136.09
  onexchtradevolumeeex: 5255160
  tradedatetimegmt: 11/30/2022 12:00:00 PM
- close: 136.09
  onexchtradevolumeeex: 4462920
  tradedatetimegmt: 12/1/2022 12:00:00 PM
- close: 133.423
  onexchtradevolumeeex: 4098096
  tradedatetimegmt: 12/2/2022 12:00:00 PM
- close: 135.082
  onexchtradevolumeeex: 4386096
  tradedatetimegmt: 12/5/2022 12:00:00 PM
- close: 139.678
  onexchtradevolumeeex: 4609056
  tradedatetimegmt: 12/6/2022 12:00:00 PM
- close: 148.715
  onexchtradevolumeeex: 4323672
  tradedatetimegmt: 12/7/2022 12:00:00 PM
- close: 137.034
  onexchtradevolumeeex: 4250136
  tradedatetimegmt: 12/8/2022 12:00:00 PM
- close: 137.637
  onexchtradevolumeeex: 3944640
  tradedatetimegmt: 12/9/2022 12:00:00 PM
- close: 138.043
  onexchtradevolumeeex: 4295928
  tradedatetimegmt: 12/12/2022 12:00:00 PM
- close: 138.043
  onexchtradevolumeeex: 4493736
  tradedatetimegmt: 12/13/2022 12:00:00 PM
- close: 132.424
  onexchtradevolumeeex: 3990504
  tradedatetimegmt: 12/14/2022 12:00:00 PM
- close: 134.633
  onexchtradevolumeeex: 4173696
  tradedatetimegmt: 12/15/2022 12:00:00 PM
- close: 106.82
  onexchtradevolumeeex: 4086792
  tradedatetimegmt: 12/16/2022 12:00:00 PM
- close: 104.921
  onexchtradevolumeeex: 4404744
  tradedatetimegmt: 12/19/2022 12:00:00 PM
- close: 104.921
  onexchtradevolumeeex: 4623120
  tradedatetimegmt: 12/20/2022 12:00:00 PM
- close: 88.934
  onexchtradevolumeeex: 3850944
  tradedatetimegmt: 12/21/2022 12:00:00 PM
- close: 88.934
  onexchtradevolumeeex: 4481184
  tradedatetimegmt: 12/22/2022 12:00:00 PM
- close: 79.81
  onexchtradevolumeeex: 3284040
  tradedatetimegmt: 12/23/2022 12:00:00 PM
- close: 79.81
  onexchtradevolumeeex: 7920
  tradedatetimegmt: 12/26/2022 12:00:00 PM
- close: 79.81
  onexchtradevolumeeex: 748656
  tradedatetimegmt: 12/27/2022 12:00:00 PM
- close: 79.412
  onexchtradevolumeeex: 3848256
  tradedatetimegmt: 12/28/2022 12:00:00 PM
- close: 79.412
  onexchtradevolumeeex: 4049136
  tradedatetimegmt: 12/29/2022 12:00:00 PM
- close: 66.651
  onexchtradevolumeeex: 2587608
  tradedatetimegmt: 12/30/2022 12:00:00 PM
- close: 68.297
  onexchtradevolumeeex: 602160
  tradedatetimegmt: 1/2/2023 12:00:00 PM

Now I want to calculate the average of the item ‘close’ in that list.
I already am able to iterate trough the list, but calculating the average of ‘close’ for all the items I’m stuck at.

{% set items = state_attr('sensor.spot_ttf', 'items') %}
{% for i in range(0, items | count, 1) %}
  {{ state_attr('sensor.spot_ttf', 'items')[i].close }}
{% endfor %}

Help would be appreciated.

{{ state_attr('sensor.spot_ttf', 'items') | map(attribute='close') | average }}

Tnx 123 for the quick (and easy when you know where to look :slight_smile: ) solution.

1 Like