pythoninfluxdb-python

Writing to influx db returns no error, but no data is stored


I am trying to write some sensor data into InfluxDB. The client returns no error, but no data is hitting the db. Can you please give me a hand? I played around with tags, different, time formats and so on, but to no avail. I read what I could find, but I am lost somehow. I am using InfluxDB 1.8.3,

def create_dictionary_for_value(temperature,humidity):
    return [{
            "measurement": "kojiboxclimate",
            "tags": {
                "host": "server01",
                "region": "us-west"
            },
            "time": datetime.datetime.utcnow().isoformat(),
            "fields": {
                "temperature": temperature,
                "humidity": humidity
            }
    }]

def main():
    client = InfluxDBClient(HOST, PORT, USER, PASSWORD, DBNAME)
    retention_policy = 'my_policy'
    client.create_retention_policy(retention_policy, '3d', 3, default=True)

    while True:
        temperature, humidity = sensor.read()
        payload = create_dictionary_for_value(temperature, humidity)
        print("{}\n".format(payload))
        client.write_points(payload, retention_policy=retention_policy, protocol='json')
        result = client.query('select value from kojiboxclimate;')
        print("Result: {0}".format(result))
        time.sleep(60)

Output of the payload produces what I would expect:

[{'fields': {'temperature': 20.164034485389493, 'humidity': 38.62821393148699}, 'tags': {'host': 'server01', 'region': 'us-west'}, 'time': '2020-11-26T19:00:15.042571', 'measurement': 'kojiboxclimate'}]

The result set is empty, so is the db.


Solution

  • As I tried your code it seems to work for me, except the search query.

    You try to query your data with this line of code:

    result = client.query('select value from kojiboxclimate;')
    

    With this you try to get a field value of your data, which doesn't exist.

    If you want to get all data you can do:

    result = client.query('select * from kojiboxclimate;')
    

    If you want to get e.g. the temperature you can use the following code snippet:

    result = client.query('select temperature from kojiboxclimate;')