Skip to content

Failed to write records to Timestream. The error received was 'Invalid timestamp value ''.

0

I publish MQTT Messages to AWS IoT Core with the following payload:

{
  "SensorValue": {
    "Speed": 49,
    "Direction": 9
  },
  "MsgTime": "2024-10-10T11:05:24Z"
}

IoT Rule receives those messages and should insert them into the Timestream.

I defined the timestamp value in the following way:

${time_to_epoch(MsgTime,"yyyy-MM-dd'T'HH:mm:ss'Z'")}
unit='MILLISECONDS'

When I publish messages, I receive the following error:

"errorMessage": "Failed to write records to Timestream. The error received was 'Invalid timestamp value ''. Length should be in [1, 256] in UTF-8 encoded bytes.'. Message arrived on test_device, Action: timestream, Database: TSTelemetryDatabase, Table: TSTelemetryTable"

Any suggestions are very appreciated!

3 Answers
0

I tried below and it absolutely worked for me. https://repost.aws/questions/QUmWAjLqG7RwGHCSL2tMrXvw/iot-core-rules-to-map-timestamp-to-timestream

Timestamp value - optional
value: ${time_to_epoch(MsgTime,"yyyy-MM-dd'T'HH:mm:ss'Z'")}
unit: MILLISECONDS
AWS

answered 2 years ago

  • In my case, it only works when I use ${MsgTime} and unit: SECONDS

    And the following payload:

    {
      "SensorValue": {
        "Speed": 49,
        "Direction": 9
      },
      "MsgTime": "1728584001"
    }
    

    Do you know how to debug the time_to_epoch function on AWS IoT Core?

0

By default, CDK creates the IoT Rule with the SQL version 2015-10-08, but the time_to_epoch requires the SQL version 2016-03-23 and later.

CDK snippet defining the SQL version:

        iot_rule = iot.CfnTopicRule(
            self, "IoTTelemetryRule",
            rule_name='telemetry',
            topic_rule_payload=iot.CfnTopicRule.TopicRulePayloadProperty(
                sql=sql_select,
                aws_iot_sql_version="2016-03-23",
                actions=[timestream_action, republish_action],
                error_action=error_action,
                rule_disabled=False
            )
        )

https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-functions.html#iot-sql-function-time-to-epoch

answered 2 years ago

-1

The error you're encountering suggests that the timestamp value being passed to Timestream is invalid or empty. This could be due to a few reasons:

  1. The time_to_epoch function might not be correctly parsing the timestamp from your MQTT message.

  2. The MsgTime field in your MQTT payload might not be accessible in the way you're trying to use it.

To resolve this issue, try the following:

  1. Ensure that the MsgTime field is at the top level of your JSON payload. In your current payload, it's at the root level, which is correct.

  2. Modify your timestamp value expression in the IoT Rule to explicitly reference the top-level MsgTime field:

${time_to_epoch($.MsgTime, "yyyy-MM-dd'T'HH:mm:ss'Z'")}

The $. notation ensures that you're referencing the root of the JSON payload.

  1. If the above doesn't work, you could try using the built-in timestamp() function instead, which will use the time the message was processed:
${timestamp()}
  1. Double-check that the MsgTime field in your MQTT messages always contains a valid timestamp string.

  2. Verify that your IoT Rule's SQL query is correctly selecting the fields from your MQTT payload. For example:

SELECT *, ${timestamp()} AS timestamp FROM 'your/mqtt/topic'
  1. Ensure that the IAM role associated with your IoT Rule has the necessary permissions to write to your Timestream database and table.

If you're still encountering issues after trying these suggestions, you may want to review the full configuration of your IoT Rule and Timestream action to ensure all parameters are set correctly.
Sources
Timestream - AWS IoT Core
AWS IoT Core - Amazon Timestream

answered 2 years ago

  • I modified the timestamp value expression in the IoT Rule to explicitly reference the top-level MsgTime field and received the following error when trying to save changes:

    SqlParseException
    Unexpected character '$' time_to_epoch($.MsgTime, "yyyy-MM-dd'T'HH:mm:ss'Z'")} --------------^ at 1:15
    

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.