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

**URL:** <https://discourse.nodered.org/t/how-to-make-2-msgs-go-to-a-single-database-row-i-get-2-separate-rows/87841>\
**Category:** General\
**Created:** [9 May 2024 16:32 UTC](https://discourse.nodered.org/t/how-to-make-2-msgs-go-to-a-single-database-row-i-get-2-separate-rows/87841 "2024-05-09T16:32:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [9 May 2024 16:32 UTC](https://discourse.nodered.org/t/how-to-make-2-msgs-go-to-a-single-database-row-i-get-2-separate-rows/87841/1 "2024-05-09T16:32:26Z")

</div>

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.

 ![10](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/812c8a1e5f5bef446b8868da8e447d0993a42d0b.png)

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

Thanks

Flow ...

```auto
[
    {
        "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"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [9 May 2024 17:21 UTC](https://discourse.nodered.org/t/how-to-make-2-msgs-go-to-a-single-database-row-i-get-2-separate-rows/87841/2 "2024-05-09T17:21:31Z")

</div>

> [@questuk](#):
>
> because one is fractional slower than the other

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 :

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [9 May 2024 17:26 UTC](https://discourse.nodered.org/t/how-to-make-2-msgs-go-to-a-single-database-row-i-get-2-separate-rows/87841/3 "2024-05-09T17:26:09Z")

</div>

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

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [9 May 2024 17:33 UTC](https://discourse.nodered.org/t/how-to-make-2-msgs-go-to-a-single-database-row-i-get-2-separate-rows/87841/4 "2024-05-09T17:33:10Z")

</div>

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

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 👍

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 ?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [9 May 2024 17:39 UTC](https://discourse.nodered.org/t/how-to-make-2-msgs-go-to-a-single-database-row-i-get-2-separate-rows/87841/5 "2024-05-09T17:39:37Z")

</div>

> [@questuk](#):
>
> how do you get the flow into one single line for the forum

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/9/395283e579a1a79c54761b7e7c194d6889235ebe.png)

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [9 May 2024 17:39 UTC](https://discourse.nodered.org/t/how-to-make-2-msgs-go-to-a-single-database-row-i-get-2-separate-rows/87841/6 "2024-05-09T17:39:48Z")

</div>

> [@questuk](#):
>
> how do you get the flow into one single line...

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

 ![thurs_compact_v2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/b/fbf2faa459036102c6b1a0176befab8008842c79.png)

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [9 May 2024 18:16 UTC](https://discourse.nodered.org/t/how-to-make-2-msgs-go-to-a-single-database-row-i-get-2-separate-rows/87841/7 "2024-05-09T18:16:36Z")

</div>

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

Thanks

---

<div class="post-metadata">

**Author:** ![Best](https://avatars.discourse-cdn.com/v4/letter/b/8baadc/32.png) [@Best](https://discourse.nodered.org/u/Best)\
**Post date:** [12 May 2024 05:03 UTC](https://discourse.nodered.org/t/how-to-make-2-msgs-go-to-a-single-database-row-i-get-2-separate-rows/87841/8 "2024-05-12T05:03:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [12 May 2024 13:29 UTC](https://discourse.nodered.org/t/how-to-make-2-msgs-go-to-a-single-database-row-i-get-2-separate-rows/87841/9 "2024-05-12T13:29:32Z")

</div>

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 👍

Thanks

---

<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:** [10 August 2024 13:29 UTC](https://discourse.nodered.org/t/how-to-make-2-msgs-go-to-a-single-database-row-i-get-2-separate-rows/87841/10 "2024-08-10T13:29:50Z")

</div>

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