Python client library; how can I add a timestamp to a write request?

Hi All,

Another newbie here !

I am getting familiar with Influx/Grafana to create a reporting platform.

I’ve run through the docs and am able to successfully write and query data, however I would like to add a custom timestamp to data I’m injecting so that I can group like entries to graph the sum.

my basic write works, however whatever way I attempt to add a timestamp the write does not complete and fails silently…

I have tried using epoch as an integer as well as various iterations through datetime formats, but can’t seem to figure it out - any advice welcome pls

$ influxd version
InfluxDB v2.7.0 (git: 85f725f8b9) build_date: 2023-04-05T15:32:25Z

Here is the code I am trying to execute (the first ‘point1’ definition works, the second does nothing):
write_api = client.write_api(write_options=SYNCHRONOUS)

now =
fmt = now.strftime(‘%Y-%m-%dT%H:%M:%SZ’)

number1 = float(random.randint(20,30))
// point1 = influxdb_client.Point(“testdata”).tag(“location”, “site1”).field(“temperature”, number1)
point1 = influxdb_client.Point(“testdata”).tag(“location”, “site1”).field(“temperature”, number1).time(1692176445)

write_api.write(bucket=bucket, org=org, record=point1)

The output I get is (I’ve tried using both strings as variables for time in the point):
2023-08-16 11:57:57.717360

Thanks in advance

never used python but based on the line protocol documentation timestamp should be in ns, unless you specify the precision like this:

write_api.write(bucket=bucket, org=org, record=point1, write_precision='s')

give it a try I just barely read this and came to that conclusion:

Start with Python and InfluxDB | InfluxData

Hi fercasjr,

Thanks for your comment - unfortunately that does not help; however it did put me on the right track

swapping out the write_precision does something, as if you change and put in delibarately wrong errors as a time var, then you will get messages such as out of range

ns is the default & works from a syntax point of view, however I’ve not found any library that works to ns granularity for python (other than time.now_ns() - however you cannot parse any arg to this to convert a time from the past)

If I fabricate a ‘ns’ timestamp based on epoch (either by 10^9 or random string to pad the spaces) the write fails, however using works fine… very confusing

I did find another comment that references a similar issue:

write_api.write("my-bucket", "my-org", "h2o_feet,location=coyote_creek water_level=2", write_precision=WritePrecision.S)

however has not helped my cause either

so bit stuck now, can’t work out how to change the unit to anything other than ns & cannot reliably create a epoch integer string in the past to 19 digits that the client/API accepts !!!

Note: If I put the same fabricated ns strings into an online epoch calculator; output is as expected and works fine; so its something to do with how the client/api processes and parses timestamps

Why you can’t just multiply the int timestamp by 1000000000?

believe me I’ve tried LOTS !!!
I’ve tried calculating by multiplication & concatanating a suffix and converting back to an integer, same result :cry:

Im obviously missing something…

I’m going to have to go back to basics and build it up again, I’m assuming now it must be syntax so will trying specifying attributes as JSON/Dictionary see if I get anwhere

Final update is I presuming there is some kind of bug/incompatability issue:

I have now tried numerous approaches including a Influx Git process & that doesn’t work with time field added

Dictionary/JSON etc… no effect

Time added in natively with ns precision does not work for me either

This excercise was to ingest historical data for analysis, which I now cannot complete. I will therefor have to move forward with timestamp added as data is ingested by the system

Im outta time to dig any further :sweat:

I am working on a similar thing, and am also newbie to both python and influxdb. I want to put power prices that I get from a rest API formatted in json into my influxdb2 database. Power prices are decided one day ahead for every hour so I want to do an API call once a day and put the data for the next day, and therefore set the timestamp for each record

loclaltime = datetime.strptime(timestring, "%Y-%m-%dT%H:%M:%S%z")
utctime = loclaltime.astimezone(pytz.timezone("UTC"))
nanotime = int(time.mktime(utctime.timetuple())) * 1000000000
point = (Point(measurement).field(field, price).time(nanotime))
write_api.write(bucket=bucket, org=org, record=point)