Time Stamp and Influx DB problem

I formatted the msg.payload as shown in the documentaqtion of influxdb node, but the opc node needs topic Id individually for each topic ID seperately (can't handle as a list of topic IDs all together :frowning: ), So have to handle message after message!

After getting the payload from OPC node, I have to format the message acc. to Influxdb Out Node! I am not sure how to collect/assemble all the messages coming out of OPC node, eventhough they come out individually (one by one, at diff. timestamps)!

Here is my Function to format msg.payload acc.to Influxdb out Node (as in the Homepage of the node):

timestamp = new Date().getTime() *1e6;
msg = {payload:{}}
function append_array(topic_array, meas_array, timestamp){
    var totalCount = topic_array.length;
    const appended_array = [];
    //append dicts to array
    for (var i = 0; i < totalCount; i++){
    	appended_array.push([{topic: topic_array[i],measurement: meas_array[i], time:timestamp}]);
        }
        return appended_array;
}

const topic_array = [
'NodeID;ns=3;s="ExGrafanaDb"."MachineState1"."Data"."Mode"."TimeProduction"',
'NodeID;ns=3;s="ExGrafanaDb"."MachineState1"."Data"."Mode"."TimeProductionStart"',
'NodeID;ns=3;s="ExGrafanaDb"."MachineState1"."Data"."Mode"."TimeProductionEnd"',
];
				
const meas_array = [
'ExGrafanaDbMachineState1DataModeTimeProduction',
'ExGrafanaDbMachineState1DataModeTimeProductionStart',
'ExGrafanaDbMachineState1DataModeTimeProductionEnd',

];

msg.payload = append_array(topic_array, meas_array, timestamp);
console.log('msg.payload:', msg.payload);

Output:

[Running] node "d:\Thangz\Projekte\VC\Java\influxdb_set_ts"
msg.payload: [
  [
    {
      topic: 'NodeID;ns=3;s="ExGrafanaDb"."MachineState1"."Data"."Mode"."TimeProduction"',
      measurement: 'ExGrafanaDbMachineState1DataModeTimeProduction',
      time: 1655716440354000000
    }
  ],
  [
    {
      topic: 'NodeID;ns=3;s="ExGrafanaDb"."MachineState1"."Data"."Mode"."TimeProductionStart"',
      measurement: 'ExGrafanaDbMachineState1DataModeTimeProductionStart',
      time: 1655716440354000000
    }
  ],
  [
    {
      topic: 'NodeID;ns=3;s="ExGrafanaDb"."MachineState1"."Data"."Mode"."TimeProductionEnd"',
      measurement: 'ExGrafanaDbMachineState1DataModeTimeProductionEnd',
      time: 1655716440354000000
    }
  ]
]

[Done] exited with code=0 in 0.116 seconds

Currently I am working on it, any helps would be appreciated!

If they come out of the OPC node one by one then send them to influx one by one.
Show us what is coming out of the OPC node.

I need to write all the messages with specific (or current_time) timestamp, so I can have uniform records/rows with same timestamp! If I process them one by one, it gets different timestamp each time it is processed/written to influxdb!

OPC Node Output:

20.6.2022, 12:46:10[node: OPC_node_output](http://10.12.5.4:1880/#)NodeID;ns=3;s="ExGrafanaDb"."MachineState1"."Data"."Mode"."TimeProduction" : msg : Object

object

topic: "NodeID;ns=3;s="ExGrafanaDb"."MachineState1"."Data"."Mode"."TimeProduction""

measurement: "ExGrafanaDbMachineState1DataModeTimeProduction"

timestamp: 1655721971460000000

_msgid: "6681f7220e0b6acb"

payload: 23371320

statusCode: object

serverTimestamp: "2022-06-20T10:46:11.729Z"

sourceTimestamp: "2022-06-20T10:46:11.729Z"

20.6.2022, 12:46:10[node: OPC_node_output](http://10.12.5.4:1880/#)NodeID;ns=3;s="ExGrafanaDb"."MachineState1"."Data"."Mode"."TimeProductionStart" : msg : Object

object

topic: "NodeID;ns=3;s="ExGrafanaDb"."MachineState1"."Data"."Mode"."TimeProductionStart""

measurement: "ExGrafanaDbMachineState1DataModeTimeProductionStart"

timestamp: 1655721971460000000

_msgid: "6681f7220e0b6acb"

payload: object

statusCode: object

serverTimestamp: "2022-06-20T10:46:11.732Z"

sourceTimestamp: "2022-06-20T10:46:11.732Z"

20.6.2022, 12:46:10[node: OPC_node_output](http://10.12.5.4:1880/#)NodeID;ns=3;s="ExGrafanaDb"."MachineState1"."Data"."Mode"."TimeProductionEnd" : msg : Object

object

topic: "NodeID;ns=3;s="ExGrafanaDb"."MachineState1"."Data"."Mode"."TimeProductionEnd""

measurement: "ExGrafanaDbMachineState1DataModeTimeProductionEnd"

timestamp: 1655721971460000000

_msgid: "6681f7220e0b6acb"

payload: object

statusCode: object

serverTimestamp: "2022-06-20T10:46:11.735Z"

sourceTimestamp: "2022-06-20T10:46:11.735Z"

20.6.2022, 12:46:10[node: test_db](http://10.12.5.4:1880/#)msg : error

"TypeError: points.forEach is not a function"

20.6.2022, 12:46:10[node: test_db](http://10.12.5.4:1880/#)msg : error

"TypeError: points.forEach is not a function"

20.6.2022, 12:46:10[node: test_db](http://10.12.5.4:1880/#)

msg : error

"TypeError: points.forEach is not a function"

Node-Red Flow:

What timestamp do you want that value (23371320) to be recorded at? The field timestamp has the value 1655721971460000000 which is 2022-06-20 12:28:35, which is different to the server and source timestamps that are also in the record.

[Edit] No it isn't, I pasted the wrong value in the timestamp converter. The timestamp values are the same. I will post again in a few minutes.

For the data you posted you need to construct msg.payload with something like

[
    {
        measurement: "ExGrafanaDbMachineState1DataModeTimeProduction",
        fields: {
            value: 23371320,
        }
        timestamp: 1655721971460000000
    }
]

and send it to the batch node.

Yes, my problem was constructing this payload from the output side of OPC node! How can I construct by collecting/assemblling each output from OPC node until all variables are processed (ex. 10 outputs/variables) and store it in influxdb node? As I said before that OPC node processes messages one by one only!

I tried using batch node (see below):

But I still couldn't achieve storing the data as a batch in influxdb with specific timestamp!

The payload shown there is not what I suggested. You could use a function node containing something like

msg.payload = [
    {
        measurement: msg.measurement,
        fields: {value: msg.payload},
        timestamp: msg.timestamp
    }
]
return msg;

Feed the OPC node into that and check in a debug node that output of the function is correct. Then feed it into the influx batch node.

1 Like

thank you very much @Colin, it works perfect :grinning: