Create an 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.

Automation

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).
  • Controlling the devices using the SmartThings API.
  • Creating schedules and handling scheduled executions.

Prerequisites

  • Node.js and npm 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

  1. Download the Weather Color Light SmartApp on GitHub.
  2. Create an API key with Open Weather Map (free tier is fine), and store it in an environment variable named WEATHER_API_KEY.

Step 2. Run WebHook service

  1. Install the dependencies for this app: npm install.
  2. Start the server: npm start.
  3. 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. From the project creation page on Developer Workspace, select Automation.

  • Name your Automation and click CREATE PROJECT.
  • In the Develop menu, click Automation to register your Automation. The Hosting tab will open.
  • Select the WebHook Endpoint hosting type.
    • For the Target URL enter the https: URL you copied in Step 2.
  • Click Next to open the App Scope tab.
    • For the App Scope, select the following OAuth2 scopes:
      • r:devices:*
      • x:devices:*
  • Click Next to open the App Settings tab.
  • Enter an Automation Display Name to be shown in the SmartThings app.
  • Select whether you will allow single or multiple SmartApp Instances.
    • Multiple instances allows users to install this SmartApp more than once, with different configurations in the same location.
    • Single instance allows users to install this SmartApp once, using a single configuration in one location.
  • Add any Custom Parameters used in your SmartApp code.
  • Click SAVE.

After saving the above information, you can return to the Automation page to access the public key, client ID, and client secret for this SmartApp.

SmartApp Connector info

Copy the Public Key and replace the contents of the file config/smartthings_rsa.pub with it.

Step 4. Restart your WebHook service

  1. Stop the npm server: CTRL-C.
  2. 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.

  1. After adding your Automation in the app, you will see the Example Weather Color Light app. Tap on it to install it.
  2. Enter all the required inputs on the configuration screens.
  3. 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.

Troubleshooting

  • 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.

Credits

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:

See also