stackhero-io / node-red-contrib-stackhero-influxdb-v2

Node-RED node to read and write to an InfluxDB v2 database.
31 stars 7 forks source link

How to query multiple fields from influxdb-v2 using stackhero query node #19

Open robertotarga opened 1 year ago

robertotarga commented 1 year ago

Hi, thank you for having developed such a nice node! I'm using the stackhero-influxdbv2 node version 1.0.4 with Node-red v. 3.0.2. with influxdb v.2.5 on a windows 11 pro pc.

Following your code example it's been very easy to write a measurement with multiple fields, but I struggle in querying more than one field in order to plot them in a table or a multiline graph for example.

Using this code I get "records" correctly:

msg.topic = [ 'from(bucket: "domotica") |> range(start: -3d)', ' |> filter(fn: (r) => r._measurement == "energy")', ' |> filter(fn: (r) => r._field == "consumption")' ].join('\n');; return msg;

but adding another r.field such as r._field == "production"):

msg.topic = [ 'from(bucket: "domotica") |> range(start: -3d)', ' |> filter(fn: (r) => r._measurement == "energy")', ' |> filter(fn: (r) => r._field == "consumption")', ' |> filter(fn: (r) => r._field == "production")' ].join('\n');; return msg;

returns an empty object!

Can you please provide the correct syntax to be able to query more than one field in the same response msg from influxdb?

robertotarga commented 1 year ago

Since I got no reply I investigated the flux syntax and find out the solution with more than one field and to assemble the output of multiple fields in one row using pivot() function as in the example:

msg.topic = [ 'from(bucket: "domotica") |> range(start: -3d)', ' |> filter(fn: (r) => r._measurement == "energy" and (r._field == "consumption" or r._field == "production"))', ' |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")' ].join('\n');; return msg;