Specific timestamp from Node-Red to InfluxDB shell version 1.8.10

Hi community,

my first time in the forum since so far I was able to help myself (also thanks to the forum) but my current task is kind of challenging. The basic idea is kind of simple and I will explain on a minimum example: With Node-RED (using v4.0.2) I want to create a inject-pushed function, that gives a value (later different values) with a specific timestamp (defined by myself) to the influx Database (InfluxDB shell version 1.8.10) and get visualized by Grafana. The timestamp is always now+10s, so that the values are "already" in the dashboard.

[
    {
        "id": "2de6969d2c637700",
        "type": "debug",
        "z": "464b0d4053eb7bea",
        "name": "debug 185",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 690,
        "y": 1120,
        "wires": []
    },
    {
        "id": "3dc325a70c8f5e9a",
        "type": "influxdb out",
        "z": "464b0d4053eb7bea",
        "influxdb": "50fec97b649b4aa5",
        "name": "temperatur",
        "measurement": "temperatur",
        "precision": "",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "organisation",
        "bucket": "bucket",
        "x": 690,
        "y": 1160,
        "wires": []
    },
    {
        "id": "2c7a4cc6b5e1cfc6",
        "type": "inject",
        "z": "464b0d4053eb7bea",
        "name": "",
        "props": [],
        "repeat": "1",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 170,
        "y": 1120,
        "wires": [
            [
                "d517ee8d117e1942"
            ]
        ]
    },
    {
        "id": "d517ee8d117e1942",
        "type": "function",
        "z": "464b0d4053eb7bea",
        "name": "function 23",
        "func": "let futureTimestamp = Date.now() + 60000;  // Beispiel-Zeitstempel in der Zukunft (in Millisekunden)\nlet timestampNs = futureTimestamp * 1000000; // Umrechnung in Nanosekunden\n\n// Den Payload im Line Protocol Format fĂĽr InfluxDB setzen\nmsg.payload = -45;\nmsg.timestamp = timestampNs;\n\n// msg Objekt zurĂĽckgeben\nreturn msg;\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 410,
        "y": 1120,
        "wires": [
            [
                "2de6969d2c637700",
                "3dc325a70c8f5e9a"
            ]
        ]
    },
    {
        "id": "50fec97b649b4aa5",
        "type": "influxdb",
        "hostname": "127.0.0.1",
        "port": "8086",
        "protocol": "http",
        "database": "enasensors",
        "name": "enasensors",
        "usetls": false,
        "tls": "",
        "influxdbVersion": "1.x",
        "url": "http://localhost:8086",
        "timeout": "10",
        "rejectUnauthorized": true
    }
]

Everything but the timestamp-transmission works. I really don't know how to format the payload, so that InfluxDB take my timestamp and not the systems one.

I am thankful for every hint and advice. So far I tested a lot but make no step forward.

Best regards and thanks in advance.

Welcome to the forum @King-of-Noobs

Try

// Den Payload im Line Protocol Format fĂĽr InfluxDB setzen
msg.payload = {value: -45, time:  timestampNs}

I am not certain, but I think that is right. It is a bit confusing, the timestamp field is actually called time.

In addition, if in the influx node, in the advanced options, you set the precision to millisecs then you won't need to convert to nanosecs.
Finally, in modern javascript you generally don't need the semi colons, unless you put more than one statement on a line, which isn't a good idea anyway.

1 Like

Hi Colin,

first of all: thank you very much for your quick reply! I tried it and it works, thanks a lot. For all who are looking for a similar approach I post the working code below. Best regards!

[
    {
        "id": "b3d5eda1ee434aef",
        "type": "inject",
        "z": "53acd9b4e7811135",
        "name": "",
        "props": [],
        "repeat": "1",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 1170,
        "y": 860,
        "wires": [
            [
                "a8b5d292c778eeb7"
            ]
        ]
    },
    {
        "id": "42ce1c002c4ebbea",
        "type": "debug",
        "z": "53acd9b4e7811135",
        "name": "debug 188",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1650,
        "y": 860,
        "wires": []
    },
    {
        "id": "a8b5d292c778eeb7",
        "type": "function",
        "z": "53acd9b4e7811135",
        "name": "function 22",
        "func": "function getFutureTimestamp(seconds) {\n    const futureDate = new Date(Date.now() + (seconds * 1000));\n    return futureDate.getTime(); \n}\n\nconst futureTimestamp = getFutureTimestamp(10);\n\nmsg.payload = {value:-45, time: futureTimestamp}\n\nreturn msg;\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1410,
        "y": 860,
        "wires": [
            [
                "42ce1c002c4ebbea",
                "f14062a41395a561"
            ]
        ]
    },
    {
        "id": "f14062a41395a561",
        "type": "influxdb out",
        "z": "53acd9b4e7811135",
        "influxdb": "50fec97b649b4aa5",
        "name": "temperatur",
        "measurement": "temperatur",
        "precision": "ms",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "organisation",
        "bucket": "bucket",
        "x": 1650,
        "y": 820,
        "wires": []
    },
    {
        "id": "50fec97b649b4aa5",
        "type": "influxdb",
        "hostname": "127.0.0.1",
        "port": "8086",
        "protocol": "http",
        "database": "enasensors",
        "name": "enasensors",
        "usetls": false,
        "tls": "",
        "influxdbVersion": "1.x",
        "url": "http://localhost:8086",
        "timeout": "10",
        "rejectUnauthorized": true
    }
]

Just one small point.

function getFutureTimestamp(seconds) {
    var futureDate = new Date(Date.now() + (seconds * 1000));
    return futureDate.getTime();
}

var futureTimestamp = getFutureTimestamp(10);

msg.payload = {value:-45, time: futureTimestamp}

return msg;

Don't use var, use let or const. Both of those vars should be const.

Thanks! I fixed it in the solution

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