Contents

AWS IoT Thing Type

Overview

AWS IoT Thing Type helps organize, categorize, and search for IoT Things managed by the AWS IoT Thing Registry.

This article is a continuation of my previous post - AWS IoT Thing Attributes - introduction

All Things associated with a specific Thing Type have common attributes (like: manufacturer, serial number, color).

To manage Thing Types we will use the AWS SDK for Python (Boto3).

1
2
3
4
5
6
7
8
9
import boto3
import json

# A session manages state about a particular configuration.
# I recommend you specify profile and region - that is a good development practice.
session = boto3.Session(profile_name='train',region_name='eu-west-1')

# Obtain the IoT Client.
iot_c = session.client('iot')

Create Thing Type

Let’s try to create a Thing Type ConnectedBulbs with the following attributes:

  • manufacturer
  • serial number
  • production_date
  • color
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
thing_type_name = 'ConnectedBulbs'
thing_type_description = 'My Connected Bulbs'

iot_c.create_thing_type(
    thingTypeName = thing_type_name,
    thingTypeProperties = {
        'thingTypeDescription': thing_type_description,
        'searchableAttributes': [
            'manufacturer',
            'serial_number',
            'production_date',
            'color'
        ]
    }
)

Output:

1
InvalidRequestException: An error occurred (InvalidRequestException) when calling the CreateThingType operation: Only three searchable attributes are allowed for a thing type.

Only three searchable attributes are allowed to be defined during Thing Type creation.

Let’s reduce the number of attributes:

  • manufacturer
  • serial number
  • production_date
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
iot_c.create_thing_type(
    thingTypeName = thing_type_name,
    thingTypeProperties = {
        'thingTypeDescription': thing_type_description,
        'searchableAttributes': [
            'manufacturer',
            'serial_number',
            'production_date'
        ]
    }
)

💡Note: Thing Types are immutable. You cannot change the properties of a Thing Type after it has been created.

Thing of a Type

Let’s create a sample IoT Thing of this Type.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
iot_c.create_thing(
    thingName = "Bulb001",
    thingTypeName = thing_type_name,
    attributePayload = {
        'attributes': {
            'manufacturer': "GreatCorp",
            'serial_number': "1.2.3.4.5",
            'production_date': "2018.02"
        }
    }
)

This Thing has an assigned Type, so it can have up to 50 attributes (Things without a Type can have maximum 3 attributes).

💡Note: during Thing creation you can define maximum 3 attributes (even for Things with a Thing Type)! If you want to add additional attributes you need to invoke update_thing function:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
iot_c.update_thing(
    thingName = "Bulb001",
    attributePayload = {
        'attributes': {
            'color': 'white',
            'power': '20W'
        },
        'merge': True # Specifies whether the list of attributes provided in the AttributePayload is merged with the attributes stored in the registry, instead of overwriting them.
    }
)

AWS IoT Thing Attributes
AWS IoT Thing Attributes

Searchable attributes can be used to filter lists of things without using fleet indexing.
Non-searchable attributes can be used to find things, but only when fleet indexing is turned on.

I will cover the Fleet indexing service next time, now let’s focus on Thing Type and Thing Attributes.

We can create many Things of the same Type:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
for i in range(2,5):
    r = iot_c.create_thing(
        thingName = f"Bulb{i:03d}",
        thingTypeName = thing_type_name,
        attributePayload = {
            'attributes': {
                'manufacturer': "GreatCorp",
                'serial_number': f"{i}.2.3.4.5",
                'production_date': f"2018.{i:02d}"
            }
        }
    )
    print(r)

Searching for Things

We can easily list all Things of a specific Type:

1
2
3
iot_c.list_things(
    thingTypeName = thing_type_name
)

This functionality enables the execution of jobs across all devices (represented by IoT Things) of a specific type. I will cover IoT Jobs and fleet management in the future.

Let’s try to find a specific IoT Thing: for example with production_date = '2018.03'.

1
2
3
4
iot_c.list_things(
    attributeName = 'production_date',
    attributeValue = '2018.03'
)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
  ...
 'things': [{'thingName': 'Bulb003', // this is the Thing we were looking for
   'thingTypeName': 'ConnectedBulbs',
   'thingArn': 'arn:aws:iot:eu-west-1:693854281758:thing/Bulb003',
   'attributes': {'manufacturer': 'GreatCorp',
    'production_date': '2018.03',
    'serial_number': '3.2.3.4.5'},
   'version': 1}]
}

Now let’s try to find a Thing using non-searchable attribute (for instance power):

1
2
3
4
iot_c.list_things(
    attributeName = 'power',
    attributeValue = '20W'
)
1
2
3
4
{
  ...
  'things': [] // no IoT Things were returned
}

💡Note: Design your system is a way that you can find IoT Things using searchable attributes. Once you find your Thing, you can review all attributes:

1
2
3
iot_c.describe_thing(
    thingName = 'Bulb001'
)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
  ...
 'defaultClientId': 'Bulb001',
 'thingName': 'Bulb001',
 'thingTypeName': 'ConnectedBulbs',
 'attributes': {'color': 'white', // searchable and non-searchable attributes
  'manufacturer': 'GreatCorp',
  'power': '20W',
  'production_date': '2018.02',
  'serial_number': '1.2.3.4.5'},
 'version': 3
}

Delete a Thing Type

Before you can delete a Thing Type, you have to Deprecate it.

1
2
3
iot_c.deprecate_thing_type(
    thingTypeName = thing_type_name
)

Now let’s try to delete this Thing Type:

1
2
3
iot_c.delete_thing_type(
    thingTypeName = thing_type_name
)
1
2
InvalidRequestException: An error occurred (InvalidRequestException) when calling the DeleteThingType operation:
Cannot delete thing type : ConnectedBulbs. Please wait for 5 minutes after deprecation and then retry

Let’s wait 5 minutes and try once again.

1
2
3
iot_c.delete_thing_type(
    thingTypeName = thing_type_name
)
1
2
InvalidRequestException: An error occurred (InvalidRequestException) when calling the DeleteThingType operation:
Can not delete thing type ConnectedBulbs with things associated with it

💡Note: You can delete a Thing Type, but only if it was deprecated over 5 minutes and not used by any IoT Thing.

Let’s remove all IoT Things of this Type:

1
2
3
4
for i in range(1,5):
    iot_c.delete_thing(
        thingName = f"Bulb{i:03d}"
    )

and finally delete our Thing Type:

1
2
3
iot_c.delete_thing_type(
    thingTypeName = thing_type_name
)
1
'HTTPStatusCode': 200

Summary

AWS IoT Thing Type helps to organize and categorize similar IoT Things (for instance bulbs); it enables efficient management of a fleet of devices in a huge scale, production deployments.

IoT Thing with a specified Type can have up to 50 attributes (3 searchable and 47 non-searchable).

Support quality content❤️ Donate💰

Sign up for news: (by subscribing you accept the privacy policy)