I always gettin undefined Error if i merge 2 Messages in a Function Node

Hi,

i always gettin a TypeError: Cannot read properties of undefined (reading 'TotalStartTime')"

If i want to use 2 Merged Messages in a Function Node.

When i connect the Node coming from Influx (Read) to the Function Node i gettin this TypeError above.

Here is my Flow:

[
    {
        "id": "53900d740b730748",
        "type": "function",
        "z": "3f086c32776e553e",
        "name": "Werte prüfen/ Werte umwandeln",
        "func": "var totalstarttime = msg.payload.ENERGY.TotalStartTime;\nvar totalkwh = msg.payload.ENERGY.Total;\nvar yesterdaykwh = msg.payload.ENERGY.Yesterday;\nvar todaykwh = msg.payload.ENERGY.Today;\nvar period = msg.payload.ENERGY.Period;\nvar watt = msg.payload.ENERGY.Power;\nvar voltage = msg.payload.ENERGY.Voltage;\nvar ampere = msg.payload.ENERGY.Current;\nvar factor = msg.payload.ENERGY.Factor;\nvar scheinleistung = msg.payload.ENERGY.ApparentPower;\nvar blindleistung = msg.payload.ENERGY.ReactivePower;\n\n\n    msg.payload = {\n        totalstarttime: totalstarttime,\n        totalkwh: totalkwh,\n        yesterdaykwh: yesterdaykwh,\n        todaykwh: todaykwh,\n        period: period,\n        watt: watt,\n        voltage: voltage,\n        ampere: ampere,\n        factor: factor,\n        scheinleistung: scheinleistung,\n        blindleistung: blindleistung\n    }\nreturn msg;\n\n\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 700,
        "y": 100,
        "wires": [
            [
                "aac3aa3e82b0541d"
            ]
        ]
    },
    {
        "id": "6afb76d9e25fac8c",
        "type": "function",
        "z": "3f086c32776e553e",
        "name": "Query Totalkwh auslesen",
        "func": "msg.query ='SELECT totalkwh FROM solarkraftwerk ORDER BY time DESC LIMIT 1';\nreturn msg; ",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 210,
        "y": 240,
        "wires": [
            [
                "dc2fad0f6c3ce051"
            ]
        ]
    },
    {
        "id": "dc2fad0f6c3ce051",
        "type": "influxdb in",
        "z": "3f086c32776e553e",
        "influxdb": "0e895e68048f325c",
        "name": "InfluxDB Solarkraftwerk Read",
        "query": "",
        "rawOutput": false,
        "precision": "",
        "retentionPolicy": "",
        "org": "organisation",
        "x": 500,
        "y": 240,
        "wires": [
            [
                "8ecaf0e23257306c"
            ]
        ]
    },
    {
        "id": "4adb842cb842dae2",
        "type": "inject",
        "z": "3f086c32776e553e",
        "name": "Total 0",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"Time\":1667822720,\"Switch1\":\"OFF\",\"ENERGY\":{\"TotalStartTime\":\"2022-10-29T18:30:48\",\"Total\":0,\"Yesterday\":1.818,\"Today\":0.15,\"Period\":1,\"Power\":27,\"ApparentPower\":43,\"ReactivePower\":33,\"Factor\":0.63,\"Voltage\":237,\"Current\":0.18}}",
        "payloadType": "json",
        "x": 110,
        "y": 80,
        "wires": [
            [
                "6afb76d9e25fac8c",
                "d1ac6107bfffcb81"
            ]
        ]
    },
    {
        "id": "8ecaf0e23257306c",
        "type": "change",
        "z": "3f086c32776e553e",
        "name": "",
        "rules": [
            {
                "t": "move",
                "p": "payload[0]",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 770,
        "y": 240,
        "wires": [
            [
                "d1ac6107bfffcb81"
            ]
        ]
    },
    {
        "id": "d1ac6107bfffcb81",
        "type": "join",
        "z": "3f086c32776e553e",
        "name": "",
        "mode": "custom",
        "build": "merged",
        "property": "payload",
        "propertyType": "msg",
        "key": "payload",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 1030,
        "y": 200,
        "wires": [
            [
                "53900d740b730748"
            ]
        ]
    },
    {
        "id": "aac3aa3e82b0541d",
        "type": "debug",
        "z": "3f086c32776e553e",
        "name": "debug 4",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1040,
        "y": 120,
        "wires": []
    },
    {
        "id": "0e895e68048f325c",
        "type": "influxdb",
        "hostname": "xxx",
        "port": "8086",
        "protocol": "http",
        "database": "solarkraftwerk",
        "name": "InfluxDB Solarkraftwerk",
        "usetls": false,
        "tls": "",
        "influxdbVersion": "1.x",
        "url": "http://localhost:8086",
        "rejectUnauthorized": true
    }
]

My Function Node looks like this:

var totalstarttime = msg.payload.ENERGY.TotalStartTime;
var totalkwh = msg.payload.ENERGY.Total;
var yesterdaykwh = msg.payload.ENERGY.Yesterday;
var todaykwh = msg.payload.ENERGY.Today;
var period = msg.payload.ENERGY.Period;
var watt = msg.payload.ENERGY.Power;
var voltage = msg.payload.ENERGY.Voltage;
var ampere = msg.payload.ENERGY.Current;
var factor = msg.payload.ENERGY.Factor;
var scheinleistung = msg.payload.ENERGY.ApparentPower;
var blindleistung = msg.payload.ENERGY.ReactivePower;


    msg.payload = {
        totalstarttime: totalstarttime,
        totalkwh: totalkwh,
        yesterdaykwh: yesterdaykwh,
        todaykwh: todaykwh,
        period: period,
        watt: watt,
        voltage: voltage,
        ampere: ampere,
        factor: factor,
        scheinleistung: scheinleistung,
        blindleistung: blindleistung
    }
return msg;

MQTT IN Node:

{"Time":1667822720,"Switch1":"OFF","ENERGY":{"TotalStartTime":"2022-10-29T18:30:48","Total":0,"Yesterday":1.818,"Today":0.15,"Period":1,"Power":27,"ApparentPower":43,"ReactivePower":33,"Factor":0.63,"Voltage":237,"Current":0.18}}

Join Node: A Merged Object after 2 Messages

My Goal is it to read the Total Energy History from Influx Database and compare it in the Function Node with the actually value

Add a Debug node showing what is coming out of the Join node and check it is what you expect.

The join node set to merge and count 2 will output the inject before the query, the count should be the total of msg.payload properties after the merge, in your case 4.

I see no need for the join though, something like this should work, as long as db node does not overwrite the msg property

[{"id":"4adb842cb842dae2","type":"inject","z":"452103ea51141731","name":"Total 0","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"Time\":1667822720,\"Switch1\":\"OFF\",\"ENERGY\":{\"TotalStartTime\":\"2022-10-29T18:30:48\",\"Total\":0,\"Yesterday\":1.818,\"Today\":0.15,\"Period\":1,\"Power\":27,\"ApparentPower\":43,\"ReactivePower\":33,\"Factor\":0.63,\"Voltage\":237,\"Current\":0.18}}","payloadType":"json","x":110,"y":3260,"wires":[["9dfa53eb854bede5"]]},{"id":"9dfa53eb854bede5","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"move","p":"payload.ENERGY","pt":"msg","to":"temp","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":3260,"wires":[["6afb76d9e25fac8c"]]},{"id":"6afb76d9e25fac8c","type":"function","z":"452103ea51141731","name":"simulate Query Totalkwh auslesen","func":"msg.payload = [{totalkwh:10}];\nreturn msg; ","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":220,"y":3320,"wires":[["8ecaf0e23257306c"]]},{"id":"8ecaf0e23257306c","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"move","p":"payload[0].totalkwh","pt":"msg","to":"hold","tot":"msg"},{"t":"move","p":"temp","pt":"msg","to":"payload","tot":"msg"},{"t":"move","p":"hold","pt":"msg","to":"payload.totalkwh","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":3320,"wires":[["53900d740b730748"]]},{"id":"53900d740b730748","type":"function","z":"452103ea51141731","name":"Werte prüfen/ Werte umwandeln","func":"msg.payload = {\n        totalstarttime:  msg.payload.TotalStartTime,\n        totalkwh:  msg.payload.totalkwh,\n        yesterdaykwh: msg.payload.Yesterday,\n        todaykwh: msg.payload.Today,\n        period:msg.payload.Period,\n        watt: msg.payload.Power,\n        voltage: msg.payload.Voltage,\n        ampere: msg.payload.Current,\n        factor: msg.payload.Factor,\n        scheinleistung: msg.payload.ApparentPower,\n        blindleistung: msg.payload.ReactivePower\n    }\nreturn msg;\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":3320,"wires":[["aac3aa3e82b0541d"]]},{"id":"aac3aa3e82b0541d","type":"debug","z":"452103ea51141731","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":680,"y":3240,"wires":[]}]
1 Like

Out of Join comes this:

{"Time":1667822720,"Switch1":"OFF","ENERGY":{"TotalStartTime":"2022-10-29T18:30:48","Total":0,"Yesterday":1.818,"Today":0.15,"Period":1,"Power":27,"ApparentPower":43,"ReactivePower":33,"Factor":0.63,"Voltage":237,"Current":0.18}}

and this:

{"time":"2022-11-07T15:09:20.094Z","totalkwh":6.627}

This is what i expect. But Debug of the Function Node shows 2 Messages:

First is this:

{"totalstarttime":"2022-10-29T18:30:48","totalkwh":0,"yesterdaykwh":1.818,"todaykwh":0.15,"period":1,"watt":27,"voltage":237,"ampere":0.18,"factor":0.63,"scheinleistung":43,"blindleistung":33}

and a Second Message from the Debug Node after the Function Node:

TypeError: Cannot read properties of undefined (reading 'TotalStartTime')

The send after 4 Messages works nice big thx :grinning:

I got another little Problem with this Join Node:

On the First Message going thru Join Node i get from the value totalkwh Readed from INFLUX Node a "undefined" on the Second Message and all subsequent its fine.

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