# Fill in array of values when payload data is empty/incorrect

**URL:** https://discourse.nodered.org/t/fill-in-array-of-values-when-payload-data-is-empty-incorrect/61959
**Category:** Industrial
**Tags:** modbus
**Created:** [29 April 2022 13:35 UTC](https://discourse.nodered.org/t/fill-in-array-of-values-when-payload-data-is-empty-incorrect/61959 "2022-04-29T13:35:44Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Venomz](https://avatars.discourse-cdn.com/v4/letter/v/8dc957/32.png) [@Venomz](https://discourse.nodered.org/u/Venomz)
#### Post date: [29 April 2022 13:35 UTC](https://discourse.nodered.org/t/fill-in-array-of-values-when-payload-data-is-empty-incorrect/61959/1 "2022-04-29T13:35:44Z")

</div>

Hi!

I am currently reading 5 different Modbus Slaves and retrieving data and would like to push in "invalid" values whenever there is a connection or error retrieving the data for that specific Modbus slave.

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

What is a good way of achieving this? This is how my bad/good payload data looks like;

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

I was thinking of somehow using the quantity property to generate a self defined array[0..quantityNo] and fill this array with "invalid" data numbers, e.g. 65535 on every array element.

In my head I would look at something like:

```auto
if (msg.payload = "") then
msg.payload = (generate array0..quantity with invalid data)
else
msg.payload = msg.payload (do nothing)
return msg;

```

this code above is obviously not functional in anyway but shows how I can think of doing it right now. Would love some examples of how I can achieve my desired function!

---

<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 2022 14:24 UTC](https://discourse.nodered.org/t/fill-in-array-of-values-when-payload-data-is-empty-incorrect/61959/2 "2022-04-29T14:24:25Z")

</div>

Hello Gustav,

I notice from your screenshot, that further down your flow you use the Buffer parser nodes.  
How are those nodes configured ? Arent they using the buffer part of the msg `msg.responseBuffer.buffer` in order to do the conversion to a value ?  
My point is, if you go ahead and replace any array values you should be replacing `msg.responseBuffer.buffer` instead of `msg.payload` ?

Another question .. where do these values end up ? in a database ? on a dashboard ?  
i ask because in my case i read some Modbus values from a few energy meters, saving the data in an Sqlite database and i chose to replace the _error_ values of devices with `null`

---

<div class="post-metadata">

### Author: ![Venomz](https://avatars.discourse-cdn.com/v4/letter/v/8dc957/32.png) [@Venomz](https://discourse.nodered.org/u/Venomz)
#### Post date: [29 April 2022 14:42 UTC](https://discourse.nodered.org/t/fill-in-array-of-values-when-payload-data-is-empty-incorrect/61959/3 "2022-04-29T14:42:16Z")

</div>

Hi!

I'm using the 1st output of the Modbus Flex Sequencer which outputs an array along with the buffer to the payload. I then use the payload into a Split to split the devices and then I use the buffer parser on the devices and set their variables as global.

The 2nd output from the Modbus Flex Sequencer would probably be used if I were to read the buffer and not the array.

So yes writing the 'invalid' values to the buffer output is what is preferable. I want to write the data to the invalid values on device error/connection error sometime before I publish it as a global.

End goal from there is to take the global data and possibly process the single data before sending it to a production PLC with S7 connection for now, may be expanded upon in the future.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6e1f5da3c9fd5c8d61d877e6e7f226bd6af049c9.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/6/4638c3757ad8cdc3e1024f16b4d9952a045f5b5b.png)

An example of how you solved your swap of data to invalid data would be nice 🙂

---

<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 2022 15:28 UTC](https://discourse.nodered.org/t/fill-in-array-of-values-when-payload-data-is-empty-incorrect/61959/4 "2022-04-29T15:28:56Z")

</div>

In my project i read from many devices sequentially and if there is an error from modbus,  
i have enabled the **Empty msg on fail**.. as you have done 👍

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

and catch those errors with an **"error"** Function node after each read

```javascript
if (msg.payload == "" || msg.hasOwnProperty('error')) {

    global.set("Status.Status_31", "Disconnected")
  
    msg.payload = {

        V_31_L1: null,
        V_31_L2: null,
        V_31_L3: null,
        A_31_L1: null,
        A_31_L2: null,
        A_31_L3: null,
        P_31_L1: null,
        P_31_L2: null,
        P_31_L3: null,
        P_31_T: null,
        S_31_T: null,
        Q_31_T: null,
        PF_31_L1: null,
        PF_31_L2: null,
        PF_31_L3: null,
        F_31_L1: null,
        F_31_L2: null,
        F_31_L3: null
    }

    return [null, msg]
}

else {
    global.set("Status.Status_31", "Connected")
    return [msg, null];
}

```

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

In the code sample above im checking `if (msg.payload == "" || msg.hasOwnProperty('error'))`  
and then setting a Context global Status for the device if its Connected or Disconnected (you may not need this). Then in an ugly Javascript way manually (i didnt have much experience with JS when i wrote it)  
set each shortname coded value to `null`. If its valid send it with the first output to Buffer parser and when all msgs arrive JOIN them and then set to global. Else if its an error set to null and send to second output.

**You may not find the above useful so if you export and share your flow, we can make recommendations based on your workflow ?**

---

<div class="post-metadata">

### Author: ![Venomz](https://avatars.discourse-cdn.com/v4/letter/v/8dc957/32.png) [@Venomz](https://discourse.nodered.org/u/Venomz)
#### Post date: [30 April 2022 08:36 UTC](https://discourse.nodered.org/t/fill-in-array-of-values-when-payload-data-is-empty-incorrect/61959/5 "2022-04-30T08:36:44Z")

</div>

Thank you for the example!

I will try to implement a form of this code.

Below is my current code:

[flows (4).json](https://discourse.nodered.org/uploads/short-url/GKOyE0FgVR9LFZaCYnQEQmfjiA.json) (72.5 KB)

---

<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 2022 15:16 UTC](https://discourse.nodered.org/t/fill-in-array-of-values-when-payload-data-is-empty-incorrect/61959/6 "2022-04-30T15:16:20Z")

</div>

I studied your flow ..  
you can use something like this in the Function node

```javascript
let quantity = msg.quantity // how many addresses to fill

// if there is error
if (msg.payload == "" || msg.hasOwnProperty('error')) {

    msg.payload = [] // start with empty array 
    
    for (let x = 0; x < quantity; x++) {
        msg.payload.push(65535) // fill with 65535 
    }
}

return msg;

```

ps. i wondered before why you used `msg.payload` in the buffer parsers and not `msg.responseBuffer.buffer`. After reading the node's help tab it seems both are ok when payload is an array of integer (Int16). my bad.

---

<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 2022 17:25 UTC](https://discourse.nodered.org/t/fill-in-array-of-values-when-payload-data-is-empty-incorrect/61959/7 "2022-04-30T17:25:25Z")

</div>

It is definitely preferable to use `msg.responseBuffer.buffer` (since it is unparsed and therefore doesn't need manipulating into a buffer) but when I wrote the node I was also acutely aware that many/most of the PLC/ModBus nodes return the data parsed as array of integer so made sense to support that. Incidentally, the node also supports a hex string for processing the likes of TCP data e.g `e12baec7`

---

<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: [14 May 2022 17:26 UTC](https://discourse.nodered.org/t/fill-in-array-of-values-when-payload-data-is-empty-incorrect/61959/8 "2022-05-14T17:26:07Z")

</div>

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