Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Writing to an InfluxDB server with Python3
#!/usr/bin/python
from datetime import datetime
from influxdb import InfluxDBClient
def func_log(unit, kind, value):
client = InfluxDBClient(host='127.0.0.1', port=8086)
client.create_database('DBname')
client.switch_database('DBname')
json_body = [
{
"measurement": "MeasureName",
"tags": {
"unit": unit,
"kind": kind
},
"fields": {
"value": value
},
"time": f'{datetime.utcnow().isoformat()}Z'
}
]
try:
if client.write_points(json_body):
success = True
else:
print("Error writing to InfluxDB")
except:
print("Error writing to InfluxDB")
if __name__ == '__main__':
unit = "Celsius"
kind = "temp"
value = 15
func_log(unit, kind, value)
@Tombo1001
Copy link
Author

Tombo1001 commented Jan 19, 2021

Be sure to update your influxDB server IP and port accordingly on line 6 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment