Home > Software design >  Thingsboard : Save multiple timeseries readings in one Save Timeseries Node Post
Thingsboard : Save multiple timeseries readings in one Save Timeseries Node Post

Time:11-09

I'm trying to save a series of timestamped timeseries readings that I get as an array, in one POST of the Save Timeseries Node. I get the values in the msg parameter of the transformation script node function, transform it to the expected format, and save them in the save timeseries node. After that, I intend to plot them in a line chart.

I've tried formatting the output JSON in two different ways but it always saves only the last value of the array. Is there any way to do this bulk saving?

Here are the 2 different OUTs of the save timeseries node:

[{
    "ts": 1636160453000,
    "reading": {
        "value": 536.5833333333334
    }
}, {
    "ts": 1636158641000,
    "reading": {
        "value": 538.4666666666667
    }
}, {
    "ts": 1636156829000,
    "reading": {
        "value": 547.9333333333333
    }
}, {
    "ts": 1636155019000,
    "reading": {
        "value": 523.4666666666667
    }
}, {
    "ts": 1636153207000,
    "reading": {
        "value": 549.8666666666667
    }
}, {
    "ts": 1636151455000,
    "reading": {
        "value": 516.0344827586207
    }
}, {
    "ts": 1636149643000,
    "reading": {
        "value": 500.26666666666665
    }
}, {
    "ts": 1636147831000,
    "reading": {
        "value": 496.56666666666666
    }
}, {
    "ts": 1636146020000,
    "reading": {
        "value": 521.7
    }
}, {
    "ts": 1636144210000,
    "reading": {
        "value": 543
    }
}]

***************************

[{
    "ts": 1636160453000,
    "reading": 588.5714285714286
}, {
    "ts": 1636158641000,
    "reading": 538.4666666666667
}, {
    "ts": 1636156829000,
    "reading": 547.9333333333333
}, {
    "ts": 1636155019000,
    "reading": 523.4666666666667
}, {
    "ts": 1636153207000,
    "reading": 549.8666666666667
}, {
    "ts": 1636151455000,
    "reading": 516.0344827586207
}, {
    "ts": 1636149643000,
    "reading": 500.26666666666665
}, {
    "ts": 1636147831000,
    "reading": 496.56666666666666
}, {
    "ts": 1636146020000,
    "reading": 521.7
}, {
    "ts": 1636144210000,
    "reading": 543
}]

Thank you!!

CodePudding user response:

Before your save timeseries node place a Transformation - Script node. These nodes can output a list of values that act as individual messages (assuming TB3.3.1).

In your case I'd suggest something like this

// Don't include these, I'm just including them to get this snippet working
var msg = [{"ts": 12345, "readings": { "value": 500}}, {"ts": 54321, "readings": { "value": 600}}]
var metadata = {"deviceName": "Example", "deviceType": "default", "ts": 100000}

// Include below this line
var result = []
msg.forEach(function (reading) {
  var newMsg = reading.readings;
  var newMeta = metadata
  
  newMeta.ts = reading.ts
  result.push({
    "msg": newMsg,
    "metadata": newMeta,
    "msgType": "POST_TELEMETRY_REQUEST"
  })
})

console.log(result) // Delete me
//return result // <<< Uncomment this line
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

This will send one message to you Save Timeseries node per reading and will add it with the correct timestamp.

I've used similar code in my solution, although I did not test this exact example prior to writing the above code so you may need to alter it slightly.

  • Related