Search code examples
thingsboard

Splitting raw telemetry data into parts


Completely new to ThingsBoard (TB), and quite new to integrations in general, so be gentle. Have though successfully used Losant to display air quality related data. Data sent from the device consists of a simple string with 8 floats, e.g. "22.38, 34.73, 66.00, 1.00, 3.00, 3.30, 3.40, 3.40".

I'm able to display this string as such in TB, but how to split it to the different components (community edition)? Telemetry data

I suppose I should use Rule Chains, have tried that, but am not able to get the output to be in the form I believe it should be in, i.e.

{ "temp": 23.5, "humi": 33.1, "voci": 11, "noxi": 23, "pm10": 33, "pm25": 55, "pm40": 55, "pm100": 99 }

Instead I get this:

{ "msg": { "payload": "22.38, 34.73, 66.00, 1.00, 3.00, 3.30, 3.40, 3.40" }, "metadata": { "deviceType": "default", "deviceName": "Test Device", "ts": "1734519560226" }, "msgType": "POST_TELEMETRY_REQUEST" }

Went through other similar questions, but those cases seem to be using keys and values instead of raw values.

Script transform node


Solution

  • You are returning wrong data format for script node (https://thingsboard.io/docs/user-guide/rule-engine-2-0/transformation-nodes/#script-transformation-node).

    You should get 8 different telemetries by using following script:

    // Extract the payload string
    var parts = msg.payload.split(",");
    
    // Map the values to telemetry keys
    var telemetry = {
        temp: parseFloat(parts[0].trim()),
        humi: parseFloat(parts[1].trim()),
        voci: parseFloat(parts[2].trim()),
        noxi: parseFloat(parts[3].trim()),
        pm10: parseFloat(parts[4].trim()),
        pm25: parseFloat(parts[5].trim()),
        pm40: parseFloat(parts[6].trim()),
        pm100: parseFloat(parts[7].trim())
    };
    
    // Return the formatted telemetry
    return { msg: telemetry, metadata: metadata, msgType: msgType };
    

    enter image description here