Skip to content

AWS IoT Greengrass ShadowManager component seems to convert integer into float value

0

I have deployed ShadowManager and MyCustomComponent on my core device. ShadowManager always synchronizes data with AWS IoT Core Device Shadow in real-time. MyCustomComponent publishes a JSON message like { "test": 1 } using the GreengrassCoreIPCClientV2. I can confirm updates to the value of the 'test' property, but the value appears as 1.0. It should be 1. How do I avoid converting the integer value into a float?

If I publish a message with an integer value (e.g., 1) directly to AWS IoT Core Device Shadow via MQTT, the Device Shadow successfully retains it as an integer.

asked a year ago132 views

2 Answers
1

This behavior is expected with AWS IoT Greengrass. AWS IoT Greengrass uses JSON for configuration values, and JSON doesn't differentiate between integers and floats - it only specifies a "number" type. As a result, numeric values might convert to floats when processed by AWS IoT Greengrass.

To ensure your component uses the correct data type, the recommended approach is to define your numeric values as strings in your JSON messages. For example, instead of publishing { "test": 1 }, you could publish { "test": "1" }. Then, have your component parse these string values as integers or floats as needed.

This approach ensures that your values maintain the same data type both in the configuration and on your core device. By handling the type conversion in your component code, you can maintain precise control over whether a number is treated as an integer or a float.
Sources
AWS IoT Greengrass component recipe reference - AWS IoT Greengrass
Saving data as Double instead of bigint | AWS re:Post

answered a year ago

0

Hi JSON does not define how numbers are to be parsed, so some libraries always use floating point, which is compliant. Getting either 1 or 1.0 is expected, and may depend on the tools used. If you need an integer, you can check if the decimal part is 0, or you can encode your data as strings.

AWS

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.