This example Lambda SmartApp demonstrates the use of SmartThings APIs via the SmartApp NodeJS SDK to achieve an Automation.
It showcases:
- App installation and configuration flow.
- Integrating with a third-party API (OpenWeather).
- Actuating devices using the SmartThings API.
- Creating schedules and handling scheduled executions.
- An AWS account (free tier is fine).
- An OpenWeather account (free tier is fine).
- A Samsung account and the SmartThings mobile application.
- A Developer Workspace account.
- A SmartThings compatible color bulb, such as SYLVANIA Smart RGBW, LIFX, or Phillips Hue.
- Node.js and npm installed (verified with npm version 6.14.8 and Node 12.19.0).
- ngrok installed to create a secure tunnel and create a globally available URL for fast testing.
We've provided two options: the intended deployment platform for this automation (AWS Lambda) and a simple web server that can be used to run and test locally. In either case, first create an API key at OpenWeather. We will use this in both options below.
Clone or download this repository and follow the desired option.
-
Install the dependencies for this app:
npm install
. -
Follow the instructions to setup AWS credentials for serverless.
-
Deploy the Lambda function:
serverless deploy
. -
Navigate to the AWS Lambda dashboard to add an env var with the key
WEATHER_API_KEY
and the value as the API key you created at OpenWeather. -
Follow the steps to grant SmartThings permission to execute your Lambda function. This is required for successful registration.
-
Create a
.env
and store your OpenWeather API key as shown in.env.example
file. -
Install the dependencies for this app:
npm install
. -
Start the server:
npm start
. -
Start ngrok (in another terminal window/tab):
ngrok http 3005
. Copy thehttps:
URL to your clipboard.
Follow the instructions for registering a SmartApp with the SmartThings platform.
- The following OAuth2 scopes are required.
r:devices:*
x:devices:*
A CONFIRMATION request
log should show in the log output of the local server once registered. Navigate to this link to verify your domain ownership and enable the app to receive events. This is required for successful installation.
Follow the instructions for testing a SmartApp.
Once installed, the configured bulb will turn on and its color will either be purple (if precipitation is in the forecast), orange (if the forecast calls for temperatures above 80 degrees Fahrenheit), blue (if the forecast calls for temperatures below 50 degrees Fahrenheit), or white (if no precipitation and temperature between 50 and 80 degrees Fahrenheit). It will check the current weather forecast at the interval set during installation.
- When installing the SmartApp in the SmartThings mobile app, if you get an error Something went wrong. Please try to install the SmartApp again, then it is possible that you did not navigate to the confirmation link as specified above. If this is the case, then in the npm server terminal you will also see an error. Make sure you navigate to the URL sent with the
CONFIRMATION request
to the npm server. This can be resent by navigating to Developer WorkspaceOverview
and clickingVerify App Registration
.
- Documentation for developing SmartApps can be found on the SmartThings developer portal.
- SmartThings API reference documentation
- SmartApp API reference documentation
The concept of a SmartThings-connected color bulb that changes its color based upon weather or other environmental data is not new or original to this example. The SmartThings Community has created several similar solutions in the past, including: