AWS IoT rule wrong casting

In a small AWS IoT Core project I need to convert a string to an integer using an iot rule.

This is what the device is publishing using MQTT in the topic ‘device/+/data’:

{
device_id: “device06”,
temperature: “30”,
humidity: “92”
}

And this is what the AWS IoT Core service is expecting to receive:

{
device_id: “device06”,
temperature: 30,
humidity: 92
}

I’m using an IoT rule to insert data into a Timestream database, so I’m trying to do the conversion using the cast function, but with the wrong syntax.

AWS IoT > Message routing > myrule > SQL Statement:

SELECT device_id, cast(temperature as Int), cast(humidity as Int) from ‘device/+/data’

Since IoT Core only write Timestream single measure records at this time, I found I needed to use a Lambda to write to Timestream anyway. Because of that, I did the typecasting in the Lambda. At low scale, a simple Lambda they runs per message is fine, but if you will go over about 100 events per second you should use SQS between IoT Core and the Lambda and write to Timestream in batches of 100 records.

Did you get any error message in the IoT Rule error action logs?

Apart from the multi-measure record issue, it is a good idea to name your metric columns:

SELECT device_id, cast(temperature as Int) AS temperature, cast(humidity as Int) AS humidity from 'device/+/data'

I have named the columns and that has fixed the error. Thank you so much!