- Newest
- Most votes
- Most comments
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
answered 2 years ago
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
)
)
answered 2 years ago
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:
-
The
time_to_epochfunction might not be correctly parsing the timestamp from your MQTT message. -
The
MsgTimefield in your MQTT payload might not be accessible in the way you're trying to use it.
To resolve this issue, try the following:
-
Ensure that the
MsgTimefield is at the top level of your JSON payload. In your current payload, it's at the root level, which is correct. -
Modify your timestamp value expression in the IoT Rule to explicitly reference the top-level
MsgTimefield:
${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.
- 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()}
-
Double-check that the
MsgTimefield in your MQTT messages always contains a valid timestamp string. -
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'
- 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
Relevant content
asked 2 years ago
asked 6 years ago
asked 5 years ago

In my case, it only works when I use
${MsgTime}andunit: SECONDSAnd the following payload:
Do you know how to debug the
time_to_epochfunction on AWS IoT Core?