- Newest
- Most votes
- Most comments
To create an IoT rule that triggers an action when a specific property in the device shadow changes, you can modify your approach slightly. Here's a solution that should work for your use case:
-
Use the '$aws/things/+/shadow/update/accepted' topic, as it contains the changes made to the shadow.
-
In the SQL statement, use the 'state.reported' path to access the reported state changes.
-
Use the 'newinfo()' function to check if the 'batteryMode' property is present in the current message.
Here's an example SQL statement for your rule:
SELECT topic(3) AS id, state.reported.deviceSettings, state.reported.env FROM '$aws/things/+/shadow/update/accepted' WHERE newinfo().state.reported.deviceSettings.batteryMode IS NOT NULL
This rule will trigger when:
- The shadow update is accepted
- The update message contains a change to the 'batteryMode' property under 'deviceSettings'
The 'newinfo()' function returns only the information that has changed in the current update. By checking if 'batteryMode' is not null in the new information, you ensure that the rule only triggers when 'batteryMode' is included in the update, regardless of whether its value has changed or not.
This approach addresses your requirements:
- It triggers when 'batteryMode' is included in the update, even if other properties are also present.
- It doesn't trigger for shadow updates that don't include 'batteryMode'.
- It works for both new 'batteryMode' values and updates to existing 'batteryMode' values.
Remember that this rule will trigger for any update that includes 'batteryMode', not just when its value changes. If you specifically need to detect value changes, you would need to implement additional logic in your rule action or use a different approach, such as comparing the previous and current states in a Lambda function.
Sources
AWS IoT rule actions - AWS IoT Core
Using shadows in devices - AWS IoT Core
answered 2 years ago
Relevant content
asked 5 years ago
