# MQTT values sorting

**URL:** <https://discourse.nodered.org/t/mqtt-values-sorting/61642>\
**Category:** General\
**Created:** [22 April 2022 12:55 UTC](https://discourse.nodered.org/t/mqtt-values-sorting/61642 "2022-04-22T12:55:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olivia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/olivia/32/57161_2.png) [@Olivia](https://discourse.nodered.org/u/Olivia)\
**Post date:** [22 April 2022 12:55 UTC](https://discourse.nodered.org/t/mqtt-values-sorting/61642/1 "2022-04-22T12:55:50Z")

</div>

Hello everyone,

I would like to make a script that sorts my values automaticly. I tried it allready but it seems to look like i did somth wrong.

this is the script i made:

```auto
[{"id":"aeb5b23752a36cba","type":"function","z":"76de9c78a268111d","name":"","func":"var Vermogens = msg.payload\n\nfor (i = 0; Vermogens.channelPowers.length > i; i++){\n if (Vermogens.channelPowers[i].formula == \"$5600004355/2$\" ){\n if (Vermogens.channelPowers[i].phaseId == 0 ){\n L1net= Vermogens.channelPowers[i].power\n }\n if (Vermogens.channelPowers[i].phaseId == 1 ){\n L2net= Vermogens.channelPowers[i].power\n }\n if (Vermogens.channelPowers[i].phaseId == 2 ){\n L3net= Vermogens.channelPowers[i].power\n }\n }\n \n if (Vermogens.channelPowers[i].formula == \"$5600004035/2$\" ){\n if (Vermogens.channelPowers[i].phaseId == 0 ){\n L1auto= Vermogens.channelPowers[i].power\n }\n if (Vermogens.channelPowers[i].phaseId == 1 ){\n L2auto= Vermogens.channelPowers[i].power\n }\n if (Vermogens.channelPowers[i].phaseId == 2 ){\n L3auto= Vermogens.channelPowers[i].power\n }\n }\n}\n\nNet= L1net + L2net + L3net\nAuto = L1auto + L2auto + L3auto\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1220,"y":780,"wires":[["6e60695de9ef8e71"]]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/27f04e04ef2c674e18d45448de2feee97880b5ec.png)

I would like to have change this data :

```auto
[{"publishIndex":0,"formula":"$5600004355/2$","power":3702,"exportEnergy":0,"importEnergy":25693200,"phaseId":0,"current":162,"apparentPower":3705,"cosPhi":99},{"publishIndex":1,"formula":"$5600004355/1$","power":0,"exportEnergy":0,"importEnergy":2340000,"phaseId":1,"current":0,"apparentPower":0,"cosPhi":95},{"publishIndex":2,"formula":"$5600004355/0$","power":0,"exportEnergy":0,"importEnergy":1389600,"phaseId":2,"current":0,"apparentPower":0,"cosPhi":-46},{"publishIndex":3,"formula":"$5500027192/0$","power":0,"exportEnergy":194400,"importEnergy":0,"phaseId":0,"current":0,"apparentPower":0,"cosPhi":94},{"publishIndex":4,"formula":"$5500027192/1$","power":0,"exportEnergy":0,"importEnergy":0,"phaseId":1,"current":0,"apparentPower":0,"cosPhi":-57},{"publishIndex":5,"formula":"$5600004035/0$","power":3699,"exportEnergy":0,"importEnergy":20988000,"phaseId":0,"current":162,"apparentPower":3702,"cosPhi":99},{"publishIndex":6,"formula":"$5600004035/1$","power":0,"exportEnergy":0,"importEnergy":1414800,"phaseId":1,"current":0,"apparentPower":0,"cosPhi":96},{"publishIndex":7,"formula":"$5600004035/2$","power":0,"exportEnergy":0,"importEnergy":1386000,"phaseId":2,"current":0,"apparentPower":0,"cosPhi":-45}]

```

The first item my script has to look at is the formula if it end with 355 before the / then they should belong together, ...  
Then it has to seperate by the phaseId... after that it can sort the values as i show in my script...

the full data can be found here:

```auto
{"totalPower":3702,"totalReactivePower":166,"totalExportEnergy":194400,"totalImportEnergy":53211600,"monitorStatus":0,"utcTimeStamp":1650631894000,"measuredFrequency":49999996,"channelPowers":[{"publishIndex":0,"formula":"$5600004355/2$","power":3702,"exportEnergy":0,"importEnergy":25693200,"phaseId":0,"current":162,"apparentPower":3705,"cosPhi":99},{"publishIndex":1,"formula":"$5600004355/1$","power":0,"exportEnergy":0,"importEnergy":2340000,"phaseId":1,"current":0,"apparentPower":0,"cosPhi":95},{"publishIndex":2,"formula":"$5600004355/0$","power":0,"exportEnergy":0,"importEnergy":1389600,"phaseId":2,"current":0,"apparentPower":0,"cosPhi":-46},{"publishIndex":3,"formula":"$5500027192/0$","power":0,"exportEnergy":194400,"importEnergy":0,"phaseId":0,"current":0,"apparentPower":0,"cosPhi":94},{"publishIndex":4,"formula":"$5500027192/1$","power":0,"exportEnergy":0,"importEnergy":0,"phaseId":1,"current":0,"apparentPower":0,"cosPhi":-57},{"publishIndex":5,"formula":"$5600004035/0$","power":3699,"exportEnergy":0,"importEnergy":20988000,"phaseId":0,"current":162,"apparentPower":3702,"cosPhi":99},{"publishIndex":6,"formula":"$5600004035/1$","power":0,"exportEnergy":0,"importEnergy":1414800,"phaseId":1,"current":0,"apparentPower":0,"cosPhi":96},{"publishIndex":7,"formula":"$5600004035/2$","power":0,"exportEnergy":0,"importEnergy":1386000,"phaseId":2,"current":0,"apparentPower":0,"cosPhi":-45}],"voltages":[{"voltage":229,"phaseId":0},{"voltage":242,"phaseId":1},{"voltage":244,"phaseId":2}]}

```

Thanks in advance,

Olivia

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 April 2022 13:45 UTC](https://discourse.nodered.org/t/mqtt-values-sorting/61642/2 "2022-04-22T13:45:04Z")

</div>

If I understand correctly try something like this

```auto
msg.payload = msg.payload.channelPowers.reduce((acc, obj) => {
    if(obj.formula.includes("$5600004355/")){
        acc.net += obj.power;
    }else if (obj.formula.includes("$5600004035/")){
        acc.auto += obj.power;
    }
    return acc;
},{"net":0,"auto":0});
return msg;

```

---

<div class="post-metadata">

**Author:** ![Olivia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/olivia/32/57161_2.png) [@Olivia](https://discourse.nodered.org/u/Olivia)\
**Post date:** [23 April 2022 13:23 UTC](https://discourse.nodered.org/t/mqtt-values-sorting/61642/3 "2022-04-23T13:23:18Z")

</div>

I think i understand what you are doing but i dont see how it seperates the phaseId's from eatch other...  
the meaning of the script would be that the script automaticly search for the yellow values... even if the order of the payload changes...

after it found the yellow values it shoud store those in global valueble but that would be possible to find on my own

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/8/68f217f93b27512889772964d6b1c56f9b9e7b2c.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 April 2022 13:43 UTC](https://discourse.nodered.org/t/mqtt-values-sorting/61642/4 "2022-04-23T13:43:07Z")

</div>

I looked at your function and it totaled them , so i did the same, you could save them also

```auto
msg.payload = msg.payload.channelPowers.reduce((acc, obj) => {
    if(obj.formula.includes("$5600004355/")){
        acc.net[obj.phaseId] = obj.power;
    }else if (obj.formula.includes("$5600004035/")){
        acc.auto[obj.phaseId] = obj.power;
    }
    return acc
},{"net":[],"auto":[]})
return msg;

```

or

```auto
msg.payload = msg.payload.channelPowers.reduce((acc, obj) => {
    if(obj.formula.includes("$5600004355/")){
        acc["L" + (obj.phaseId+1) + "net"] = obj.power;
    }else if (obj.formula.includes("$5600004035/")){
        acc["L" + (obj.phaseId+1) + "auto"] = obj.power;
    }
    return acc
},{});
return msg;

```

Or save and total

```auto
msg.payload = msg.payload.channelPowers.reduce((acc, obj) => {
    if(obj.formula.includes("$5600004355/")){
        acc["L" + (obj.phaseId+1) + "net"] = obj.power;
        acc.net += obj.power;
    }else if (obj.formula.includes("$5600004035/")){
        acc["L" + (obj.phaseId+1) + "auto"] = obj.power;
        acc.auto += obj.power;
    }
    return acc
},{net:0,auto:0});
return msg;

```

---

<div class="post-metadata">

**Author:** ![Olivia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/olivia/32/57161_2.png) [@Olivia](https://discourse.nodered.org/u/Olivia)\
**Post date:** [25 April 2022 13:42 UTC](https://discourse.nodered.org/t/mqtt-values-sorting/61642/5 "2022-04-25T13:42:13Z")

</div>

Dear @E1cid ,

This looks allready pretty good, i am wondering how can plot this in a line graph ?

Thanks in advance!!  
olivia

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [25 April 2022 14:32 UTC](https://discourse.nodered.org/t/mqtt-values-sorting/61642/6 "2022-04-25T14:32:24Z")

</div>

That would be a new question so might be better in a new thread, and also you would need to define "this". As i gave many solutions and you never pointed to a solution.  
But to help, you would output the auto/net values, then split the object using key as msg.topic and feed the live data into the chart.

---

<div class="post-metadata">

**Author:** ![Olivia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/olivia/32/57161_2.png) [@Olivia](https://discourse.nodered.org/u/Olivia)\
**Post date:** [25 April 2022 16:01 UTC](https://discourse.nodered.org/t/mqtt-values-sorting/61642/7 "2022-04-25T16:01:49Z")

</div>

all 3 answers look good to me, ill probably use the last one.  
and ill try it by my selves first and otherwise ill ask in a new question.

Thanks !

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 May 2022 16:02 UTC](https://discourse.nodered.org/t/mqtt-values-sorting/61642/8 "2022-05-09T16:02:25Z")

</div>

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