How to control the value of a Datapoint using t6 ?
This recipe will help you to figure out how the transformation preprocessor should be implemented and how you can use it.
Tagged on #recipe, #flow, #preprocessor,
In this recipe, our target will be to have a Flow to collect measurement on a sanitized format using a sensor that does not format the float correctly.
Check prerequisites
t6 require a Flow to store specific measurements. To use this recipe you’d first need to create a Flow like the following example. Feel free to review the technical documentation for details
{
"name": "My Flow that contains only floats normalized values",
"data_type": "e7dbdc23-5fa8-4083-b3ec-bb99c08a2a35",
"require_signed": false,
"require_encrypted": false,
"retention": "retention1w",
"preprocessor": [
{
"name": "transform",
"mode": "replace",
"pattern": "(,)",
"replacer": "."
},
{
"name": "sanitize",
"datatype": "float"
}
]
}
Basically, this Flow is setting the datatype as a Float, and 2 preprocessors will fire before saving datapoint to the Flow:
- The first transformation will simply substitute coma with a dot
- The second one will sanitize the value from a string to a Float type
Once your Flow created (that will contains your measurements), take note of the flow.data.id
on the Api results. This value will be used on datapoints creation as the referring variable {{$flow_id}}
.
Create datapoints
Here we are going to post datapoints as string with a non-normalized value (we’ll use a coma instead of a dot for decimals).
{
"value": "100,456798",
"flow_id": "{{$flow_id}}",
"save": true,
"publish": true
}
And voilà, you’ll notice the Api results will transform the initial value into a Float using a correct Float value.
The preprocessor array in the result shows all the values along the preprocessor computation.
There is a double sanitization to Float as the latest one is forced by t6 on any datapoint posted ; we would have removed the sanitize
preprocessor on the Flow creation above with exactly the same result.
"value": 100.456798,
"preprocessor": [
{
"name": "transform",
"mode": "replace",
"pattern": "(,)",
"replacer": ".",
"initialValue": "100,456798",
"transformedValue": "100.456798",
"message": "Transformed to replace.",
"status": "completed"
},
{
"name": "sanitize",
"datatype": "float",
"initialValue": "100.456798",
"message": "Converted to float.",
"status": "completed"
},
{
"name": "sanitize",
"datatype": "float",
"initialValue": "100.456798",
"message": "Converted to float.",
"status": "completed"
}
],
To have more details on Datapoints, read the technical documentation.
Tagged on #recipe, #flow, #preprocessor,