Adding up a number of objects

Hi, I want to add my consumption from the last 12 month.
so I get a very long formula:

let Kosten = msg.payload[0].cost + msg.payload[1].cost + msg.payload[2].cost + msg.payload[3].cost + msg.payload[4].cost + msg.payload[5].cost + msg.payload[6].cost + msg.payload[7].cost + msg.payload[8].cost + msg.payload[9].cost + msg.payload[10].cost + msg.payload[11].cost;

let Verbrauch = msg.payload[0].consumption + msg.payload[1].consumption + msg.payload[2].consumption + msg.payload[3].consumption + msg.payload[4].consumption + msg.payload[5].consumption + msg.payload[6].consumption + msg.payload[7].consumption + msg.payload[8].consumption + msg.payload[9].consumption + msg.payload[10].consumption + msg.payload[11].consumption;

if the last object is missing => error

Is there a trick/formula to just add up the number of objects that are incoming / present?

Have a look at array.reduce()

let Kosten = msg.payload.reduce((acc,obj) =>  acc += obj.cost, 0)

1 Like

hm, no answere from this.

Here is the input:

[{"from":"2023-01-01T00:00:00.000+01:00","cost":107.9644096335,"unitPrice":0.21972,"consumption":491.372},{"from":"2023-02-01T00:00:00.000+01:00","cost":37.1151410147,"unitPrice":0.220789,"consumption":168.102},{"from":"2023-03-01T00:00:00.000+01:00","cost":9.2954857464,"unitPrice":0.228925,"consumption":40.605},{"from":"2023-04-01T00:00:00.000+02:00","cost":1.2290573232,"unitPrice":0.24194,"consumption":5.08},{"from":"2023-05-01T00:00:00.000+02:00","cost":0.8601506949,"unitPrice":0.229312,"consumption":3.751},{"from":"2023-06-01T00:00:00.000+02:00","cost":0.598018673,"unitPrice":0.228688,"consumption":2.615},{"from":"2023-07-01T00:00:00.000+02:00","cost":-9.5399015481,"unitPrice":-0.273907,"consumption":34.829},{"from":"2023-08-01T00:00:00.000+02:00","cost":1.428502299,"unitPrice":0.22765,"consumption":6.275},{"from":"2023-09-01T00:00:00.000+02:00","cost":1.1379060721,"unitPrice":0.208675,"consumption":5.453},{"from":"2023-10-01T00:00:00.000+02:00","cost":3.0323304039,"unitPrice":0.214193,"consumption":14.157},{"from":"2023-11-01T00:00:00.000+01:00","cost":54.8661958705,"unitPrice":0.200132,"consumption":274.15}]

Sure there is , did you forget to add it to the msg object
e.g

[{"id":"acb2384ee87c80c1","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"from\":\"2023-01-01T00:00:00.000+01:00\",\"cost\":107.9644096335,\"unitPrice\":0.21972,\"consumption\":491.372},{\"from\":\"2023-02-01T00:00:00.000+01:00\",\"cost\":37.1151410147,\"unitPrice\":0.220789,\"consumption\":168.102},{\"from\":\"2023-03-01T00:00:00.000+01:00\",\"cost\":9.2954857464,\"unitPrice\":0.228925,\"consumption\":40.605},{\"from\":\"2023-04-01T00:00:00.000+02:00\",\"cost\":1.2290573232,\"unitPrice\":0.24194,\"consumption\":5.08},{\"from\":\"2023-05-01T00:00:00.000+02:00\",\"cost\":0.8601506949,\"unitPrice\":0.229312,\"consumption\":3.751},{\"from\":\"2023-06-01T00:00:00.000+02:00\",\"cost\":0.598018673,\"unitPrice\":0.228688,\"consumption\":2.615},{\"from\":\"2023-07-01T00:00:00.000+02:00\",\"cost\":-9.5399015481,\"unitPrice\":-0.273907,\"consumption\":34.829},{\"from\":\"2023-08-01T00:00:00.000+02:00\",\"cost\":1.428502299,\"unitPrice\":0.22765,\"consumption\":6.275},{\"from\":\"2023-09-01T00:00:00.000+02:00\",\"cost\":1.1379060721,\"unitPrice\":0.208675,\"consumption\":5.453},{\"from\":\"2023-10-01T00:00:00.000+02:00\",\"cost\":3.0323304039,\"unitPrice\":0.214193,\"consumption\":14.157},{\"from\":\"2023-11-01T00:00:00.000+01:00\",\"cost\":54.8661958705,\"unitPrice\":0.200132,\"consumption\":274.15}]","payloadType":"json","x":90,"y":2760,"wires":[["63288222d43f6855"]]},{"id":"63288222d43f6855","type":"function","z":"d1395164b4eec73e","name":"function 149","func":"let Kosten = msg.payload.reduce((acc,obj)=>  acc += obj.cost, 0)\nmsg.payload = Kosten\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":2820,"wires":[["ee0c6359eb5ba6e4"]]},{"id":"ee0c6359eb5ba6e4","type":"debug","z":"d1395164b4eec73e","name":"debug 2462","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":2760,"wires":[]}]

output in payload
207.98729618310003

Now it runs, thank you

my solution:

[
    {
        "id": "63288222d43f6855",
        "type": "function",
        "z": "a85445c69706ecc9",
        "name": "Jahresauswertung",
        "func": "let Kosten = msg.payload.reduce((acc,obj)=> acc += obj.cost, 0)\nlet Bezug = msg.payload.reduce((acc,obj)=> acc += obj.consumption, 0)\nlet Preis = Kosten/Bezug *100;\nPreis = parseFloat(Preis.toFixed(2));\nKosten = parseFloat(Kosten.toFixed(2));\nBezug = parseFloat(Bezug.toFixed(2));\nmsg.payload = {Kosten,Bezug,Preis};\nmsg.topic = \"Jahresauswertung\";\nreturn msg",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 730,
        "y": 900,
        "wires": [
            []
        ]
    }
]

What do you mean, there is no change in the expression, so it must have worked before.

I don´t know :laughing:
maybe the tibber api query was not successfull.

Do you know why monaco is angry:
grafik

It is not an error (angry) as the message is just informing you that the parameter is of type any. You may ignore it.

I would probably use a change node and the following JSONata expression:

$sum(payload.cost)

That should find all the cost values on the array of payload objects, add them together, and return the sum in msg.payload (but i did not test it)...

1 Like

this works too, thank you

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.