resolver101
resolver101

Reputation: 2255

node red function and export to CSV

I have the raspberry pi and sense hat. I wish to extract acceleration z,y,x,Temperature and Pressure and time to csv. However, the code I've written does not do what i want it to do. I was expecting the data to leave the function in an array but the debug window shows them as separate objects. How do i get acceleration z,y,x and time to leave as one array so i can export to CSV?

    29/03/2017, 17:17:23node: abf1ef08.31a6f
msg : Object
object
payload: 0.9487
_msgid: "ae0db049.020f6"
29/03/2017, 17:17:23node: abf1ef08.31a6f
msg : Object
object
payload: 0.2781
_msgid: "ae0db049.020f6"
29/03/2017, 17:17:23node: abf1ef08.31a6f
msg : Object
object
payload: 0.1491
_msgid: "ae0db049.020f6"
29/03/2017, 17:17:23node: abf1ef08.31a6f
msg : Date
"Wed Mar 29 2017 16:17:25 GMT+0000 (UTC)

Here's the function code :

var msgAccelZ,msgAccelX,msgAccelY,msgTemperature,msgPressure,msgCurrentTime;
if (msg.topic === 'motion') {
    msgAccelZ = { payload: msg.payload.acceleration.z, };
    msgAccelX = { payload: msg.payload.acceleration.x, };
    msgAccelY = { payload: msg.payload.acceleration.y, };
} else if (msg.topic === 'environment') {
    msgTemperature = { payload: msg.payload.temperature };
    msgPressure = { payload: msg.payload.pressure };
}
msgCurrentTime = new Date();
return [ [msgAccelZ, msgAccelX, msgAccelY,msgCurrentTime] ,[msgTemperature, msgPressure, msgCurrentTime] ];

Upvotes: 0

Views: 1446

Answers (1)

knolleary
knolleary

Reputation: 10117

A Function needs to return a message object, not just a raw value.

By convention, you'd put the data you want to return under msg.payload:

msg.payload = [ [msgAccelZ, msgAccelX, msgAccelY,msgCurrentTime] ,[msgTemperature, msgPressure, msgCurrentTime] ];
return msg;

The documentation for the Function node explains in more detail: http://nodered.org/docs/writing-functions

Upvotes: 1

Related Questions