# Persist Payload Data Through Modbus Read

**URL:** <https://discourse.nodered.org/t/persist-payload-data-through-modbus-read/44934>\
**Category:** General\
**Created:** [29 April 2021 14:16 UTC](https://discourse.nodered.org/t/persist-payload-data-through-modbus-read/44934 "2021-04-29T14:16:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![igloodan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igloodan/32/40997_2.png) [@igloodan](https://discourse.nodered.org/u/igloodan)\
**Post date:** [29 April 2021 14:16 UTC](https://discourse.nodered.org/t/persist-payload-data-through-modbus-read/44934/1 "2021-04-29T14:16:23Z")

</div>

I've got a flow setup where a CSV file is parsed for various items of data, the most important being and electricity meters "name" and then all the information required to read it via Modbus. I have managed to get the Modbus to work but the "name" doesn't make it through the "Modbus - Flex - Getter" node.

I have thought about using a "Split" and "Join" node but that would make a new message for every property of the modbus information, which I don't want.

The payload going into the "Modbus - Flex - Getter" node has all the data, the payload coming out has only the value read from the Modbus device. What's the best way for me get the original payload with a new item named "meterReading"?

Thanks.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 April 2021 14:32 UTC](https://discourse.nodered.org/t/persist-payload-data-through-modbus-read/44934/2 "2021-04-29T14:32:26Z")

</div>

Welcome to the forum @igloodan

If you copy the name to a top level message property such as msg.meterName before the modbus node then, if the node is well behaved, it should still be there in the message passed on.

---

<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:** [29 April 2021 14:42 UTC](https://discourse.nodered.org/t/persist-payload-data-through-modbus-read/44934/3 "2021-04-29T14:42:59Z")

</div>

Hi Igloodan,

yes .. msg.payload is replaced by the Modbus Flex Getter node with the modbus register values reply .. and unfortunatelly the node doesnt keep the properties of the original msg ..  
but i believe you can preserve the original payload (or any other properties) with the following example.

Consider having a simple read

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/27fe4e3e0e2251cedfcd794c5e210c24975d61df.png)

In the Function node you can pass in `value` all the data that you want to preserve.

```auto
msg.payload = { 
    'value': {originalPayload: msg.payload, myKey: "test"},
    'fc': 4,
    'unitid': 1,
    'address': 0,
    'quantity': 120
} 
return msg;

```

That you can later pickup from `msg.modbusRequest.value`

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0cf7863d9276d2049e846114021811962b2f1be7.png)

**Test Flow:**

```auto
[{"id":"d7423fb4.625788","type":"function","z":"981a2a72.4329f","name":"FC4","func":"msg.payload = { \n \n 'value': {originalPayload: msg.payload, myKey: \"test\"},\n 'fc': 4,\n 'unitid': 1,\n 'address': 0,\n 'quantity': 120\n \n} \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":940,"wires":[["5f36b457.bc277c"]]},{"id":"5f36b457.bc277c","type":"modbus-flex-getter","z":"981a2a72.4329f","name":"","showStatusActivities":true,"showErrors":true,"logIOActivities":false,"server":"bf37180a.2d18e","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":560,"y":940,"wires":[["19f8c57d.a690e3"],[]]},{"id":"6b38e4c3.61cac4","type":"inject","z":"981a2a72.4329f","name":"trigger","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"temperatures","payload":"","payloadType":"date","x":140,"y":940,"wires":[["d7423fb4.625788"]]},{"id":"19f8c57d.a690e3","type":"debug","z":"981a2a72.4329f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":940,"wires":[]},{"id":"bf37180a.2d18e","type":"modbus-client","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"192.168.1.66","tcpPort":"502","tcpType":"TPC-RTU-BUFFERED","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":1,"commandDelay":20,"clientTimeout":1000,"reconnectOnTimeout":true,"reconnectTimeout":2000,"parallelUnitIdsAllowed":true}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 April 2021 14:49 UTC](https://discourse.nodered.org/t/persist-payload-data-through-modbus-read/44934/4 "2021-04-29T14:49:49Z")

</div>

> [@UnborN](#):
>
> unfortunatelly the node doesnt keep the properties of the original msg

That is a pity. I think that is arguably a bug in that node. Certainly it is breaking the convention.

---

<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:** [29 April 2021 23:53 UTC](https://discourse.nodered.org/t/persist-payload-data-through-modbus-read/44934/5 "2021-04-29T23:53:31Z")

</div>

After further reading on the Flex Getter node's settings .. it seems that there is an option to pass on properties of the original msg. Its not enabled by default but you have to check **Keep Msg Properties**.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/c/4cac81d83e44349eee1431d21c95e5ccdf48ec67.png)

So whatever data you need to pass on, dont leave it msg.payload but have it in other properties like  
`msg.deviceName = "myMeter1"` etc

ps. apologies for the confusion, even though passing data with the use of `value` would have worked also

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 April 2021 08:16 UTC](https://discourse.nodered.org/t/persist-payload-data-through-modbus-read/44934/6 "2021-04-30T08:16:37Z")

</div>

It is odd having that optional, it is very unusual to have the situation where you would want a node to discard other properties.

---

<div class="post-metadata">

**Author:** ![igloodan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igloodan/32/40997_2.png) [@igloodan](https://discourse.nodered.org/u/igloodan)\
**Post date:** [30 April 2021 10:30 UTC](https://discourse.nodered.org/t/persist-payload-data-through-modbus-read/44934/7 "2021-04-30T10:30:36Z")

</div>

UnborN,

Thanks for your first (and subsequent) help with this.  
Why is it that an object within the original payload makes it through, but not values?

Thanks.

---

<div class="post-metadata">

**Author:** ![igloodan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igloodan/32/40997_2.png) [@igloodan](https://discourse.nodered.org/u/igloodan)\
**Post date:** [30 April 2021 10:32 UTC](https://discourse.nodered.org/t/persist-payload-data-through-modbus-read/44934/8 "2021-04-30T10:32:10Z")

</div>

While we're on the topic of the Modbus Flex Getter node, what is actual difference between the two outputs other than the order of what is returned?  
The below seems to suggest that it's exactly the same data but in a different order?

```auto
Output 1: data Array (PDU), modbus response Buffer, input message

Output 2: modbus response Buffer, data Array (PDU), input message

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 April 2021 10:34 UTC](https://discourse.nodered.org/t/persist-payload-data-through-modbus-read/44934/9 "2021-04-30T10:34:26Z")

</div>

It seems the 2nd message gives you access to the original message. It might have been added at a later time for the reasons above?

---

<div class="post-metadata">

**Author:** ![igloodan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igloodan/32/40997_2.png) [@igloodan](https://discourse.nodered.org/u/igloodan)\
**Post date:** [30 April 2021 10:47 UTC](https://discourse.nodered.org/t/persist-payload-data-through-modbus-read/44934/10 "2021-04-30T10:47:45Z")

</div>

It looks like "input message" is listed on both outputs though!?

---

<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:** [30 April 2021 11:07 UTC](https://discourse.nodered.org/t/persist-payload-data-through-modbus-read/44934/11 "2021-04-30T11:07:59Z")

</div>

> [@igloodan](#):
>
> Why is it that an object within the original payload makes it through, but not values?

Can you share a simple flow and screenshots to better visualize what you are asking ?  
Use Debug nodes before and after your Flex Getter and show as the results.  
Set them to show Complete msg object  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/e/debd6d292a0acfbe2531f16337884871f751ff35.png)

> [@igloodan](#):
>
> The below seems to suggest that it's exactly the same data but in a different order?

.. yes the order and structure of the reply msg from two outputs differs a little but it practically has the same data.

---

<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:** [29 June 2021 11:08 UTC](https://discourse.nodered.org/t/persist-payload-data-through-modbus-read/44934/12 "2021-06-29T11:08:59Z")

</div>

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