Hello
ty, for the answer.
I want a for loop for JSONata .... I tried to use some functions (like $count to get the size of the array), but to no sucess.
What I want for my application is that the user can select as many variables as he wants for the chart (between 1 and 15 variables). So I am looking for something dynamic, read the size of the array / quantity of variables and format the information.
I started to do something in a simple function of NR.
//
// Format the InfluxDB results to match the charts JSON format
//
var series = ["1","2","3","4","5"];
var labels = ["Data Values"];
var data = "[[";
var thetime;
var arrSize = msg.payload.results[0].series[0].values.length;
node.warn("arrSize="+arrSize);
var arrValueSize = msg.payload.results[0].series[0].columns.length;
node.warn("arrValueSize="+arrValueSize);
for (var j = 1; j < arrValueSize; j++) {
for (var i = 0; i < arrSize; i++) {
thetime = Number(msg.payload.results[0].series[0].values[i][0]);
data += '{ "x":' + thetime + ', "y":' + msg.payload.results[0].series[0].values[i][j] + '}';
if (i < (arrSize - 1)) {
data += ","
}
}
if (j < (arrSize - 1)) {
data += "],["
} else {
data += "]]"
}
}
node.warn("Data="+data);
var jsondata = JSON.parse(data);
msg.payload = [{ "series": series, "data": jsondata, "labels": labels }];
msg.playload = data;
return msg;
That function works well if the size of the array is under 15, higher than 15 i got error "function : (error)
"SyntaxError: Unexpected end of JSON input""
The Data i get from influx its this:
{"_msgid":"6b3701fb.36058","payload":{"results":[{"statement_id":0,"series":[{"name":"Boiler3","columns":["time","mean_HTF_L3_INLET_Temp_BoilerSd","mean_HTF_L3_INLET_Pressure_BoilerSd","mean_HTF_L3_OUTLET_Pressure_BoilerSd","mean_HTF_L3_OUTLET_Temp_BoilerSd"],"values":[[1598192400,208.272,0,0,208.077],[1598193000,205.98083333333327,0,0,205.74450000000004],[1598193600,203.217,0,0,202.96050000000002],[1598194200,200.98450000000008,0,0,200.1555],[1598194800,207.1098333333333,0,0,197.33816666666672],[1598195400,216.7206666666667,0,0,194.43133333333344],[1598196000,219.60200000000006,0,0,194.80716666666666],[1598196600,226.62233333333333,0,0,200.04766666666666],[1598197200,239.0766666666667,0,0,205.7966666666667],[1598197800,253.5746666666666,0,0,211.71133333333333],[1598198400,265.5541666666668,0,0,220.43750000000003],[1598199000,275.61216666666655,0,0,232.17100000000002],[1598199600,272.9371666666667,0,0,244.71483333333327],[1598200200,270.986,0,0,253.78683333333328],[1598200800,269.4945,0,0,248.0783333333333],[1598201400,267.7300000000001,0,0,232.50483333333332],[1598202000,271.39616666666666,0,0,239.0506666666666],[1598202600,271.03650000000005,0,0,243.46950000000004],[1598203200,269.36116666666663,0,0,249.65983333333324],[1598203800,270.8678333333334,0,0,248.15099999999993],[1598204400,269.06666666666666,0,0,247.2495],[1598205000,270.2483333333334,0,0,247.95749999999995],[1598205600,269.67183333333327,0,0,243.35633333333334],[1598206200,269.93149999999997,0,0,244.4910000000001],[1598206800,269.84736842105264,0,0,244.53473684210525]]}]}]},"topic":"","query":"SELECT mean(HTF_L3_INLET_Temp_BoilerSd) AS mean_HTF_L3_INLET_Temp_BoilerSd, mean(HTF_L3_INLET_Pressure_BoilerSd) AS mean_HTF_L3_INLET_Pressure_BoilerSd, mean(HTF_L3_OUTLET_Pressure_BoilerSd) AS mean_HTF_L3_OUTLET_Pressure_BoilerSd, mean(HTF_L3_OUTLET_Temp_BoilerSd) AS mean_HTF_L3_OUTLET_Temp_BoilerSd FROM Boiler3 WHERE time > now() - 4h AND time < now() AND Grp='HTF_L3' AND Side='BOILER3' GROUP BY time(10m) FILL(null)"}
anyone have any idea ?
thanks