Tip: You are viewing the latest version of the DRS docs. To access the legacy APIs click here
An AWS Lambda function is responsible to inform the Alexa Smart Home cloud of new connected devices that have been discovered and any subsequent changes to the inventory of these devices.
Using the AWS Lambda function you can send two types of information required by the Alexa cloud for devices that have inventory sensors:
A Discovery response (proactive or reactive) that adds the DRS capability for a specific device
Any subsequent inventory update
If your connected devices already use Smart Home APIs within a Lambda function, you may choose to use the same function and expand the existing Discover.Response.
If you haven't read the Understanding Device Sensors (Alexa.InventorySensors) section click here before you start.
Creating the Lambda function
Note: Only perform these steps if you don't have a Lambda function using Alexa Smart Home APIs yet
Sign in to the AWS Management Console and open the AWS Lambda console (Opens in a new tab).
Make sure you've selected:
N.Virginia for English (US)
EU (Ireland) region for English (UK), French (FR), German, Italian, Spanish (ES)
On the Create function page, choose Author from scratch
Select the Function name and the runtime (in this guide we will use Node.JS). Click on Create function
Select Add Trigger and search for Alexa Smart Home
Go to your Alexa console (Opens in a new tab), select the Smart Home skill you created at the beginning of this process, click on the Smart Home model and copy your skill Id
Keep this tab open.
Go back to the Trigger page of your Lambda function and paste the skill Id in the Application ID field. Click Add
You may now copy the ARN visible on the top right part of the AWS console.
Go back to the Alexa console, in the Smart Home section of your skill and paste the ARN in the Default endpoint field.
If you have regional endpoints for your Smart Home skill you can choose to change the endpoint to target customers who are closest to it.
1. Discovering the device
The Lambda function will be using the Smart Home Skill API to provide capability interfaces that enable you to describe your devices and the properties, events, and directives that they support. You describe a device to Alexa by sending a Discover.Response event after receiving a Discover directive from Alexa, or by proactively sending an AddOrUpdateReport to Alexa for any device already discovered.
Discovering the device upon account linking
The initial configuration established through a Discover.Response can be triggered by the Discover directives upon account linking (i.e. when a customer enables the manufacturer's Smart Home skill). In simple terms, this means that when the customer enables your Smart Home skill, Alexa will send a Discover directive to your Lambda function which will trigger a Discover.Response back.
The following diagram illustrates the exchange between the Alexa cloud and the manufacturer cloud (your cloud) when a customer enables your Smart Home skill.
Building the main logic
We will start by building the basic structure to handle our reactive Discover.Response.
Important: This page includes code that provides a starting point for implementing a smart home skill and provides the basic structure for responding to a discovery. You must add code to respond to ReportState directives and code specific to your device types and the interfaces that your devices support. You can find additional sample code in the GitHub repository for Alexa smart home.
In the Lambda function code section make sure Edit code inline is selected. Leave the Runtime and Handler set to their defaults.
Paste in the following code, completely replacing the code in index.js.
exports.handler=function(request,context){if(request.directive.header.namespace==='Alexa.Discovery'&&request.directive.header.name==='Discover'){log("DEBUG:","Discover request",JSON.stringify(request));handleDiscovery(request,context);}functionhandleDiscovery(request,context){varpayload={"endpoints":[{"endpointId":"appliance-001",// This can be a device serial number to help you identify it uniquely"friendlyName":"Printer","description":"Printer by Sample Manufacturer","manufacturerName":"Sample Manufacturer","displayCategories":["OTHER"],"cookie":{},"capabilities":[// Capabilities array]}]};varheader=request.directive.header;header.name="Discover.Response";log("DEBUG:","Discovery Response: ",JSON.stringify({header:header,payload:payload}));context.succeed({event:{header:header,payload:payload}});}functionlog(message,message1,message2){console.log(message+message1+message2);}};
Click Save
The above sample code will do two things:
Listen for a Discover directive
Respond with an Alexa.Discovery response
The content of the Reactive Discovery response will include a predefined header that will help the Alexa cloud identify the intent of the request:
The payload object will include an array of endpoints you wish Alexa to discover, each including device-specific attributes to help the Alexa cloud display the appropriate device information within the Alexa app. A single Discovery Response may contain multiple endpoints (e.g. a customer with two printers) that can be bundled in one single request:
"endpoints":[{"endpointId":"appliance-001",//Thiscanbeadeviceserialnumbertohelpyouidentifyituniquely"friendlyName":"Printer","description":"Printer by Sample Manufacturer","manufacturerName":"Sample Manufacturer","displayCategories":["OTHER"],"cookie":{},"capabilities":[//Capabilitiesarray]}]
When a smart device consumes inventory, you must inform the Alexa cloud of the change so that DRS can update and accurately track the overall inventory for the customer. There are currently two ways to perform this action, depending on the type of sensor you wish to update:
Level sensor can send the level through a ChangeReport (proactively)
Usage and LevelUsage sensors can use the InventoryConsumed event .