# MQTT Values out of object

**URL:** <https://discourse.nodered.org/t/mqtt-values-out-of-object/63231>\
**Category:** General\
**Created:** [30 May 2022 06:32 UTC](https://discourse.nodered.org/t/mqtt-values-out-of-object/63231 "2022-05-30T06:32:34Z")\
**Posts on this page:** 5\
**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:** [30 May 2022 06:32 UTC](https://discourse.nodered.org/t/mqtt-values-out-of-object/63231/1 "2022-05-30T06:32:34Z")

</div>

Hello everyone,

I got this script with some help of @E1cid and now i would like to call some of the object values later in that same node. Is that possible ?

```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("$5600006601/")) {
        acc["L" + (obj.phaseId + 1) + "auto"] = obj.power;
        acc.auto += obj.power;
    } else if (obj.formula.includes("$5600006973/")) {
        acc["L" + (obj.phaseId + 1) + "PV"] = obj.power;
        acc.PV += obj.power;
    } else if (obj.formula.includes("$5600006667/")) {
        acc["L" + (obj.phaseId + 1) + "Stopcontacten"] = obj.power;
        acc.Stopcontacten += obj.power;
    }
    return acc
}, { net: 0, auto: 0, PV: 0, Stopcontacten: 0 });
return msg;

```

```auto
[{"id":"cf0f39de322a6ea0","type":"mqtt in","z":"76de9c78a268111d","name":"Real-time energy data (1-second data)","topic":"servicelocation/e734acdc-a528-40ba-8f5c-4f21780b5d46/realtime","qos":"1","datatype":"json","broker":"2197f44a.ad9f2c","nl":false,"rap":true,"rh":0,"inputs":0,"x":170,"y":380,"wires":[["678afc1b0857ce6c","0d56ecda3b488ad6"]]},{"id":"678afc1b0857ce6c","type":"function","z":"76de9c78a268111d","name":"","func":"msg.payload = msg.payload.channelPowers.reduce((acc, obj) => {\nif(obj.formula.includes(\"$5600004355/\")){\nacc[\"L\" + (obj.phaseId+1) + \"net\"] = obj.power;\nacc.net += obj.power;\n}else if (obj.formula.includes(\"$5600006601/\")){\nacc[\"L\" + (obj.phaseId+1) + \"auto\"] = obj.power;\nacc.auto += obj.power;\n}else if (obj.formula.includes(\"$5600006973/\")){\nacc[\"L\" + (obj.phaseId+1) + \"PV\"] = obj.power;\nacc.PV += obj.power;\n}else if (obj.formula.includes(\"$5600006667/\")){\nacc[\"L\" + (obj.phaseId+1) + \"Stopcontacten\"] = obj.power;\nacc.Stopcontacten += obj.power;\n}\nreturn acc\n},{net:0,auto:0,PV:0,Stopcontacten:0});\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":380,"wires":[["7d3fd77334f951d9","33b5474b13bbb037"]]},{"id":"26e7ddf548c4f606","type":"comment","z":"76de9c78a268111d","name":"Visualisatie","info":"Dit gedeelte geeft de visualisatie weer van de vermogens op het dashboard","x":90,"y":340,"wires":[]},{"id":"33b5474b13bbb037","type":"debug","z":"76de9c78a268111d","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":420,"wires":[]},{"id":"0d56ecda3b488ad6","type":"debug","z":"76de9c78a268111d","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":410,"y":420,"wires":[]},{"id":"2197f44a.ad9f2c","type":"mqtt-broker","name":"","broker":"192.168.0.249","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

```

thanks Olivia

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 May 2022 06:41 UTC](https://discourse.nodered.org/t/mqtt-values-out-of-object/63231/2 "2022-05-30T06:41:11Z")

</div>

> [@Olivia](#):
>
> i would like to call some of the object values later in that same node. Is that possible ?

Depends what you mean by "call some of the object values" but esentially, you can do whatever you want.

example..

```javascript
const origPayload = msg.payload
const channelPowers = origPayload.channelPowers
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("$5600006601/")) {
        acc["L" + (obj.phaseId + 1) + "auto"] = obj.power;
        acc.auto += obj.power;
    } else if (obj.formula.includes("$5600006973/")) {
        acc["L" + (obj.phaseId + 1) + "PV"] = obj.power;
        acc.PV += obj.power;
    } else if (obj.formula.includes("$5600006667/")) {
        acc["L" + (obj.phaseId + 1) + "Stopcontacten"] = obj.power;
        acc.Stopcontacten += obj.power;
    }
    return acc
}, { net: 0, auto: 0, PV: 0, Stopcontacten: 0 });

//do whatever you need to here!
msg.payload.inputLength = channelPowers.length
msg.payload.sum = msg.payload.value_a + msg.payload.value_b
msg.payload.originalPayload = origPayload

//now send msg to next node
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:** [30 May 2022 06:47 UTC](https://discourse.nodered.org/t/mqtt-values-out-of-object/63231/3 "2022-05-30T06:47:35Z")

</div>

> [@Steve-Mcl](#):
>
> Depends what you mean by "call some of the object values" but esentially, you can do whatever you want.
> 
> example..

I would like to use for example "net" or "auto" in a formula later in that node.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 May 2022 07:05 UTC](https://discourse.nodered.org/t/mqtt-values-out-of-object/63231/4 "2022-05-30T07:05:17Z")

</div>

As you have not provided any example data nor have you said what you want to actually do with the properties of the payload - I have no idea!

Making some assumptions, here is another example...

```auto
const origPayload = msg.payload
const channelPowers = origPayload.channelPowers
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("$5600006601/")) {
        acc["L" + (obj.phaseId + 1) + "auto"] = obj.power;
        acc.auto += obj.power;
    } else if (obj.formula.includes("$5600006973/")) {
        acc["L" + (obj.phaseId + 1) + "PV"] = obj.power;
        acc.PV += obj.power;
    } else if (obj.formula.includes("$5600006667/")) {
        acc["L" + (obj.phaseId + 1) + "Stopcontacten"] = obj.power;
        acc.Stopcontacten += obj.power;
    }
    return acc
}, { net: 0, auto: 0, PV: 0, Stopcontacten: 0 });

//do whatever you need to here! 👇
msg.payload.info = `auto has the value is ${msg.payload.auto}`
msg.payload.net_times_two = msg.payload.net * 2
//do whatever you need to here! 👆

//now send msg to next node
return msg;

```

---

<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:** [13 June 2022 07:06 UTC](https://discourse.nodered.org/t/mqtt-values-out-of-object/63231/5 "2022-06-13T07:06:10Z")

</div>

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