# Convert Array to a number

**URL:** https://discourse.nodered.org/t/convert-array-to-a-number/89579
**Category:** General
**Created:** [17 July 2024 18:54 UTC](https://discourse.nodered.org/t/convert-array-to-a-number/89579 "2024-07-17T18:54:40Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![xfear1](https://avatars.discourse-cdn.com/v4/letter/x/4da419/32.png) [@xfear1](https://discourse.nodered.org/u/xfear1)
#### Post date: [17 July 2024 18:54 UTC](https://discourse.nodered.org/t/convert-array-to-a-number/89579/1 "2024-07-17T18:54:40Z")

</div>

Hi!  
I'm having a problem to convert a value my PLC is sending with modbus, is says its in array, but I need to use this value to write on a chart, I don't know if I need to convert to number to be able to write in the chart, but thats what I think is the way to work.  
I tried to convert the value the modbus is sending with this following function but is not working:

msg.payload = Number(msg.payload)

return msg;

Anyone can help? Do I need to convert the value from array to number to write on the chart or don't? If not why is not working?  
If I need to convert the value to number, can anyone tell me how to do it?

 ![number 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/5/158ceb18a8721a057d1622b525b2d52d4453bd64.png)

---

<div class="post-metadata">

### Author: ![AndyO](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@AndyO](https://discourse.nodered.org/u/AndyO)
#### Post date: [17 July 2024 19:15 UTC](https://discourse.nodered.org/t/convert-array-to-a-number/89579/2 "2024-07-17T19:15:06Z")

</div>

Can you show debug1 as complete msg object?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/21af519dfe48d938adaf7c949c39c77b146cbb2b.png)

- you can try for function:

```auto
msg.payload = msg.payload[0]
return msg;

```

---

<div class="post-metadata">

### Author: ![xfear1](https://avatars.discourse-cdn.com/v4/letter/x/4da419/32.png) [@xfear1](https://discourse.nodered.org/u/xfear1)
#### Post date: [17 July 2024 19:23 UTC](https://discourse.nodered.org/t/convert-array-to-a-number/89579/3 "2024-07-17T19:23:10Z")

</div>

Yes.  
Changed the debug to complete.

 ![number2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/a/7a5e3960cfb53a36085e9a00ab900864fd2dd69e.png)

And then changed it back to normal and changed the function code to what you suggested.

 ![number3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2f88a96df7d8f9cbf13e1ff9efd74cc81b7deb6f.png)

---

<div class="post-metadata">

### Author: ![AndyO](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@AndyO](https://discourse.nodered.org/u/AndyO)
#### Post date: [17 July 2024 19:41 UTC](https://discourse.nodered.org/t/convert-array-to-a-number/89579/4 "2024-07-17T19:41:05Z")

</div>

I think response from both ports is different:

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

this will work on first node

```auto
msg.payload = msg.payload[0]
return msg;

```

this will work on second node

```auto
msg.payload = msg.payload.data[0]
return msg;

```

---

<div class="post-metadata">

### Author: ![xfear1](https://avatars.discourse-cdn.com/v4/letter/x/4da419/32.png) [@xfear1](https://discourse.nodered.org/u/xfear1)
#### Post date: [17 July 2024 20:16 UTC](https://discourse.nodered.org/t/convert-array-to-a-number/89579/5 "2024-07-17T20:16:09Z")

</div>

It worked, thank you!  
My chart is reading the values as it should, just one thing, is there a way to filter the value anything above 0?  
I see there is the polling rate of my modbus reading but i need that reading the entire time waiting for the value to come.  
Is there a function I can write like If value \> 0 then pass the information?

---

<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: [15 October 2024 20:16 UTC](https://discourse.nodered.org/t/convert-array-to-a-number/89579/6 "2024-10-15T20:16:22Z")

</div>

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