Help with Javascript on Function Node

the sensor payloads appear to be {"value": 0} so to keep the values consistant best use msg.payload.*.value

As you can see in the debug image shown above, defined payloads are objects and the ones set to 0 are not.

I would probably go with -

const keys = ["ProgramNumber",
var output = [];
for(const V of keys){
    output.push(typeof msg.payload[V] === "undefined" ? 0 : msg.payload[V].value);
msg.topic = `INSERT INTO SSreadings (${keys}) VALUES (${output});`
return msg;
if (typeof msg.payload.ProgramNumber === "undefined") msg.payload.ProgramNumber = {"value":0};
if (typeof msg.payload.NumOfLeaks === "undefined") msg.payload.NumOfLeaks = {"value":0};
if (typeof msg.payload.FsFlow === "undefined") msg.payload.FsFlow = {"value":0};
if (typeof msg.payload.MsFlow === "undefined") msg.payload.MsFlow = {"value":0};
if (typeof msg.payload.Ds1Flow === "undefined") msg.payload.Ds1Flow = {"value":0};
if (typeof msg.payload.SpruFlow === "undefined") msg.payload.SpruFlow = {"value":0};
if (typeof msg.payload.Jc1Flow === "undefined") msg.payload.Jc1Flow = {"value":0};
if (typeof msg.payload.Jc2Flow === "undefined") msg.payload.Jc2Flow = {"value":0};
if (typeof msg.payload.Jc3Flow === "undefined") msg.payload.Jc3Flow = {"value":0};
if (typeof msg.payload.Jc4Flow === "undefined") msg.payload.Jc4Flow = {"value":0};
if (typeof msg.payload.FacFlow === "undefined") msg.payload.FacFlow = {"value":0};
if (typeof msg.payload.JcFlow === "undefined") msg.payload.JcFlow = {"value":0};
if (typeof msg.payload.FacPress === "undefined") msg.payload.FacPress = {"value":0};
if (typeof msg.payload.JcPress === "undefined") msg.payload.JcPress = {"value":0};
if (typeof msg.payload.Ds2Flow === "undefined") msg.payload.Ds2Flow = {"value":0};

msg.topic = `INSERT INTO SSreadings (ProgramNumber, NumOfLeaks, FsFlow, MsFlow, Ds1Flow, SpruFlow, Jc1Flow, Jc2Flow, Jc3Flow, Jc4Flow, FacFlow, JcFlow, FacPress, JcPress, Ds2Flow) VALUES (${msg.payload.ProgramNumber.value}, ${msg.payload.NumOfLeaks.value}, ${msg.payload.FsFlow.value}, ${msg.payload.MsFlow.value}, ${msg.payload.Ds1Flow.value}, ${msg.payload.SpruFlow.value}, ${msg.payload.Jc1Flow.value}, ${msg.payload.Jc2Flow.value}, ${msg.payload.Jc3Flow.value}, ${msg.payload.Jc4Flow.value}, ${msg.payload.FacFlow.value}, ${msg.payload.JcFlow.value}, ${msg.payload.FacPress.value}, ${msg.payload.JcPress.value}, ${msg.payload.Ds2Flow.value});`
return msg;

Everything seems to work using the above code. I am going to run it through the machines process and see if it works. I will Update this thread. Thanks for all the help, I appreciate it!

The above code worked, Thanks for all the Help!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.