Post data to influxdb 1.x with own timestamp

I need to post data to InfluxDB with its original timestamp since the data may arrive at the database at a different time from when it was generated. I'm encountering difficulties!
The influx version is still the 1.x

This is the typical payload I receive:
payload: "113|25.75|2023-09-07T06:32:28.434Z"
The last portion is the timestamp at the time the measurements taken.

This is the output of the function receiving the above payload:

This is the function I wrote:

if (msg.topic === "BMS!2022/temperature") {
    var t = msg.payload;

    // Find the last occurrence of a pipe, which should be the start of the timestamp
    var lastPipeIndex = t.lastIndexOf('|');

    // Extract DeviceID and temperature
    var DeviceID = t.substring(0, t.indexOf('|'));
    var Tamb = parseFloat(t.substring(t.indexOf('|') + 1, lastPipeIndex));

    // Extract the RFC 3339 timestamp
    var rfc3339Timestamp = t.substring(lastPipeIndex + 1);

    // Convert RFC 3339 timestamp to nanoseconds
    var date = new Date(rfc3339Timestamp);
    var nanoTimestamp = date.getTime() * 1e6;  // Convert milliseconds to nanoseconds

    msg.topic = "temperature";

    if (DeviceID && !isNaN(Tamb)) {
        msg.payload = {
            "Tamb": Tamb,
            "timestamp": nanoTimestamp  // Use nanosecond precision timestamp
        };
        msg.measurement = DeviceID;
        msg.timestamp = nanoTimestamp;
        return msg;
    }
} else {

    return null;
}

and this is the content of the measurement in influx:

time                Tamb   timestamp
----                ----   ---------
1694064087553806889 22.312 1694064087550000000
1694064117590312070 22.312 1694064117586000000
1694064147639415844 22.312 1694064147614000000

based on what I see in the above influx output, the db is still using its own time stamp rather then the one I provide. This is also confirmed by showing the data on a chart.
I need some help to make sure that influx use the provided timestamp instead of the own one.

Try making it time in the payload instead of timestamp.

1 Like

Hi Colin. You are a star! Thanks a million. It works well now.

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