Reading from an MQTT array

Hi, I am very new to Telegraf and would like some suggestions about how to ingest JSON arrays of seismic data into Influxdb 2.0.

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 JSON msg via MQTT, which would be then received by Telegraf using the following topic:

accel/VEHIGE9820/data, where VEHIGE9820 is the device_id (tag for influxdb). Perhaps we could a wildcard to catch all the devices with accel/*/data ?

Here is a sample msg:

{"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]}

Using node-red I am able to convert this into individual messages, by looping on the array of each axis, and adding a timestamp which is uses 1/32 of the last second in epoch time. However I would prefer to have this done via Telegraf as I feel it would be more efficient.

Here is the node-red function code I use to make the individual timestamped messages for influxdb (works fine):

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]) / 1000;
    var accely = (msg.payload.y[i]) / 1000;
    var accelz = (msg.payload.z[i]) / 1000;
    var time = timestamp - 1000 + (i/125 * 1000);
    newMsg.payload = [{ time: time, x: accelx, y: accely, z: accelz }, { tag: deviceid }];
    node.send(newMsg);
}
return null;

any help on how to do this in Telegraf (if its even possible!) would be very appreciated !

Hello @seismicdatalogger,
Yes! You can use the Telegraf file plugin.

And the following:

Then you could also use the execd processor plugin to process that data.

Or you could just use the execd input plugin and just use your script.

On second thought I’d probably go with the second option. I think from a UX perspective that would be the easiest.