How to get thing name from MQTT topic in AWS Iot Analytics pipeline?

1.4k views Asked by At

I have created the following in AWS Iot Analytics:

  • Channel: Iot Core topic pattern => $aws/things/+/shadow/update/accepted
  • Pipeline: Fed from previous channel, selects attributes from message
  • Datastore to save messages

The incoming message to the channel is a shadow update/accepted and looks like this:

{
  "state": {
    "reported": {
      "active": false,
      "telemetry": {
        "temperature": 72,
        "humidity": 58,
        "occupancy": 28
      },
      "config": null
    }
  },
  "metadata": {
    "reported": {
      "active": {
        "timestamp": 1533059587
      },
      "telemetry": {
        "temperature": {
          "timestamp": 1533059587
        },
        "humidity": {
          "timestamp": 1533059587
        },
        "occupancy": {
          "timestamp": 1533059587
        }
      },
      "config": {
        "timestamp": 1533059587
      }
    }
  },
  "version": 89,
  "timestamp": 1533059587,
  "clientToken": "..."
}

I want to extract the thing name from the topic structure, which I have as a wild card here:

$aws/things/+/shadow/update/accepted

Is there any way to do this or must it be included in the message body directly if I want to use it here for processing?

Thanks.

3

There are 3 answers

1
ms-schneider-electric On BEST ANSWER

Apparently this is done by enriching the payload that leaves IOT Core by adding to the SQL statement in the IOT Core Rule that transmits payloads to IOT Analytics.

0
R.W On

There is an accepted answer but it seems to have missed out on this part of the question which is

I want to extract the thing name from the topic structure, which I have as a wild card here

$aws/things/+/shadow/update/accepted

I thought I'd post this for those who need to extract the thingname. If you want to extract the thing name then you can use topic(3) as it evaluates to 3 (which is the position of the thing name in the topic). Here is an example for a rule which republishes to a topic that uses the thingname which is retrieved from the topic.

{
  "sql": "SELECT state, version FROM '$aws/things/+/shadow/update/delta'",
  "ruleDisabled": false,
  "actions": [
    {
      "republish": {
        "topic": "${topic(3)}/delta",
        "roleArn": "arn:aws:iam::123456789012:role/my-iot-role"
      }
    }
  ]
}

Here is the link to the above example. "topic()" returns the complete topic. These are called Substitution templates.

Also one important thing to note (which may lead to some unnecessary debugging time) is that to republish to a reserved topic, which begins with $, use $$ (escape the '$' using another '$'). For example, to republish to the device shadow topic $aws/things/MyThing/shadow/update, specify the topic as $$aws/things/MyThing/shadow/update

0
atlascoder On

Use topic() function inside SQL statement:

SELECT *, topic() AS topic FROM 'your/thing/+/topic'

https://docs.aws.amazon.com/en_us/iot/latest/developerguide/iot-substitution-templates.html