Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A function node for Node-RED that assembles data from WoTKit to create an HTTP request.
// Return an error if data is empty.
if (msg.payload.length === 0){
msg.payload = {"error":"no data found"};
return msg;
}
// Get the values as an array
var values = []
var sensors = []
msg.payload.map(function(element){
values.push(element.value)
if (sensors.indexOf(element.sensor_name) == -1 && element.sensor_name !== null)
sensors.push(element.sensor_name)
});
// Compute the average
var average = values.reduce(function(previous, current, index, array) {
return previous + current;
}) / values.length;
// Assemble response and return
msg.payload = {'average_value': average, 'sensors_parsed':sensors, 'data_points' : values.length }
return msg;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.