# Splitting arrays from an accelerometer into small records for Influxdb

**URL:** <https://discourse.nodered.org/t/splitting-arrays-from-an-accelerometer-into-small-records-for-influxdb/77252>\
**Category:** General\
**Created:** [5 April 2023 00:45 UTC](https://discourse.nodered.org/t/splitting-arrays-from-an-accelerometer-into-small-records-for-influxdb/77252 "2023-04-05T00:45:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![seismo](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@seismo](https://discourse.nodered.org/u/seismo)\
**Post date:** [5 April 2023 00:45 UTC](https://discourse.nodered.org/t/splitting-arrays-from-an-accelerometer-into-small-records-for-influxdb/77252/1 "2023-04-05T00:45:37Z")

</div>

Hi, I am trying to convert the MQTT msg below into separate records for the ingest InfluxDB node:

```auto
{"device_id":"VEHIGE9820","sr":32,"device_t":1680654643559,
"x":[324,294,358,230,279,332,385,490,381,362,290,336,328,275,370,411,543,385,306,234,475,426,396,298,264,211,309,298,400,362,306,283],
"y":[155,143,181,324,234,166,260,362,207,204,302,223,226,207,234,136,358,396,298,306,260,321,226,181,449,290,109,256,91,339,219,181],
"z":[2150,2071,2184,2041,2018,2007,2033,2139,2082,2029,2003,2093,2154,1901,2018,2010,2056,2188,2161,2041,2124,2044,2135,2105,2108,2097,1897,2297,2033,1942,2154,2071]}

```

This sensor data arrives every second from an accelerometer that buffers 32 samples per second for all 3 axis (`x`, `y` and `z`). It registers the timestamp `device_t` when it grabs the last of those samples, then sends the mqtt msg.

In order to plot these msgs out in grafana, I need to have individual records of each 1/32s containing 3 axis values.

Therefore I need 32 records written to the influxdb node each sec, eg (`device_id`, `x1`, `y1`, `z1`, `time1`), (`device_id`, `x2`, `y2`, `z2`, `time2`)...(`device_id`, `x32`, `y32`, `z32`, `time32`), etc, where `time1` is the device time second (`device_t`) / 32.

I hope this makes sense so far!. I am having issues creating a node that can split the msg into these individual msgs with their corresponding times... Any help or pointers would really help me out! thanks.

---

<div class="post-metadata">

**Author:** ![seismo](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@seismo](https://discourse.nodered.org/u/seismo)\
**Post date:** [5 April 2023 03:03 UTC](https://discourse.nodered.org/t/splitting-arrays-from-an-accelerometer-into-small-records-for-influxdb/77252/2 "2023-04-05T03:03:53Z")

</div>

I can get the individual msgs from this function node by iterating over the array:

```auto
var deviceid = msg.payload.device_id;
var timestamp = msg.payload.device_t;

for (var i = 0; i < msg.payload.x.length; i++) {
    var newMsg = {};
    var accelx = msg.payload.x[i];
    var accely = msg.payload.y[i];
    var accelz = msg.payload.z[i];
    newMsg.payload = { d: deviceid, t: timestamp, x: accelx, y: accely, z: accelz };
    node.send(newMsg);
}
return null;

```

but I still need to add the correct timestamp to each msg which takes away the last second and divides it by increments of 1/32...

---

<div class="post-metadata">

**Author:** ![seismo](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@seismo](https://discourse.nodered.org/u/seismo)\
**Post date:** [5 April 2023 03:13 UTC](https://discourse.nodered.org/t/splitting-arrays-from-an-accelerometer-into-small-records-for-influxdb/77252/3 "2023-04-05T03:13:42Z")

</div>

ok this is my best effort, which spits out something that looks ok, but pls let me know if my maths works out..

```plaintext
var timestamp = msg.payload.device_t;

for (var i = 0; i < msg.payload.x.length; i++) {
    var newMsg = {};
    var accelx = msg.payload.x[i];
    var accely = msg.payload.y[i];
    var accelz = msg.payload.z[i];
    var time = timestamp - 1000 + (i/32 * 1000);
    newMsg.payload = { d: deviceid, t: time, x: accelx, y: accely, z: accelz };
    node.send(newMsg);
}
return null;

```

---

<div class="post-metadata">

**Author:** ![seismo](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@seismo](https://discourse.nodered.org/u/seismo)\
**Post date:** [5 April 2023 07:04 UTC](https://discourse.nodered.org/t/splitting-arrays-from-an-accelerometer-into-small-records-for-influxdb/77252/4 "2023-04-05T07:04:10Z")

</div>

all done. i got there in the end.  
 ![Peek 2023-04-05 00-55](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/043ab593c3e29e49dfb3e2cbe97aa5c7a3e7501d.gif)

---

<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:** [4 June 2023 07:05 UTC](https://discourse.nodered.org/t/splitting-arrays-from-an-accelerometer-into-small-records-for-influxdb/77252/5 "2023-06-04T07:05:09Z")

</div>

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