Writing Node Parameters Dynamically

Hi all. I'm trying to write multiple values to influx DB through a single node. What confuses me about the node-red-contrib-influxdb batch node is that I'm not sure where to specify the database I want to write to, so I'm trying a different method. Here is my example flow where I try to write multiple values to influx using a single output node:

[
    {
        "id": "e6665605.651228",
        "type": "inject",
        "z": "67579b26.28b6e4",
        "name": "1 minute inject",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 320,
        "y": 180,
        "wires": [
            [
                "90085c22.e39ae"
            ]
        ]
    },
    {
        "id": "90085c22.e39ae",
        "type": "MSSQL",
        "z": "67579b26.28b6e4",
        "mssqlCN": "ee093d52.6f53a",
        "name": "Read from myTable",
        "query": "select top 1 DATEDIFF(s, '1970-01-01 00:00:00', timestamp) as t_date, tag1, tag2, tag3 from [server].database.[dbo].[table]\nwhere not (tag1 is null OR tag2 is null OR tag3 is null)\norder by timestamp desc",
        "outField": "payload",
        "x": 580,
        "y": 180,
        "wires": [
            [
                "5bbe5a5c.451874"
            ]
        ]
    },
    {
        "id": "5bbe5a5c.451874",
        "type": "function",
        "z": "67579b26.28b6e4",
        "name": "Prepare InfluxDB Writes",
        "func": "time = (msg.payload[0].t_date-7200)*1000;\ntag1 = msg.payload[0].tag1;\ntag2 = msg.payload[0].tag2;\ntag3 = msg.payload[0].tag3;\n\nmsg.payload = [\n     {\n        measurement: \"my_tag1\",\n        value: tag1\n    },\n    {\n        measurement: \"my_tag2\",\n        value: tag1\n    },\n    {\n        measurement: \"my_tag3\",\n        value: tag1\n    },\n]\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 850,
        "y": 180,
        "wires": [
            [
                "53977f30.eea18"
            ]
        ]
    },
    {
        "id": "53977f30.eea18",
        "type": "split",
        "z": "67579b26.28b6e4",
        "name": "",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "payload",
        "x": 1030,
        "y": 380,
        "wires": [
            [
                "c9f0a383.e9153",
                "5f23740.255ad8c"
            ]
        ]
    },
    {
        "id": "c9f0a383.e9153",
        "type": "debug",
        "z": "67579b26.28b6e4",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1290,
        "y": 440,
        "wires": []
    },
    {
        "id": "5f23740.255ad8c",
        "type": "influxdb out",
        "z": "67579b26.28b6e4",
        "influxdb": "",
        "name": "",
        "measurement": "",
        "precision": "",
        "retentionPolicy": "",
        "database": "mydatabase",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "organisation",
        "bucket": "bucket",
        "x": 1300,
        "y": 380,
        "wires": []
    },
    {
        "id": "ee093d52.6f53a",
        "type": "MSSQL-CN",
        "tdsVersion": "7_4",
        "name": "My Server",
        "server": "address",
        "port": "",
        "encyption": false,
        "database": "database",
        "useUTC": true,
        "connectTimeout": "15000",
        "requestTimeout": "15000",
        "cancelTimeout": "5000",
        "pool": "5"
    }
]

What I want to do, is to set the "measurement" parameter of the influx output node dynamically based on the value of "measurement" in my object that I'm splitting. So it should write all three of my tags to the influxDB database indvidually. Is that possible?

It is in the same place in the Batch nodes as for the Influx Out node, in the Server config

image

In the normal Out node you can certainly set the Measurement property in the message. Is it not working? If not then show us the content of the messages you are sending (use a Debug node set to show Complete Message).

Ah, I'm using 1.8flux. When I try to write using 1.x I get Error: getaddrinfo EAI_AGAIN http http:80. The normal output node does work, but I need to specify the measurement in the node itself as shown belwo:

image

If I don't specify a measurement it gives me an error asking me to specify the measurement. When I try to write using the single output node, I have the following flowing to debug:

{"measurement":"my_tag1","value":123}
{"measurement":"my_tag2","value":456}
{"measurement":"my_tag3","value":789}

Do you want to use Flux? If not then select 1.x type (which includes 1.8 not using Flux).

Is that in msg.payload? I suggested setting the debug node to show Complete Message for a reason - because the measurement should be in msg.measurement not msg.payload.measurement

Do the three values represent the same thing but associated with something else (my_tagn)? For example it might be bedroom temperature, office temperature, and temperatures for other rooms. If so then you should send them all to the same measurement (room_temperatures for example) and use an influx Tag to identify the room. Then in the query you would use where room = "theroom" in the query to select the appropriate one.

Thanks, I understand not that I need to write to msg.measurement. Also, I'll change things up to make them all write to the same tag. I now write the following msg object to the influx node:

measurement = msg.payload.measurement
value = msg.payload.value
time = msg.payload.timestamp

msg = {
    measurement: measurement,
    fields: {
        value: value
    },
    timestamp: time
}

I check the object in a debug node and it looks right, like this (this is the entire msg object):

{"measurement":"my_tag1","fields":{"value":123},"timestamp":1612361772000,"_msgid":"8d4c3793.db22d8"}

Now, the influx output nodes give me no errors, but neither does it write the values to the desired measurement. Is there something wrong with my object structure? I've also tried this without the fields object..

If you want to specify a timestamp use the batch node.
However, if you don't need to specify a timestamp then you still haven't got it right.
From the docs
" If msg.payload is a string, number, or boolean, it will be written as a single value to the specified measurement (called value )."

So to simple write a value to measurement the message should be
{measurement: "my_tag1", payload: 123}

1 Like

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