Create a new Automation
Automations allow a user to control their SmartThings ecosystem without manual intervention. An example of an Automation is a WebHook or AWS Lambda function that uses the SmartThings REST API to control and get status notifications from SmartThings devices.
This article demonstrates how to create an Automation that sets the color of a light based on the weather in a given zip code. This Automation is a WebHook SmartApp.
We will cover:
- SmartApp installation and configuration flow.
- HTTP Signature verification to ensure that the incoming requests are from SmartThings.
- Integrating with a third-party API (Weather API, in this case).
- Actuating the devices using the SmartThings API.
- Creating schedules and handling scheduled executions.
- Node.js and npn installed (verified with npm version 4.0.5 and Node 7.4.0).
- ngrok installed to create a secure tunnel for a globally available URL for fast testing.
- Docker for running Redis. Alternatively, install and run Redis manually.
- A Samsung account and the SmartThings app.
- A SmartThings-compatible color bulb, such as SYLVANIA Smart RGBW, LIFX, or Philips Hue.
Step 1. Download the sample code
- Download the Weather Color Light SmartApp on GitHub.
- Create an API key with Open Weather Map (free tier is fine), and store it in an environment variable named
Step 2. Run WebHook service
- Install the dependencies for this app:
- Start the server:
- Start ngrok (in another terminal window/tab):
ngrok http 3005. Copy the
https:URL to your clipboard.
Step 3. Register Automation in Developer Workspace
You can now register your Automation in Developer Workspace.
- Select WebHook endpoint.
- For the Target URL enter the
https:URL you copied in Step 2.
- For the App Scope, select the following:
After saving your Automation info, return to the Automation page to find the following information:
- Copy the Public Key and replace the contents of the file
Step 4. Restart your WebHook service
- Stop the npm server:
- Start the server again:
npm start(this ensures that the public key will be used to verify requests from SmartThings).
Step 5. Test your Automation
Start testing your device with the SmartThings app.
- Make sure you enable the Developer Mode.
- Go to the Automations tab in the SmartThings app and add your Automation. After a moment you will see the Example Weather Color Light app. Tap on it to install it.
- Enter all the required inputs on the configuration screens.
- Once installed, the configured bulb will turn on and its color will be either:
- 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). The app will check the current weather at the interval set during installation.
- When installing the SmartApp, if you get the error Something went wrong. Please try to install the SmartApp again, then:
- Make sure you restart the npm server as specified in Step 4 above. If you did not restart the npm server, you will see this error in the npm server terminal:
forbidden - failed verifySignature.
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, including: