Turning an array of acceleration values into individual records for influxdb

Hi there!

I am using Node-RED to take incoming MQTT msgs every second that contain values from an accelerometer. Each msg has an array of JSON objects that contain x,y,z arrays of 32 floats, as well as the device id. A sample msg with one second of accelerometer data may look like this:

{"d":{"device_id":"A8032A4DD5F0","traces":[{"x":[-0.03,-0.011,-0.019,0,-0.026,-0.023,0.091,-0.049,0.004,0.03,-0.023,-0.004,0.057,0.049,0.023,-0.008,-0.113,0.008,-0.026,-0.023,0.015,-0.026,-0.004,0,0.011,-0.045,0.004,-0.008,-0.124,-0.026,0.041,0],"y":[-0.207,-0.083,-0.147,-0.298,-0.253,-0.256,-0.158,-0.17,-0.147,-0.275,-0.287,-0.207,-0.26,-0.162,-0.241,-0.215,-0.162,-0.223,-0.207,-0.207,-0.219,-0.264,-0.26,-0.226,-0.215,-0.162,-0.181,-0.181,-0.207,-0.256,-0.162,-0.155],"z":[-0.17,-0.204,-0.2,0.011,-0.249,-0.17,-0.057,-0.204,-0.106,0.038,-0.17,-0.196,-0.185,-0.256,-0.079,-0.068,-0.072,-0.128,-0.136,-0.102,-0.091,-0.049,-0.109,-0.143,-0.038,-0.279,-0.147,-0.109,-0.004,-0.151,-0.26,-0.162]}]}}

I'm having trouble writing a function node that separate these values into 32 separate entries that can be written to an influxdb out node, where each entry would look something like:

traces,device_id=A8032A4DD5F0 x=-0.03 y=1.02 z=-3.23 1465839830100400200
(timestamp is created by influx on arrival)

Given that the accelerations have been buffered on the device for one second, each of the 32 array is actually 1/32s.

The difficulty im having is setting the time for each entry so that the first x,y,z is 32/32s, the next is 31/32s, then 30/32s etc.

Im not sure if that makes sense! please let me know any pointers.

Thank you,

Since you don't send the timestamp from the device you will need to use (Date.now()-1000) as your first record then add 31.25 to each one in the loop that generates your payloads.

Note, because you don't send timestamp from source, you are likely to see deviation in the timestamps between sets of data due to network latency.

1 Like

That makes sense. Thank you! I will try now :grinning:

You have not said, but you should use the influxdb batch node for this so they are all written in one go.

1 Like