Node Red Custom Date Time insert into influx FLUX issue

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



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;
}



Issue
Unable to restore old data using the old time stamp


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

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

What resolution have you configured the influx node to use?

image

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.

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?

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

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


[{"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":[]}]

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

So what can be done

What is the time_in field that is in the screenshot you posted?

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

timestamp":1663470615000

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


[{"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}]

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

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

@ankit.prasad

You should be able to specify the time. See my post here.

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