- Newest
- Most votes
- Most comments
Yes, you can use variables from the SELECT statement in the republishing action, but there might be an issue with your current setup. Let's address your problem and provide a solution.
The issue you're experiencing is likely due to the way you've structured your topic and SELECT statement. In your current setup, you're trying to extract the controllerId from the second level of the topic, but your topic structure doesn't align with this.
To fix this, you need to adjust your rule query statement to correctly extract the controllerId. Here's a corrected version:
SELECT * AS message, topic(3) AS controllerId FROM 'controller/+/shadow/status/update'
In this corrected version, we're using topic(3) instead of topic(2) because the controllerId is the third element in your topic structure (controller/[controllerId]/shadow/status/update).
For the republish action, you can use the extracted controllerId like this:
$aws/things/${controllerId}/shadow/name/status/update
This should correctly populate the controllerId in the republished topic.
Regarding your overall approach to tracking device connection status, using MQTT Last Will and Testament (LWT) messages with a rule to update the device shadow is a valid and robust method. Here's a summary of how you can implement this:
-
When your device connects to AWS IoT, it should set up an LWT message on a non-reserved topic (like the one you're using: 'controller/[controllerId]/shadow/status/update').
-
Create an AWS IoT rule (as you've done) to listen for messages on this topic and republish them to the device shadow topic.
-
When the device connects, it should publish a "connected" status to the same topic.
-
If the device disconnects unexpectedly, the LWT message will be published, and your rule will update the shadow to reflect the disconnected status.
This approach will allow you to track the connection status of your IoT Things effectively, with the device shadow always reflecting the current state.
Remember to ensure that your IAM role associated with the republish action has the necessary permissions to publish to the shadow topics.
Sources
Republish - AWS IoT Core
Tutorial: Republishing an MQTT message - AWS IoT Core
Using shadows in apps and services - AWS IoT Core
answered 2 years ago
Relevant content
asked 3 years ago

Why should 'controllerId' be the third element of the topic? That doesn't make sense to me.