# Time Stamp and Influx DB problem

**URL:** <https://discourse.nodered.org/t/time-stamp-and-influx-db-problem/13715>\
**Category:** General\
**Created:** [26 July 2019 11:53 UTC](https://discourse.nodered.org/t/time-stamp-and-influx-db-problem/13715 "2019-07-26T11:53:46Z")\
**Posts on this page:** 9\
**Page:** 3

<div class="post-metadata">

**Author:** ![mkdthanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mkdthanga/32/61384_2.png) [@mkdthanga](https://discourse.nodered.org/u/mkdthanga)\
**Post date:** [20 June 2022 09:43 UTC](https://discourse.nodered.org/t/time-stamp-and-influx-db-problem/13715/43 "2022-06-20T09:43:36Z")

</div>

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 ☹ ), 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):

```auto
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:**

```auto
[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!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 June 2022 10:18 UTC](https://discourse.nodered.org/t/time-stamp-and-influx-db-problem/13715/44 "2022-06-20T10:18:11Z")

</div>

> [@mkdthanga](#):
>
> 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

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.

---

<div class="post-metadata">

**Author:** ![mkdthanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mkdthanga/32/61384_2.png) [@mkdthanga](https://discourse.nodered.org/u/mkdthanga)\
**Post date:** [20 June 2022 10:51 UTC](https://discourse.nodered.org/t/time-stamp-and-influx-db-problem/13715/45 "2022-06-20T10:51:00Z")

</div>

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:**

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/6/36f603f0f61e240f37831289f3cc47cec98be649.png)

```auto
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:**

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/4/541d2b353967d2c51d46fa6e47598ea53a0aa9a5.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 June 2022 12:32 UTC](https://discourse.nodered.org/t/time-stamp-and-influx-db-problem/13715/46 "2022-06-20T12:32:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 June 2022 12:47 UTC](https://discourse.nodered.org/t/time-stamp-and-influx-db-problem/13715/47 "2022-06-20T12:47:23Z")

</div>

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

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

```

and send it to the batch node.

---

<div class="post-metadata">

**Author:** ![mkdthanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mkdthanga/32/61384_2.png) [@mkdthanga](https://discourse.nodered.org/u/mkdthanga)\
**Post date:** [20 June 2022 13:47 UTC](https://discourse.nodered.org/t/time-stamp-and-influx-db-problem/13715/48 "2022-06-20T13:47:52Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![mkdthanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mkdthanga/32/61384_2.png) [@mkdthanga](https://discourse.nodered.org/u/mkdthanga)\
**Post date:** [20 June 2022 13:52 UTC](https://discourse.nodered.org/t/time-stamp-and-influx-db-problem/13715/49 "2022-06-20T13:52:25Z")

</div>

> [@Colin](#):
>
> ```auto
> [
> {
> measurement: "ExGrafanaDbMachineState1DataModeTimeProduction",
> fields: {
> value: 23371320,
> }
> timestamp: 1655721971460000000
> }
> ]
> 
> ```

I tried using batch node (see below):

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/3591ebabe309e4f12387136d419e38f577e04522.png)

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 June 2022 14:32 UTC](https://discourse.nodered.org/t/time-stamp-and-influx-db-problem/13715/50 "2022-06-20T14:32:16Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![mkdthanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mkdthanga/32/61384_2.png) [@mkdthanga](https://discourse.nodered.org/u/mkdthanga)\
**Post date:** [20 June 2022 15:51 UTC](https://discourse.nodered.org/t/time-stamp-and-influx-db-problem/13715/51 "2022-06-20T15:51:27Z")

</div>

thank you very much @Colin, it works perfect 😀

[Previous page](https://discourse.nodered.org/t/time-stamp-and-influx-db-problem/13715.md?page=2)
