Cant Add a own value Number to an Object Payload

Hi,

i have a Problem to add in a function Node to a Msg.Payload (is an Object) a normal value like a Number.

Its to add the Total Watthours from an old Inverter to the New Inverters Value:

var time = new Date();
var totalkwh = msg.payload["opendtu/12123/0/yieldtotal"];
var field1 = "totalkwh";


var totalkwhnew = totalkwh+1544;
msg.params = [time, totalkwhnew, field1];

return msg;

The 1544 are not added but why ?

You are not putting 1544 into msg.payload

Have you set the debug to output the complete message?

What do you see in debug panel if you change code to this:

var time = new Date();
var totalkwh = msg.payload["opendtu/12123/0/yieldtotal"];
var field1 = "totalkwh";
var totalkwhnew = totalkwh+1544;

msg.params = [time, totalkwhnew, field1];

msg.warn({ totalkwh, totalkwhnew, payload: msg.payload, params: msg.params })

return msg;

Try this...

var time = new Date();

var totalkwh = Number(msg.payload["opendtu/12123/0/yieldtotal"]);

if (isNaN(totalkwh)) {
    node.error("yieldtotal is not a number");
    return null;
}

var field1 = "totalkwh";
var totalkwhnew = totalkwh + 1544;

msg.params = [time, totalkwhnew, field1];

return msg;

It appears to bypass the function node without effect.

I think it's a problem with the join node before the function node

Post deleted

Maybe we need to check the value of msg.payload["opendtu/12123/0/yieldtotal"]

@dynamicdave put in a check that exits if it is not a number. (NaN)

So if it isn't a number, it will just exit.

Are you seeing/getting the node.error("yieldtotal is not a number"); message?

Yes i get this if i set the Join Node to Array or String, if i set the Join Node (before the Function Node) to "a key /value Object " with msg.topic as the key then it bypass the function Node and i dont see the node.error("yieldtotal is not a number");

Show us the message going into the function node

I post the complete JSON:

[
    {
        "id": "e181ab23ca2013dc",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "a51b2b36cf83199b",
        "type": "mqtt in",
        "z": "e181ab23ca2013dc",
        "name": "",
        "topic": "opendtu/12123/0/yieldtotal",
        "qos": "0",
        "datatype": "json",
        "broker": "722c90e4f8ec0db9",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 270,
        "y": 260,
        "wires": [
            [
                "0184268473772f5f"
            ]
        ]
    },
    {
        "id": "0184268473772f5f",
        "type": "join",
        "z": "e181ab23ca2013dc",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "useparts": true,
        "accumulate": false,
        "timeout": "",
        "count": "1",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 630,
        "y": 240,
        "wires": [
            [
                "d185134d62647599"
            ]
        ]
    },
    {
        "id": "d185134d62647599",
        "type": "function",
        "z": "e181ab23ca2013dc",
        "name": "function 2",
        "func": "var time = new Date();\n\nvar totalkwh = Number(msg.payload[\"opendtu/12123/0/yieldtotal\"]);\n\nif (isNaN(totalkwh)) {\n    node.error(\"yieldtotal is not a number\");\n    return null;\n}\n\nvar field1 = \"totalkwh\";\nvar totalkwhnew = totalkwh + 1544;\n\nmsg.params = [time, totalkwhnew, field1];\n\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 860,
        "y": 260,
        "wires": [
            [
                "20ebe008a74ff4f5"
            ]
        ]
    },
    {
        "id": "20ebe008a74ff4f5",
        "type": "debug",
        "z": "e181ab23ca2013dc",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1140,
        "y": 260,
        "wires": []
    },
    {
        "id": "722c90e4f8ec0db9",
        "type": "mqtt-broker",
        "name": "Mqtt Server",
        "broker": "mosquitto",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "5",
        "keepalive": "60",
        "cleansession": true,
        "autoUnsubscribe": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    }
]

Frist Message is from MQTT to Join Node to Function Node to Debug

Edit: Shrug

As @Steve-Mcl has mentioned, set the debug to show complete message object, as the result of your addition is in msg.params.

or if you want the result in msg.payload
change

msg.params = \[time, totalkwhnew, field1\];

to

msg.payload = [time, totalkwhnew, field1];

This works, but how to insert this into Postgresql ?

My Postgres Node is as follows:

INSERT INTO solar (time, field1) VALUES ($1, $2),($1, $2);

Heres explained only with msg.params

It works with msg.params i just didnt see it because i had a debug node with msg.payload

In the Database its all fine !

Just saying ^ :sweat_smile:

From my experience setting debug to show entire msg is nr1 most important thing when dragging a new debug node into a flow. Ideally I'll copy an existing one instead to get it configured correctly automatically.

See this PR already merged into v5 to set default the way you want it.

Glorious!