MQTT Values out of object

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 ?

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;
[{"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

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

example..

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;

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

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...

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;
1 Like

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