How to make 2 msgs go to a single database row ... I get 2 separate rows?

Hi,

I have tried but cannot work this out, I am sure it is simple but i cannot find how.
A simple test example and flow ...
Insert a number, trim payload to fixed and the other is payload and is an electricity cost sum.

But I get 2 outputs because one is fractional slower than the other, somehow it needs to wait ?

Thanks

Flow ...

[
    {
        "id": "3593ba4fbc73c796",
        "type": "function",
        "z": "1f152ae0632b70bb",
        "name": "to fixed",
        "func": "msg.payload = Number(msg.payload.toFixed(3))\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 420,
        "y": 320,
        "wires": [
            [
                "2af008fd6fecd0d4"
            ]
        ]
    },
    {
        "id": "7e6d8b9fef9b1119",
        "type": "function",
        "z": "1f152ae0632b70bb",
        "name": "Cost 04-2024",
        "func": "msg.payload = ((msg.payload) * 0.2884); // 1kw rate\nmsg.payload = (msg.payload   + 0.4001); // Standing charge\nmsg.payload = (msg.payload   * 1.05);   // Adds 5% VAT\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 440,
        "y": 360,
        "wires": [
            [
                "c638a34eb22b0a85"
            ]
        ]
    },
    {
        "id": "6077005b98249772",
        "type": "inject",
        "z": "1f152ae0632b70bb",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "3.35123",
        "payloadType": "num",
        "x": 260,
        "y": 330,
        "wires": [
            [
                "3593ba4fbc73c796",
                "7e6d8b9fef9b1119"
            ]
        ]
    },
    {
        "id": "2af008fd6fecd0d4",
        "type": "change",
        "z": "1f152ae0632b70bb",
        "name": "",
        "rules": [
            {
                "t": "move",
                "p": "payload",
                "pt": "msg",
                "to": "data",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 640,
        "y": 320,
        "wires": [
            [
                "58510885dbc75e25"
            ]
        ]
    },
    {
        "id": "c638a34eb22b0a85",
        "type": "change",
        "z": "1f152ae0632b70bb",
        "name": "",
        "rules": [
            {
                "t": "move",
                "p": "payload",
                "pt": "msg",
                "to": "cost",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 640,
        "y": 360,
        "wires": [
            [
                "58510885dbc75e25"
            ]
        ]
    },
    {
        "id": "05452333b8a861c0",
        "type": "debug",
        "z": "1f152ae0632b70bb",
        "name": "debug 127",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1030,
        "y": 340,
        "wires": []
    },
    {
        "id": "58510885dbc75e25",
        "type": "change",
        "z": "1f152ae0632b70bb",
        "name": "set DB values",
        "rules": [
            {
                "t": "set",
                "p": "table",
                "pt": "msg",
                "to": "energy",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "device",
                "pt": "msg",
                "to": "IP22",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "data",
                "pt": "msg",
                "to": "data",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "cost",
                "pt": "msg",
                "to": "cost",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 850,
        "y": 340,
        "wires": [
            [
                "05452333b8a861c0"
            ]
        ]
    }
]

Hello .. yes msgs never arrive at one point at the same time so
you either do your calculations (to fixed and Cost) in one Function node or
if your data are coming from different sources and that is not possible,
then use a Join node to "wait" for the two messages and Join them.
In order to do that your can use a unique topic from each source

Example :

[{"id":"3593ba4fbc73c796","type":"function","z":"54efb553244c241f","name":"to fixed","func":"msg.topic = \"data\"\nmsg.payload = Number(msg.payload.toFixed(3))\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":3180,"wires":[["79374b32eb255bb8"]]},{"id":"7e6d8b9fef9b1119","type":"function","z":"54efb553244c241f","name":"Cost 04-2024","func":"msg.topic = \"cost\"\nmsg.payload = ((msg.payload) * 0.2884); // 1kw rate\nmsg.payload = (msg.payload   + 0.4001); // Standing charge\nmsg.payload = (msg.payload   * 1.05);   // Adds 5% VAT\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":3220,"wires":[["79374b32eb255bb8"]]},{"id":"6077005b98249772","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"3.35123","payloadType":"num","x":200,"y":3190,"wires":[["3593ba4fbc73c796","7e6d8b9fef9b1119"]]},{"id":"05452333b8a861c0","type":"debug","z":"54efb553244c241f","name":"debug 127","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":950,"y":3200,"wires":[]},{"id":"58510885dbc75e25","type":"change","z":"54efb553244c241f","name":"set DB values","rules":[{"t":"set","p":"payload.table","pt":"msg","to":"energy","tot":"str"},{"t":"set","p":"payload.device","pt":"msg","to":"IP22","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":3200,"wires":[["05452333b8a861c0"]]},{"id":"79374b32eb255bb8","type":"join","z":"54efb553244c241f","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":570,"y":3200,"wires":[["6296d869b4a88565","58510885dbc75e25"]]},{"id":"6296d869b4a88565","type":"debug","z":"54efb553244c241f","name":"debug 128","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":3120,"wires":[]}]
1 Like

Why split the message in first place, you can do it all in one function node.
e.g.

[{"id":"3593ba4fbc73c796","type":"function","z":"d1395164b4eec73e","name":"to fixed","func":"msg.data = Number(msg.payload.toFixed(3))\nmsg.cost = msg.payload * 0.2884; // 1kw rate\nmsg.cost+= 0.4001; // Standing charge\nmsg.cost = (msg.cost   * 1.05);   // Adds 5% VAT\nmsg.table = \"energy\";\nmsg.device = \"IP22\";\nreturn msg;\n","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":4660,"wires":[["05452333b8a861c0"]]},{"id":"6077005b98249772","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"3.35123","payloadType":"num","x":120,"y":4670,"wires":[["3593ba4fbc73c796"]]},{"id":"05452333b8a861c0","type":"debug","z":"d1395164b4eec73e","name":"debug 127","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":470,"y":4660,"wires":[]}]
2 Likes

Thanks to the both of you
2 different ways, but both give the correct result :slightly_smiling_face:

I spent hours this afternoon, trying nodes and also one single function, but could not see how to do it !

Anyway that's another set of flows I will remember and use in the future :+1:

Thanks

One quick question how do you get the flow into one single line for the forum as mine is very long, what did i not do ?

when you Export a Flow from the Editor .. and you have the JSON tab open select the compact option
That will copy it minified .. on a single line

image

That's an easy question to answer - just select the 'Compact' option when you export a flow.

The times i have exported flows, hundreds of times and i have never seen the compact button :roll_eyes:

Thanks

I use join node

merge object as an array

before join node i change each msg to difference payload like msg.payload.A and msg,payload.B

on the database node insert

insert into tablename (time,status) values ({{{msg.payload.A}}}, {{{msg.payload.B}}})

something like this

ps the database query is not exactly correct
just want to show how i separate msg

Hi,

In the end i decided to adapt E1cid version for my needs, as it was all contained in one function node, so no joining :+1:

Thanks

1 Like

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