Skip to content

Instantly share code, notes, and snippets.

@calderonroberto
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calderonroberto/2fc3f3eb5c30f3d1ac9c to your computer and use it in GitHub Desktop.
Save calderonroberto/2fc3f3eb5c30f3d1ac9c to your computer and use it in GitHub Desktop.
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