Giter VIP home page Giter VIP logo

node-red-contrib-stackhero-influxdb-v2's Introduction

node-red-contrib-stackhero-influxdb-v2

Node-RED node to read and write to an InfluxDB v2 database.

Remember: if you like it, please star it! ๐Ÿฅฐ

Official repository: https://github.com/stackhero-io/node-red-contrib-stackhero-influxdb-v2

Sponsors

node-red-contrib-stackhero-influxdb-v2 is developed by Stackhero. If you are looking for powerful managed services, like InfluxDB, you should seriously consider Stackhero ๐Ÿค“

Usage

We have 2 nodes here. A write node, to send data to InfluxDB and a query node, to retrieve data using the Flux language.

Write data

The write node requires a payload object like this one:

msg.payload = {
  // You bucket
  // Optional (it can be defined in the node credentials settings)
  bucket: 'myBucket',

  // Precision of timestamp
  // Optional
  // Can be `ns` (nanoseconds),
  //        `us` (microseconds),
  //        `ms` (milliseconds),
  //        `s` (seconds).
  // The default is `ns`
  // Note: if you set the `timestamp` field to `Date.now()`, you have to set the `precision` to `ms`
  precision: 'ms',

  // Data to send to InfluxDB
  // Can be an array of objects or only one object
  data: [
    {
      measurement: 'machinerySensor',

      tags: {
        deviceId: 'gyh43',
        hardwareVersion: '1.0.2',
        softwareVersion: '2.5.1',
        location: 'factory-1'
      },

      fields: {
        temperature: 12,
        humidity: 46,
        vibrations: 18,
        batteryVoltage: 3.6
      },

      timestamp: Date.now()
    },

    // More data can be send here, simply re add an object
    // { ... },
  ]
};

return msg;

Example of the write node

Query data

The query node requires a topic string containing a Flux query.

msg.topic = 'from(bucket: "myBucket") |> range(start: -1h)';
return msg;

You can write multiple lines queries like this:

msg.topic = [
  'from(bucket: "myBucket")',
  '  |> range(start: -1d, stop: now)',
  '  |> filter(fn: (r) => r._measurement == "machinerySensor")',
  '  |> filter(fn: (r) => r._field == "vibrations")',
  '  |> aggregateWindow(every: 1h, fn: mean)',
  '  |> yield(name: "mean")',

  'from(bucket: "myBucket")',
  '  |> range(start: -1d, stop: now)',
  '  |> filter(fn: (r) => r._measurement == "machinerySensor")',
  '  |> filter(fn: (r) => r._field == "vibrations")',
  '  |> aggregateWindow(every: 1h, fn: max)',
  '  |> yield(name: "max")',
].join('\n');

return msg;

Example of the query node

node-red-contrib-stackhero-influxdb-v2's People

Contributors

bacto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

node-red-contrib-stackhero-influxdb-v2's Issues

int not possible

When adding a trailing "i" to an integer value I would expect that the influxdb field becomes an integer, but it turns out to be a string instead.
A trailing "i" should mark an integer according to the influxdb docu.

best,
fairtv

Query for value with strings return NaN

If i run a query for a field that contains strings i get a result as normal but "_value: NaN". If i change the field to a numeric it works. The strings are simple words added from a snmp lookup table inserted with telegraph. The query works fine in the Influx Data Explorer.

version 1.0.4

Error in function example code

In the multiple query example the range has an error in the stop time, it is shown as range(start: -24h, stop: now), this returns an error. The correct form is , range(start: -24h, stop: now())

Unable to provide DB token from file

Hi,

Would it be possible for you to modify the configuration node in a way, that would allow users to provide the DB token from a text file as well? This would most certainly be useful when using Node-Red from a Docker container, as this feature would allow people to use Docker Secrets for supplying tokens.

The implementation could very much look like what the default Node-Red MQTT config node has for providing certificates to allow connection encryption: if a checkbox is activated, the user is allowed to provide a file path in the text field, which in turn reads the sensitive data out from a text file.

Please consider adding this small feature, as it would truly be very useful. Thanks in advance.

Couln't write to influxdb with event trigger time timestamp

Hi Team, Greetings

I am trying to write to influxdb without timestamp it works. by default it adds node-red timestamp in influxdb when it writes.
query shows the timestamp of the node-red.

{
"_msgid": "df366b6bdb1b98a9",
"payload": {
"data": {
"measurement": "customer_a",
"tags": {
"device_id": "mydevice-name"
},
"fields": {
"cycle_on": 1,
},
}
},
"topic": "customer_a"
}

but when i assign a timestamp value from payload / manually and it fails to write to influxdb. no error, not written in influxdb, nothing happening. no clue at all. can someone share light on this please?
{
"_msgid": "df366b6bdb1b98a9",
"payload": {
"data": {
"measurement": "customer_a",
"tags": {
"device_id": "mydevice-name"
},
"fields": {
"cycle_on": 1,
},
"timestamp": 1636097507
}
},
"topic": "customer_a"
}

Please add license

Several people have already forked it, maybe with permission. But could you consider adding a license, e.g., MIT?

Support of strings with whitespaces

Hi there,
thanks a lot for your node! Have you ever used a string including whitespaces for tags or measurement etc.?
I do not get it to work. According to Influx Line Protocol whitespaces, equal sign, comma... need to be escaped with a backslash in order to not mess up the command. But even if I put a backslash into the tag string that is put into the write node it does not work. According to the error message the backslash used for escaping is apparently removed at some point before the whole command string is given to InfluxDB, so in the end there is just the whitespace in the tag part of the command string, which is then interpreted by InfluxDB that the fieldKey already starts.

Do you actively remove that backslash escape at some point when putting the tagKey string into the whole command string?

Example:
data: [
{
measurement: 'StringTest',

  tags: {
    deviceId: "Text\ withSpace"
  },

  fields: {
    test: 22
  },
},

]

Best Regards,
Yenzor

TypeError: text.replace is not a function

The following is the payload that I send and the error that I get:

{
  "payload": {
    "bucket": "weather-data",
    "precision": "ms",
    "data": [
      {
        "measurement": "weatherdata",
        "tags": {
          "device_sn": "SK-00000732",
          "firmware_revision": 43,
          "report_interval": 1,
          "wind_sample_interval": 3,
          "precipitation_type": 0
        },
        "fields": {
          "wind_speed": 0.89,
          "wind_lull": 0,
          "wind_gust": 1.74,
          "wind_direction": 144,
          "uv_index": 0,
          "illuminance": 0,
          "solar_radiation": 0,
          "battery": 3.25,
          "rain_accumulated": 0
        },
        "timestamp": 1615224793257
      }
    ]
  },
  "_session": {
    "type": "websocket",
    "id": "315a89d2.d2f9f6"
  },
  "_msgid": "ea8958bf.3574c8",
  "error": {
    "message": "TypeError: text.replace is not a function",
    "source": {
      "id": "ceb1cd06.6c177",
      "type": "Stackhero-InfluxDB-v2-write",
      "name": "influxdb2write",
      "count": 1
    },
    "stack": "TypeError: text.replace is not a function\n    at escape (/data/node_modules/influxdb-v2/src/index.js:101:25)\n    at Object.keys.map.tagKey (/data/node_modules/influxdb-v2/src/index.js:107:67)\n    at Array.map (<anonymous>)\n    at lines.map (/data/node_modules/influxdb-v2/src/index.js:107:12)\n    at Array.map (<anonymous>)\n    at Influxdb.write (/data/node_modules/influxdb-v2/src/index.js:95:8)\n    at InfluxDbV2Node.write (/data/node_modules/node-red-contrib-stackhero-influxdb-v2/src/main.js:28:34)\n    at process._tickCallback (internal/process/next_tick.js:68:7)"
  }
}

write node error since upgrade to 1.0.4

Hi I'm facing write error from existing function (reading Homematic sensors and writing it to influxdb V.2.0.3)

Node function to prepare values:
var pl = { bucket: 'homematic', precision: 's', data: [{ measurement: msg.room ? msg.room.replace(/ /g, '\\ ') : 'undefined', tags: { 'device': msg.device, 'name': msg.deviceName.replace(/ /g, '\\ '), 'deviceType': msg.deviceType }, fields: {}, timestamp: Math.round(msg.ts / 1000) }] } pl.data[0]['fields'][msg.datapoint] = msg.payload; msg.payload = pl; return msg;

returns following json to the write node:
Schlafzimmer Temperatur.TEMPERATURE : msg.payload : Object object bucket: "homematic" precision: "s" data: array[1] 0: object measurement: "Schlafzimmer" tags: object device: "OEQ0128863" name: "Schlafzimmer\ Temperatur" deviceType: "HM-WDS40-TH-I-2" fields: object TEMPERATURE: 25.2 timestamp: 1611741808

this gives now the following errors:

"Error: 400 on http://xxxx:xxxx/api/v2/query?org=xxx: {"code":"invalid","message":"compilation failed: error at @1:25-1:26: invalid statement: :"}"
And
Error: 400 on http://xxxx:xxxx/api/v2/query?org=xxx: {"code":"invalid","message":"error @1:1-1:11: undefined identifier Wohnzimmer"}

thanks in advance to correct that,
Best regards
Thomas

Problems writing to DB after building object using a for-loop

As the title suggest, I'm having trouble writing to the database when using a for-loop to construct the object that I want to send. If I write out the same data one-by-one, it works like it should.
When I debug the output from my function, it is of the same format as another function where I do not use for-loop. Also the stackhero influxdb-V2 write node does give an output with empty payload (no error).
Also, if the measurement does not exist in the bucket, it will be created by the write node, so it is sending something to the DB.
This is the code used for constructing in a function node, the incoming msg.payload is an array of 24 objects. Each object consists of energy prices valid for one hour, and a timestamp to define what hour it is valid from (output from node nordpool-api-plus). This also means I will be writing to the past, and also to the future in the database.

`var dataset = [];

for (i = 0; i < msg.payload.length; i++) {
dataset.push(
{
measurement: 'technical',
tags: {
location: 'power'
},
fields: {
price: (msg.payload[i].price / 1000)
},
timestamp: Date.parse(msg.payload[i].timestamp)
}
)
}

msg.payload = {
bucket: 'test',
presicion: 'ms',
data: dataset
}
return msg;`

Disable certificate validation

Hello,
If the certificate used for the influxdb server is self signed, then the stackhero cannot connect as it tries to validate the certificate and it fails.
There should be a flag to disable this check.

Best

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

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?

Uncaught Exception:TypeError: Cannot create property 'data' on number ...

Hello, we tried the node to connect to influxdb version 2 on our Bosch Rexroth ctrlX Hardware (Linux Ubuntu Core arm64). Unfortunately right after deploy, the Node.js process crashes caused by an Uncaught Exception of your node.

It seems to be caused by some internal conversions. Please could you provide us a fix for this or give us a hint if there's a workaround. Thank you in advance

Please see the callstack for details:

...
2022-12-07T09:17:56Z ctrlx-node-red.node-red[11962]: 7 Dec 09:17:56 - [red] Uncaught Exception:
2022-12-07T09:17:56Z ctrlx-node-red.node-red[11962]: 7 Dec 09:17:56 - [error] TypeError: Cannot create property 'data' on number '11.6'
2022-12-07T09:17:56Z ctrlx-node-red.node-red[11962]: at InfluxDbV2NodeWrite._inputCallback (/var/snap/rexroth-solutions/common/solutions/DefaultSolution/configurations/appdata/node-RED/node_modules/node-red-contrib-stackhero-influxdb-v2/src/write.js:37:26)
2022-12-07T09:17:56Z ctrlx-node-red.node-red[11962]: at /snap/ctrlx-node-red/674/lib/node_modules/@node-red/runtime/lib/nodes/Node.js:210:26
2022-12-07T09:17:56Z ctrlx-node-red.node-red[11962]: at Object.trigger (/snap/ctrlx-node-red/674/lib/node_modules/@node-red/util/lib/hooks.js:166:13)
2022-12-07T09:17:56Z ctrlx-node-red.node-red[11962]: at InfluxDbV2NodeWrite.Node._emitInput (/snap/ctrlx-node-red/674/lib/node_modules/@node-red/runtime/lib/nodes/Node.js:202:11)
2022-12-07T09:17:56Z ctrlx-node-red.node-red[11962]: at InfluxDbV2NodeWrite.Node.emit (/snap/ctrlx-node-red/674/lib/node_modules/@node-red/runtime/lib/nodes/Node.js:186:25)
2022-12-07T09:17:56Z ctrlx-node-red.node-red[11962]: at InfluxDbV2NodeWrite.Node.receive (/snap/ctrlx-node-red/674/lib/node_modules/@node-red/runtime/lib/nodes/Node.js:485:10)
2022-12-07T09:17:56Z ctrlx-node-red.node-red[11962]: at Immediate. (/snap/ctrlx-node-red/674/lib/node_modules/@node-red/runtime/lib/flows/Flow.js:831:52)
2022-12-07T09:17:56Z ctrlx-node-red.node-red[11962]: at processImmediate (node:internal/timers:466:21)

unsupported input type for mean aggregate: string

I'm trying to insert string in Influxdb v2 (2.0.3)
When I send a msg, like:


var text = msg.payload.data;
var Appname = msg.payload.applicationName;
var Devapp = msg.payload.device;

msg.payload = {
bucket: 'Teststring',
precision: 'ms',
data: [
{
measurement: 'string',
tags: {
aplication: Appname,
device: Devapp
},
fields: {
gauge2 : "test"
},
timestamp: Date.now()
},
]
};
return msg;

I receive inside Influx the following msg: unsupported input type for mean aggregate: string
I tried in field format with
"gauge2":"test" but I hhave the same issue

2021-01-25_15h33_53

How can I use the import function?

My Flux query is:

msg.topic = [
    'import "experimental"',
    'import "date"',
    'htruncs = date.truncate(t: v.timeRangeStart, unit: 1d)',
    'htrunce = date.truncate(t: v.timeRangeStop, unit: 1d)',
    'hstart = experimental.subDuration(d: 0d, from: htrunce)',
    'hstop = experimental.addDuration(d: 1d, to: hstart)',
    'from(bucket: "ath01")',
    '    |> range(start: hstart, stop: hstop)',
    '    |> filter(fn: (r) => r["_measuremthent"] == "consumption")',
    '    |> filter(fn: (r) => r["_field"] == "watermeter")',
    '    |> sum()',
].join('\n');

TypeError: Cannot convert undefined or null to object

Trying to write to influxdb, but keep getting the TypeError: Cannot convert undefined or null to object

My payload

{"data":{"gps_as_of":1605132723,"heading":53,"latitude":31.099831,"longitude":5.113069,"native_latitude":51.099831,"native_location_supported":1,"native_longitude":5.113069,"native_type":"wgs","power":0,"shift_state":"P","speed":null,"timestamp":1605132724637}}

Is the problem the speed? which is set to null?

Are String Data Types for Field Values supported ?

Heya thanks for the node, been starting to move some of my data flows in node red to influxdb 2.0 on the cloud for testing, I am not sure if Strings are supported or not currently as a field value data type for this node?

I seem to be getting errors on strings but not numbers when trying to do a write operation

if I set the field set in the payload to something like this:
{"message":"WDTMODEMINIT"}
I get a 400 error message on the write node, the quotation mark's seem to be being stripped out (see below error msg from the node)

Error: 400 on https://us-west-2-1.aws.cloud2.influxdata.com:443/api/v2/write?org=xxxxxxx&bucket=xxxxxx&precision=s: {"code":"invalid","message":"unable to parse 'errors,customer=xxx,location=weatherstation,errorType=WDT message=WDTMODEMINIT 2009020403': invalid boolean"}

I have tried the exact same payload construct with a numeric value (as per below) and all is good, no errors
{"message":100}

Thanks !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.