InfluxDB wrong value type on 1st import (field type conflict)


#1

Dear community,
I have an issue with InfluxDB integration and count on your support.

I have a temperature sensor which provides a temperature in Celsius.
I’ve written a simple Decode js function and it was working fine until I got a problem.

As you know InfluxDB on the 1st data import will create corresponding MEASUREMENTS and FIELD KEYS

If my Decode function will return a result like:
{ sensor: ‘CO2’, temp: 23.25, hum: 29.35, gas: 450 }

It will create
device_frmpayload_data_temp as value float
device_frmpayload_data_hum as value float
device_frmpayload_data_gas as value integer

But !!!
If my temperature is 25.0 I will get on 1st data import
{ sensor: ‘CO2’, temp: 23, hum: 29.35, gas: 450 }
and
device_frmpayload_data_temp as value integer

And then, on an attempt to export decimal temperature like 25.15 will produce an error like "“error”:"partial write: field type conflict: input field"

Is any way to avoid such field type conflict and return from the Decode function value with decimal zero?
Or could you please suggest any other way how to fix it?

I’ve found InfluxDB exporter here https://github.com/brocaar/lora-app-server/blob/5032624e5afc80553ff255d66c9dacbdfe10fd4a/internal/handler/influxdbhandler/influxdb_handler.go

Function inside code treats values as integer (like 17i) or float (like17) depending on data type.
But I do not understand how to return from my Decode js code my temperature as ```
float32 or float64.

func formatInfluxValue(v interface{}, quote bool) string {
switch v := v.(type) {
case float32, float64:
	return fmt.Sprintf("%f", v)
case int, uint, uint8, int8, uint16, int16, uint32, int32, uint64, int64:
	return fmt.Sprintf("%di", v)
case string:
	if quote {
		return strconv.Quote(v)
	}
	return v
case bool:
	return fmt.Sprintf("%t", v)
default:
	return fmt.Sprintf("%v", v)
}

}

Thank you in advance.


#2

As a dirty way around I use a function that forces integer values to float. But I do not like it. :frowning:

   function forceFloat(n) {
    return ( n == (0 | n) ) ? n+0.00001 : n;
}