# Writing array of arrays data to Influx

**URL:** https://discourse.nodered.org/t/writing-array-of-arrays-data-to-influx/85756
**Category:** General
**Created:** [22 February 2024 05:49 UTC](https://discourse.nodered.org/t/writing-array-of-arrays-data-to-influx/85756 "2024-02-22T05:49:03Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![NewHopeFarm](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@NewHopeFarm](https://discourse.nodered.org/u/NewHopeFarm)
#### Post date: [22 February 2024 05:49 UTC](https://discourse.nodered.org/t/writing-array-of-arrays-data-to-influx/85756/1 "2024-02-22T05:49:03Z")

</div>

I'm a bit of a novice but have made progress on my Node-Red skills but I have one last hurdle I can't quite figure out. I have an IOT node that takes temperature readings every X amount of time, stores it and then after Y amount of time sends all the data as a Hex string to my node-red (current reading + 8 historical readings). I have a Javascript that parses the hex string and puts the values into an array. Each data set is an array as well (is that right?) An array of arrays? The data set is Hive, Temp & Time, repeated. I need to write each data set within the array to my Influx DB. (Hive is the tag)

Here is what the output of the Javascript function is.

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

I have the Influx database out node, which I'm using on other flows successfully.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/dba4ff6a09fd8695a67a9ead4f94eced7087f816.png)

Would someone be kinda enough to nudge me in the write direction?

On another flow, that only has a single reading, I use a Change node that looks like this.

```auto
[
    {
        "TempLower": payload.TempLower,
        "time": payload.time
    },{
        "hive": payload.hive
    }
]

```

Not sure that it helps but this is my InfluxDB measurement table.

> select \* from S31B  
> name: S31B  
> time Humidity TempLower hive

* * *

1703981797 42.7 71.06 f866207058384979  
1703982397 42 71.6 f866207058384979  
1703982697 41.7 71.78 f866207058384979

---

<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: [22 February 2024 17:24 UTC](https://discourse.nodered.org/t/writing-array-of-arrays-data-to-influx/85756/2 "2024-02-22T17:24:10Z")

</div>

Welcome to the forum @NewHopeFarm

Why don't you send each value as it is received, rather than saving them up?

You need to make an array of arrays, where each element is as you have shown for your single reading, with the addition of a `time` field which must contain the time value in (I think) the units specified for Time Precision (default is millisecs). So for the example you show it should be an array of 7 objects, where each element is an array of 2 objects, the first being the fields and the second being the tags. So each of the 7 elements will be something like

```auto
[
    {
        "TempLower": 42.7,
        "time": 1703982697000
    },{
        "hive": "f866207058384979"
    }
]

```

The example you show has the time as a string, it must be a number, and it looks like nanosecs, whereas I am pretty sure it should be msec as that is how you have configured the precision. I am not certain about that though as it may also be down to the database settings. If the times come out all wrong try nanosecs. Oddly the other example you show  
1703981797 42.7 71.06 f866207058384979  
appears to be in seconds.

---

<div class="post-metadata">

### Author: ![NewHopeFarm](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@NewHopeFarm](https://discourse.nodered.org/u/NewHopeFarm)
#### Post date: [22 February 2024 18:40 UTC](https://discourse.nodered.org/t/writing-array-of-arrays-data-to-influx/85756/3 "2024-02-22T18:40:44Z")

</div>

Thanks Colin for the reply!

> Why don't you send each value as it is received, rather than saving them up?

The device is a cellular device and as a battery saving mechanism, it wakes up long enough to take a temperature reading, store the data, go back into sleep mode. Then every so often it does a batch upload of the data. This allows me to take frequent temperature readings without killing the battery to make a cellular connection.

> So for the example you show it should be an array of 7 objects, where each element is an array of 2 objects, the first being the fields and the second being the tags.

I think that is where I am struggling, is how to create an array with two objects. Is there any sample javascript code that would demonstrate that?

Here is a snippet of code that is creating the array:

```auto
var imei = 12345678910
var array=[]
var bytes="0x"
for (var i=1;i<data1.length;i++){ //Loop through array to get data
    var temperature=((parseInt(bytes+data1[i].substring(4,8))/10)*9/5)+32 // 4,5,6,7 
    var time1=parseInt(bytes+data1[i].substring(16,)) // 16 until end
    var obj={
        'hive':imei,
        'TempLower':temperature,
        'time':time1+'000000000'
    }
    array[i]=obj
}
msg.payload=[array]

```

> am pretty sure it should be msec as that is how you have configured the precision.

Yea, I'm adding 0's so that InfluxDB doesn't complain. I probably didn't configure the InfluxDB correctly. It works for now. If I can get the data written, I can see about fixing time precision.

Matthew

---

<div class="post-metadata">

### Author: ![NewHopeFarm](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@NewHopeFarm](https://discourse.nodered.org/u/NewHopeFarm)
#### Post date: [22 February 2024 19:16 UTC](https://discourse.nodered.org/t/writing-array-of-arrays-data-to-influx/85756/5 "2024-02-22T19:16:43Z")

</div>

Thanks Colin. Your guidance got me in the right direction and it is working now. After creating the array of array with 2 objects and then feeding that into a SPLIT function, it is working!!

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

Here is how I split it into 2 objects.

```auto
    var obj=[{
        'TempLower':ds18b201,
        'time':time1+'000000000'},
    {
        'hive':imei,
    }]
    array[i]=obj
}

```

---

<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: [22 February 2024 20:07 UTC](https://discourse.nodered.org/t/writing-array-of-arrays-data-to-influx/85756/6 "2024-02-22T20:07:44Z")

</div>

> [@NewHopeFarm](#):
>
> `msg.payload=[array]`

Change that to  
`msg.payload=array`  
and remove the split node.

---

<div class="post-metadata">

### Author: ![NewHopeFarm](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@NewHopeFarm](https://discourse.nodered.org/u/NewHopeFarm)
#### Post date: [22 February 2024 20:42 UTC](https://discourse.nodered.org/t/writing-array-of-arrays-data-to-influx/85756/7 "2024-02-22T20:42:23Z")

</div>

Thanks! I couldn't figure out why I had 3 layers of arrays.

---

<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: [22 May 2024 20:42 UTC](https://discourse.nodered.org/t/writing-array-of-arrays-data-to-influx/85756/8 "2024-05-22T20:42:38Z")

</div>

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