# Node Red Custom Date Time insert into influx FLUX issue

**URL:** <https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938>\
**Category:** General\
**Created:** [25 March 2023 16:28 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938 "2023-03-25T16:28:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankit.prasad](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ankit.prasad](https://discourse.nodered.org/u/ankit.prasad)\
**Post date:** [25 March 2023 16:28 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/1 "2023-03-25T16:28:02Z")

</div>

When I am trying to insert a old timestamp  
the values are not visible in influx DB

```auto

var newMsg = {};

var data_time = msg.payload.data.substr(16, 8);
var Temp = Number(hexTo8p8(msg.payload.data.substr(16 + 8, 4)));
var Humidity = Number(hexTo8p8(msg.payload.data.substr(16 + 8 + 4, 4)));
var data = msg.payload.data.substr(16, 8);
var data_length = msg.payload.data.length;

for (var i = 1; i <= 30; i++) {
    data_time = msg.payload.data.substr(16 * i, 8);
    Temp = Number(hexTo8p8(msg.payload.data.substr(24 * i, 4)));
    Humidity = Number(hexTo8p8(msg.payload.data.substr(28 * i, 4)));
    data = msg.payload.data.substr(16 * i, 8);
    let UTC_ISO = new Date(parseInt(msg.payload.data.substr(16 * i, 8), 16) * 1000).toISOString();
    let UTC_unix = parseInt(msg.payload.data.substr(16 * i, 8), 16);
    var d = new Date(UTC_ISO);
    var converted_payload = UTC_ISO.split(/\.|T/).slice(0, 2).join(" ");
    let UTC_short = new Date(parseInt(msg.payload.data.substr(16 * i, 8), 16) * 1000).toDateString();
    var checktime = 1546337502;
    if (checktime > data_time) {

    }

    if (Temp == Humidity || Temp > 99 || Humidity > 99 || data_length < 40) {

    }
    else {
        newMsg.payload = [{
            device_mac: msg.payload.mac,

            device_timestamp: new Date(UTC_unix * 1000).getTime(),

            temp: Temp,
            humidity: Humidity,
            time: new Date(UTC_unix * 1000).getTime(),
            timestamp: new Date(UTC_unix * 1000).getTime(),
        },
        {
            device_mac: msg.payload.mac,
            history: 1,
            host: 1,
          

        }];
        node.send(newMsg);
    }

}

return null;

function calculateDistance(rssi) {
    let P = -69; // @TODO This value should come from MQTT message
    let n = 3;
    let d = Math.pow(10, ((P - rssi) / (10 * n))); //(n ranges from 2 to 4)
    return d * 2;
}

function hexTo8p8(/** @type {string} */ value) {
    let intValue = parseInt(value, 16);
    let signed = (intValue & 0x8000) > 0 ? -1 / 10 : 1;
    return signed * intValue / Math.pow(2, 8);
}

function int2float( /** @type {string} */value_int) {
    // Create new Buffer based on array bytes
    const buf = Buffer.from(value_int);
    // swap the words
    const temp0 = buf[0];
    const temp1 = buf[1];
    buf[0] = buf[2];
    buf[1] = buf[3];
    buf[2] = temp0;
    buf[3] = temp1;

    // Represent these bytes as 32-bit unsigned int
    const value = buf.readInt32BE();

    // save the value
    msg.payload = value;

    return msg;
}

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/2347cc34525708a5c163f0f2052038b66fef3225.png)

Issue  
Unable to restore old data using the old time stamp

```auto

[{"device_mac":"BC572900A06A","device_timestamp":1663470615000,"temp":31.15625,"humidity":53.91015625,"time":1663470615000,"timestamp":1663470615000},{"device_mac":"BC572900A06A","history":1,"host":1}]

```

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [25 March 2023 16:29 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/2 "2023-03-25T16:29:53Z")

</div>

Moved to General - this is not an employment advert/offering.

---

<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:** [25 March 2023 16:54 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/3 "2023-03-25T16:54:23Z")

</div>

What resolution have you configured the influx node to use?

---

<div class="post-metadata">

**Author:** ![ankit.prasad](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ankit.prasad](https://discourse.nodered.org/u/ankit.prasad)\
**Post date:** [25 March 2023 16:56 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/4 "2023-03-25T16:56:17Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/0/a0c37d739b571f2a55325109f267e8522a2a7a42.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:** [25 March 2023 17:15 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/5 "2023-03-25T17:15:38Z")

</div>

> [@ankit.prasad](#):
>
> `time: new Date(UTC_unix * 1000)`

The best way to investigate problems like this is to insert it into an empty measurement. Then you can inspect the whole dataset (1 record) and see what is going on.

You could try  
`time: new Date(UTC_unix * 1000)`  
so pass it through as a Date object and let the node sort it out. Alternatively try an ISO date string.

---

<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:** [25 March 2023 17:18 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/6 "2023-03-25T17:18:31Z")

</div>

Perhaps I am misunderstanding the problem. Do you mean they are all going in with the current time instead of the one you are passing in?

---

<div class="post-metadata">

**Author:** ![ankit.prasad](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ankit.prasad](https://discourse.nodered.org/u/ankit.prasad)\
**Post date:** [25 March 2023 17:20 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/7 "2023-03-25T17:20:50Z")

</div>

> [@Colin](#):
>
> What resolution have you configured the influx node

yes its writing with the current timestamp instead of the time which is passed by the function

---

<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:** [25 March 2023 17:23 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/8 "2023-03-25T17:23:27Z")

</div>

I haven't used influxdb 2 yet. Where is the time\_in value coming from?

---

<div class="post-metadata">

**Author:** ![ankit.prasad](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ankit.prasad](https://discourse.nodered.org/u/ankit.prasad)\
**Post date:** [25 March 2023 17:26 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/9 "2023-03-25T17:26:22Z")

</div>

```auto

[{"id":"0e9164e2d746f7f0","type":"tab","label":"Flow 6","disabled":false,"info":"","env":[]},{"id":"420fd15204c8c209","type":"inject","z":"0e9164e2d746f7f0","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"msg\":\"dAck\",\"mac\":\"BC572900A06A\",\"rslt\":\"succ\",\"seq\":3,\"cause\":0,\"rmn\":1,\"dType\":\"hex\",\"data\":\"0200F502000003C063268C171F2835E963268C561FA1360A63268C9E202235ED63268CE0209F361763268D22211D363363268D67219D363963268DB22223364563268DFD229F362363268E51231B35CF63268EAB23A2352B63268F23241C346E63268F92249B33FE6326901F251A3306632690CD259E311C632692A2261C338D632693A4269933AC632694EB271B33E76326959F279F33F0632695C3282432E7632695E4289E31D96326961A2928312A632696CB299A2F9D6326992A29D72CB163269B4629622B3D63269D6928E12A4463269E3B28662AA463269FB827E32BAA6326A14A27632A126326A20426E229756326A26D265F2939\",\"gmac\":\"94A408B0482C\"}","payloadType":"json","x":190,"y":220,"wires":[["dfc629de9d91eab6"]]},{"id":"dfc629de9d91eab6","type":"switch","z":"0e9164e2d746f7f0","name":"","property":"payload.data.length","propertyType":"msg","rules":[{"t":"lte","v":"30","vt":"str"},{"t":"gte","v":"40","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":370,"y":140,"wires":[[],["df9aec65b0f46328"]]},{"id":"df9aec65b0f46328","type":"function","z":"0e9164e2d746f7f0","name":"history1","func":"\nvar newMsg = {};\n\n\n\n\nvar data_time = msg.payload.data.substr(16, 8);\nvar Temp = Number(hexTo8p8(msg.payload.data.substr(16 + 8, 4)));\nvar Humidity = Number(hexTo8p8(msg.payload.data.substr(16 + 8 + 4, 4)));\nvar data = msg.payload.data.substr(16, 8);\nvar data_length = msg.payload.data.length;\n\nfor (var i = 1; i <= 30; i++) {\n data_time = msg.payload.data.substr(16 * i, 8);\n Temp = Number(hexTo8p8(msg.payload.data.substr(24 * i, 4)));\n Humidity = Number(hexTo8p8(msg.payload.data.substr(28 * i, 4)));\n data = msg.payload.data.substr(16 * i, 8);\n let UTC_ISO = new Date(parseInt(msg.payload.data.substr(16 * i, 8), 16) * 1000).toISOString();\n let UTC_unix = parseInt(msg.payload.data.substr(16 * i, 8), 16);\n var d = new Date(UTC_ISO);\n var converted_payload = UTC_ISO.split(/\\.|T/).slice(0, 2).join(\" \");\n let UTC_short = new Date(parseInt(msg.payload.data.substr(16 * i, 8), 16) * 1000).toDateString();\n var checktime = 1546337502;\n if (checktime > data_time) {\n\n }\n \n \n if (Temp == Humidity || Temp > 99 || Humidity > 99 || data_length < 40) {\n\n }\n else {\n\n \n newMsg.payload = [{\n device_mac: msg.payload.mac,\n\n temp: Temp,\n humidity: Humidity,\n time: d,\n \n },\n {\n device_mac: msg.payload.mac,\n history: 1,\n host: msg.payload.mac,\n \n\n\n\n\n\n }];\n node.send(newMsg);\n }\n\n\n\n\n\n}\n\n\n\n\n\n\n\n\n\n\nreturn null;\n\nfunction calculateDistance(rssi) {\n let P = -69; // @TODO This value should come from MQTT message\n let n = 3;\n let d = Math.pow(10, ((P - rssi) / (10 * n))); //(n ranges from 2 to 4)\n return d * 2;\n}\n\n\nfunction hexTo8p8(/**@type {string} */ value) {\n let intValue = parseInt(value, 16);\n let signed = (intValue & 0x8000) > 0 ? -1 / 10 : 1;\n return signed * intValue / Math.pow(2, 8);\n}\n\n\n\nfunction int2float( /** @type {string} */value_int) {\n // Create new Buffer based on array bytes\n const buf = Buffer.from(value_int);\n // swap the words\n const temp0 = buf[0];\n const temp1 = buf[1];\n buf[0] = buf[2];\n buf[1] = buf[3];\n buf[2] = temp0;\n buf[3] = temp1;\n\n // Represent these bytes as 32-bit unsigned int\n const value = buf.readInt32BE();\n\n // save the value\n msg.payload = value;\n\n return msg;\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":240,"wires":[["5ac241f12b7ef875"]]},{"id":"5ac241f12b7ef875","type":"debug","z":"0e9164e2d746f7f0","name":"debug 351","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":420,"wires":[]}]

```

---

<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:** [25 March 2023 17:27 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/10 "2023-03-25T17:27:46Z")

</div>

I meant that it doesn't exist in the sample object you posted, which I presume is what you are sending to influx.

---

<div class="post-metadata">

**Author:** ![ankit.prasad](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ankit.prasad](https://discourse.nodered.org/u/ankit.prasad)\
**Post date:** [25 March 2023 17:29 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/11 "2023-03-25T17:29:46Z")

</div>

So what can be done

---

<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:** [25 March 2023 17:32 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/12 "2023-03-25T17:32:27Z")

</div>

What is the time\_in field that is in the screenshot you posted?

---

<div class="post-metadata">

**Author:** ![ankit.prasad](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ankit.prasad](https://discourse.nodered.org/u/ankit.prasad)\
**Post date:** [25 March 2023 17:38 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/13 "2023-03-25T17:38:32Z")

</div>

> [@ankit.prasad](#):
>
> `timestamp":1663470615000`

its the utc time which i am passing  
i was experimenting by passing different tags and value

timestamp":1663470615000

---

<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:** [25 March 2023 17:44 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/14 "2023-03-25T17:44:12Z")

</div>

So the data that you posted is not the data that generated the screenshot of the interrogation of the database?  
If so then please run it again starting with an empty measurement, and show us the actual data that you are sending to the influx node and what arrives in the database. Also post the flow again including the influx node please

---

<div class="post-metadata">

**Author:** ![ankit.prasad](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ankit.prasad](https://discourse.nodered.org/u/ankit.prasad)\
**Post date:** [25 March 2023 17:54 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/15 "2023-03-25T17:54:27Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/05e14ad6498e770486148a1dbaf9639f4c344afd.png)

```auto

[{"id":"7e73b1a86e3aa311","type":"influxdb out","z":"0e9164e2d746f7f0","influxdb":"c754dc20d3f812a0","name":"Tag4Track_History1","measurement":"mem","precision":"","retentionPolicy":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"Taguard","bucket":"history1","x":660,"y":500,"wires":[]},{"id":"c754dc20d3f812a0","type":"influxdb","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"database","name":"tag4track","usetls":false,"tls":"","influxdbVersion":"2.0","url":"http://tag4track.com:8086","rejectUnauthorized":false}]

```

---

<div class="post-metadata">

**Author:** ![ankit.prasad](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ankit.prasad](https://discourse.nodered.org/u/ankit.prasad)\
**Post date:** [25 March 2023 17:55 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/16 "2023-03-25T17:55:27Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/e/fe110eabdf1c90a44bf6bcdd13e8c0d3eb9e2974.png)

---

<div class="post-metadata">

**Author:** ![ankit.prasad](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ankit.prasad](https://discourse.nodered.org/u/ankit.prasad)\
**Post date:** [25 March 2023 18:04 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/17 "2023-03-25T18:04:40Z")

</div>

Could you suggest if i want to use influx batch  
where can i mention the measurement in the json

---

<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:** [25 March 2023 19:02 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/18 "2023-03-25T19:02:23Z")

</div>

Doesn't it tell you in the node's help text?  
I am not at my computer.

---

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [26 March 2023 13:48 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/19 "2023-03-26T13:48:10Z")

</div>

@ankit.prasad

You should be able to specify the time. See [my post here](https://discourse.nodered.org/t/using-node-red-to-transfer-data-from-influxdb-1-7-to-2-3-but-receiving-enobufs-error-when-dataset-is-too-large/65532/9).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 May 2023 13:48 UTC](https://discourse.nodered.org/t/node-red-custom-date-time-insert-into-influx-flux-issue/76938/20 "2023-05-25T13:48:36Z")

</div>

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