'How to force AWS Timestream to always interpret a number as double in an IoTCore rule?

Sensor payload decoders are mostly provided as javascript code by the sensor manufacturers. As I'm using different sensor types, I want to use the original decoders without rewriting them in other languages. So I'm using an AWS Lambda (NodeJS) within AWS IoTCore rules for decoding the different sensor payloads, which works fine.

In a succeeding IoTCore rule, I want to send the decoded sensor payload to an AWS Timestream database. AWS Timestream data types are fixed during the first write. So, if the first measurement value "temperature" was a float number like 23.14 degree, the measurement type is fixed to a Timestream::Double type, which is what I want.

However, if the sensor measures a value of a flat 23.0 degrees the next time, then the Timestream write operation leads to the error "Measure name already has an assigned measure value type. Each measure name can have only one measure value type and cannot be changed."

The reason lies in the AWS Timestream parser, which works like so: A numeric value without a decimal point is interpreted as a BigInt type. So,...

> const f1 = 23.4
> const f2 = 23.0
> console.log(f1, typeof f1, f2, typeof f2)
 23.4 'number' 23 'number'
parseFloat(23.14) // becomes Javascript::Number 23.14 Timestream::Double. ==> ok!
parseFloat(23.00) // becomes Javascript::Number 23    Timestream::BigInt  ==> Error! 

I don't want to use parseFloat(23.00).toFixed(2) as it becomes a string value, nor parseFloat(23.00) + 0.001 as it is changing the value and requires always converting/rounding values when processing the AWS Timestream values later on.

How to solve that?



Solution 1:[1]

Solved by skipping the IoTCore rule framework and writing in Lambda directly to Timestream, there I can explicitly set the measureValueType = "DOUBLE" , which avoids that kind of problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tobkle