How to install a device with the smartthings-core-sdk?

How do you install/create a new device with the core-sdk?

Using the following I keep getting the “Invalid device creation data” message back.

var payloadCreateDeviceData = `{
	"label": "` + deviceLabel + `",
	"roomId": "` + IDroom + `",
	"locationId": "` + IDlocation + `",
	"app": {
		"installedAppId": "` + IDapp + `",
	  	"profileId": "` + IDprofile + `",
	  	"externalId": "` + IDexternal + `",
	}
}`

client.devices.create(payloadCreateDeviceData)

@lightning20mph

Check if this gist helps:

Just make sure to follow the last comment regarding publishing the device profile.

Cheers.

1 Like

Erick,

Thanks for the info.
I’ve successfully created the device profile.

How do I then use the profile to create the “virtual device” through the core-SDK?

The command in this line of Erick’s sample is the one that creates the device based on the profile you created, you just need to put its ID in the profile property of deviceOpt:

The example provided is using the smartapp SDK where as I’m using the core-SDK.

What is needed to set the “definition.profileId” to TRUE in the devices.js?

create(definition) { 
        return __awaiter(this, void 0, void 0, function* () {
            let data;
            if (definition.app) {
                data = {
                    label: definition.label,
                    roomId: definition.roomId,
                    locationId: this.locationId(definition.locationId),
                    app: Object.assign(Object.assign({}, definition.app), { installedAppId: this.installedAppId(definition.app.installedAppId) }),
                };
            }
            else if (definition.profileId) {
                data = {
                    label: definition.label,
                    roomId: definition.roomId,
                    locationId: this.locationId(definition.locationId),
                    app: {
                        installedAppId: this.installedAppId(definition.installedAppId),
                        profileId: definition.profileId,
                        externalId: definition.externalId,
                    },
                };
            }
            else {
                throw Error('Invalid device creation data');
            }
            return this.client.post('', data);
        });

Yes, the SmartApp SDK is based on the Core SDK.
The command of ctx.api.devices.create(deviceOpt) is actually using the create function in devices.ts of the Core SDK, so, the call is different but it should work using the same object as the sample. Something like:

const {SmartThingsClient, BearerTokenAuthenticator} = require('@smartthings/core-sdk')
const client = new SmartThingsClient(new BearerTokenAuthenticator('{YOUR-PAT-TOKEN}'))

let deviceOpt = {
      label: deviceLabel,
      locationId: locationId,
      app: {
        profileId: 'xxxx-xxxx-xxx', //A real profile ID
        installedAppId: installedAppId //A real installedAppId
      }
};

client.devices.create(deviceOpt).then(locations => {
    console.log("Device created")
})

However, I’m not sure if using the Core SDK you got an installedAppId because it is needed for the device creation.
About the condition you mention, I believe it will work if all the necessary properties like label, roomId, profileId are the main keys of the JSON object, you can see this based on the references to the values, for example: definition.roomId where definition is the parameter in create()