Skip to content

IoT Rule to trigger action when device shadow property changed

0

I'd like to create a rule that runs when a change is introduced to a particular property under device shadow. Let's take batteryPercentage as an example (original properties are different).

SELECT
    topic(3) AS id,
    state.reported.deviceSettings,
    state.reported.env
FROM '$aws/things/+/shadow/update/accepted'
WHERE
    isNull(state.reported.deviceSettings.batteryMode) = false

Contrary to my assumption this rule executes for all messages that update device shadow as soon as device shadow contains batteryMode key under deviceSettings. Is there a way to do comparison based on the message received from the topic?

E.g: I see from MQTT Test Client that an update like this invokes this rule:

{
  "state": {
    "desired": {
      "deviceSettings": {
        "txId": null
      }
    },
    "reported": {
      "deviceSettings": {
        "txId": null
      }
    }
  },
  "metadata": {
    "desired": {
      "deviceSettings": {
        "txId": {
          "timestamp": 1729894985
        }
      }
    },
    "reported": {
      "deviceSettings": {
        "txId": {
          "timestamp": 1729894985
        }
      }
    }
  },
  "version": 19003,
  "timestamp": 1729894985
}

I've considered using shadow/documents

SELECT
    topic(3) AS id,
    current.state.reported.deviceSettings,
    current.state.reported.env
FROM '$aws/things/+/shadow/update/documents'
WHERE
    isUndefined(current.state.reported.deviceSettings.batteryMode) = false
    AND previous.state.reported.deviceSettings.batteryMode != current.state.reported.deviceSettings.batteryMode

But this doesn't consider cases where a message is published that doesn't change the batteryMode. A message that contains batteryMode and other properties should still trigger the rule. I'm just interested in batteryMode included in the update json.

asked 2 years ago422 views

1 Answer
-2

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:

  1. Use the '$aws/things/+/shadow/update/accepted' topic, as it contains the changes made to the shadow.

  2. In the SQL statement, use the 'state.reported' path to access the reported state changes.

  3. 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:

  1. It triggers when 'batteryMode' is included in the update, even if other properties are also present.
  2. It doesn't trigger for shadow updates that don't include 'batteryMode'.
  3. 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

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.