Sum of json attributes

hi there,

I collect some data with rest.
the output is:

{
  "System": "4654546",
  "data": [
    {
      "logDateTime": "2023-04-20T18:15:00+02:00",
      "logDuration": 900,
      "channels": [
        {
          "channelName": "EnergyExpected",
          "channelType": "Energy",
          "value": 274.53,
          "unit": "Wh"
        }
      ]
    },
    {
      "logDateTime": "2023-04-20T18:30:00+02:00",
      "logDuration": 900,
      "channels": [
        {
          "channelName": "EnergyExpected",
          "channelType": "Energy",
          "value": 266.1325,
          "unit": "Wh"
        }
      ]
    },
    {
      "logDateTime": "2023-04-20T18:45:00+02:00",
      "logDuration": 900,
      "channels": [
        {
          "channelName": "EnergyExpected",
          "channelType": "Energy",
          "value": 250.365,
          "unit": "Wh"
        }
      ]
    },
    {
      "logDateTime": "2023-04-20T19:00:00+02:00",
      "logDuration": 900,
      "channels": [
        {
          "channelName": "EnergyExpected",
          "channelType": "Energy",
          "value": 228.1025,
          "unit": "Wh"
        }
      ]
    },
    {
      "logDateTime": "2023-04-20T19:15:00+02:00",
      "logDuration": 900,
      "channels": [
        {
          "channelName": "EnergyExpected",
          "channelType": "Energy",
          "value": 184.6,
          "unit": "Wh"
        }
      ]
    },
    {
      "logDateTime": "2023-04-20T19:30:00+02:00",
      "logDuration": 900,
      "channels": [
        {
          "channelName": "EnergyExpected",
          "channelType": "Energy",
          "value": 135.375,
          "unit": "Wh"
        }
      ]
    },
    {
      "logDateTime": "2023-04-20T19:45:00+02:00",
      "logDuration": 900,
      "channels": [
        {
          "channelName": "EnergyExpected",
          "channelType": "Energy",
          "value": 85.531,
          "unit": "Wh"
        }
      ]
    }
  ]
}

I want to totalize all values of the attribute “value”
Can someone tell me how to do this?

Thanks!

{{ value_json['data']
   |map(attribute='channels')
   |map(attribute=0)
   |sum(attribute='value')
   |round(4) }}

Assumes that each channels list only contains a single item.

Alternatively:

{{ value|string|regex_findall("value.:\ (-?[\d\.]+)")|map('float')|sum|round(4) }}

you don’t need the double maps

{{ value_json.data | sum(attribute='channels.0.value') | round(4) }}
1 Like

Since this solution is looking simplier to understand to me I only tried this which worked fine.
Thanks at both!