Hi, I’m new to influxdb and have a very strange behavior that I don’t understand, maybe someone can enlighten me:
I have a python script (see some code below) that reads multiple sensors and inserts it to influxdb in several measurements.
Problem I have is with the measurement “soilMoistureMapped”. First I had the code below without the explicit float cast.
The values of the sensor go from 0.0 to 10.0, so being a float with 1 decimal.
First everything worked as expected.
Then at some point this measurement stopped working with this error:
influxdb_client.rest.ApiException: (422)
Reason: Unprocessable Entity
HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json; charset=utf-8', 'X-Influxdb-Build': 'OSS', 'X-Influxdb-Version': 'v2.7.11', 'X-Platform-Error-Code': 'unprocessable entity', 'Date': 'Fri, 23 May 2025 20:21:48 GMT', 'Content-Length': '236'})
HTTP response body: {"code":"unprocessable entity","message":"failure writing points to database: partial write: field type conflict: input field \"capacitive\" on measurement \"soilMoistureMapped\" is type float, already exists as type integer dropped=1"}
So it says "you give me a float (which is correct), but I expect an int (which is nonsense since it alwas used to be float).
First I thought it’s because data is going through JSON, so I guessed that some X.0 value got interpreted as int and (somehow) changed the type of the whole measurement to int.
This was when I added the expicit float() cast to the python code you can see below.
And I deleted the measurement by:
influx delete --bucket growpi --start 2024-01-01T00:00:00+02:00 --stop 2026-01-01T00:00:00Z --predicate '_measurement="soilMoistureMapped"'
After that everything worked again (besides having lost some data).
But now the very same issue came up again, so it seems the missing type cast was not the issue.
Now I found another pattern in causing the error. It always comes up when I reboot the system that holds both, the influxdb and the python script for the sensor (which is a Raspberry Pi 5 , if that should matter).
However I have no idea how / why this is happening.
Anyone can help?
Btw. all the other readings, which are int or even also float (but with 2 decimals) and also come from the same JSON and python script work just fine.
I now issued the delete above again and for now it is working again and even losing data is fine for now, since it is still development / testing phase, but it will not be ok later on to be forced to lose all data on each reboot of the system.
It this point I start to wonder if it would be better to use a typesafe language like golang instead of python. Don’t know if this would make it any better, since the script seems to give float to the db, which actually is the expected behavior.
import influxdb_client
# ...
conf = getConfig()
client = influxdb_client.InfluxDBClient(
url=conf["influxdb"]["url"],
token=conf["influxdb"]["token"],
org=conf["influxdb"]["org"],
)
write_api = client.write_api(write_options=influxdb_client.client.write_api.SYNCHRONOUS)
# ...
def processData(data):
values = json.loads(data)
# ...
write_api.write(
bucket=bucket,
org=conf["influxdb"]["org"],
record=influxdb_client.Point("soilMoisture")
.tag("sensor", "analog")
.field("capacitive", int(values["capacitiveSoilHumidity"]))
.field("current", int(values["currentSoilHumidity"])),
)
write_api.write(
bucket=bucket,
org=conf["influxdb"]["org"],
record=influxdb_client.Point("soilMoistureMapped")
.tag("sensor", "analog")
# cast to foat to ensure field is ALWAYS float since json may suggest int
.field("capacitive", float(values["capacitiveSoilHumidityMapped"]))
.field("current", float(values["currentSoilHumidityMapped"])),
)
# ...
EDIT: forgot to mention… I use influxdb 2 (2.7.11-1)