How to convert data type from string to float in databucket

Hi everyone,

Allow me to ask.
Is there a way to convert the string data type to float which has been stored in the thinger data bucket? The data source comes from the MQTT device in JSON format. Because of this, maybe the apex pie chart widget that Im using is not display data because my data format is string. In this case I can’t mess with the third party device that I use to go to thinger.io.

Thank you.

IMHO there is not “float” data type in a bucket. It is always plain text format.

Hi,

If the JSON msg has the number between quote marks maybe that is why is stored as string.

You may set a function into product’s scripts to remove the quote marks from the JSON msg numbers before it is stored into the bucket.

To change the stored data you will need to download it, modify and then upload again.

Hope this helps.

Hi @ega,

Event inspector on my bucket is as follows.

{
    "bucket": "test",
    "data": {
        "AI1": "17.43",
        "AI2": "10.60",
        "AI3": "16.30",
        "AI4": "19.29",
        "TIME": "1731912797",
        "device": "flow_mqtt_thinger",
        "flowrate": "-18.2",
        "flowspeed": "-0.03",
        "group": "assetgroup",
        "totalizer": "77861"
    },
    "device": "flow_mqtt_thinger",
    "event": "bucket_write",
    "group": "assetgroup",
    "ts": 1731912797310,
    "user": "christosunbilly"
}

How do I remove quote marks from this JSON for AI1, AI2, AI3, flowrate, flowspeed and totalizer values.

To change the stored data need to download it, modify and then upload it again. Sorry I’m new to this if you don’t mind can I get some direction on how to do it? So that I can plot some values ​​on the apex chart widget.

Thank you.

My standard tinger.io bucket data has also quote marks around all float data.


    "bucket": "MIN",
    "data": {
        "M": {
            "Bat%": {
                "N": "0"
            },
            "BatI": {
                "N": "0"
            },
            "BatV": {
                "N": "0"
            },
            "BatW": {
                "N": "0"
            },
            "LEq": {
                "N": "48.832344055175781"
            },
            "LEtr": {
                "N": "44.543727874755859"
            },
            "PanV": {
                "N": "0"
            },
            "backgr": {
                "N": "45.276187896728516"
            },
            "max": {
                "N": "57.580001831054688"
            },
            "min": {
                "N": "41.987014770507812"
            }
        }
    },
    "event": "bucket_write",
    "ts": 1731921719811,
    "user": "Schutzgemeinschaft"
}

IMHO it’s just the regular syntax for float bucket data.
Apex Charts has no issue with them, at least with time series.
Integers are however stored without quotes.
Maybe converting the values to integers (with e.g. factor 100) could provide what you need?

Hi,

To be able to modify the payload before is written into the bucket, you need to use a product.

After the product is defined, in its profile, you may add the bucket to be subscribed to the topic, therefore in this bucket profile, you may establish to execute a script before the msg’s payload is used.

In this script, you can use a method like → JavaScript parseFloat() Method to modify the desired string values to float.

To import and export the data → https://docs.thinger.io/features/buckets#bucket-data-import

When data is exported, it is a standard .csv file, however currently I am not totally sure if strings values are in between quote marks, if not, necessarily you are going to need to write the correct datatype values from another method before importing the .csv file.

Why this? this is because the database establish the data type when the bucket is empty, when this first dataset written, it establish each datatype (boolean, string, float…) for each column, after this it does not allow to write a string into a bool value (for example).

What do I recommend? if the data is not critical and you can start to fill this bucket from scratch, clear the whole bucket and write the script to modify the values you need to store as float and start storing it in this way.

If you have time and want to mess around, for testing purposes, download the data and check if the float are in between quote marks, upload data to a new bucket to verify how is stored and it works with the widget, etc…

Hope this helps.

Hi @rin67630 ,

Noted with thanks. I will try this method.

Hi @ega ,

Many thanks for your advice. I got the idea and will try this methods.

Btw, thinger developers recommend to store numeric values as float or integers, although the apexchart widget could be able to use string data type, this data type is not compatible, for example, with the aggregation feature, because it is not able to do math with string data type.

Hope this helps,

…but how can one specify a float as a float value in the bucket using
thing.write_bucket(); ?
The bucket definition in setup() is e.g.

thing[BUCKET_MIN] >> [](pson& out) {
    out["min"] = A0dB;
    out["bg"] = A0dBbg;
}

where A0dB and A0dBbg are floats.

Hi,

I guess the device’s library automatically sets each var data type, how you do it is the recommended way to do it.