This SmartApp hosts a web page that allows the control of scenes, locks, and switches. It's an example of how to:
- Execute scenes
- Execute device commands
- Subscribe to device state change events by capability
- Store the auth tokens and settings of installed app instances and use them when making calls to the SmartThings API that are not in response to SmartApp lifecycle events.
- lib/db.js — module for listing installed app instances from the context store
- lib/smartapp.js — the SmartApp implementation
- lib/sse.js — Server-Sent Event object used to update the web page
- locales/en.json — English version of the app configuration page text
- public/images — web image files
- public/javascript — web page JavaScript files
- public/stylesheets — web page css files
- server.js — the Express server that hosts the SmartApp as a web-hook
- views/index.ejs — the home page
- views/isa.ejs — the installed app instance control page
-
ngrok or similar tool to create a secure tunnel to a publically available URL
git clone https://github.com/SmartThingsCommunity/device-scene-example-nodejs.gitcd device-scene-example-nodejs
npm install
node server.jsngrok http localhost:3001
Make note of the HTTPS forwarding URL, for example https://c79461932dfc.ngrok.io
Go to the SmartThings developer workspace and create an Automation SmartApp. This app should have the scopes:
r:devices:*
x:devices:*
r:scenes:*
x:scenes:*
Choose the web-hook option when creating the app. The targetURI should be set to the ngrok forwarding
URL, for example https://c79461932dfc.ngrok.io
Take note of the appId, clientId, and clientSecret displayed after creating your app.
Look in your server log for a line with a URL to use for verifying your app. Visit this URL to confirm the location of your app (you can do this in a web browser).
Create a file named .env in the project directory that sets your appId, clientId, and clientSecret.
For example:
APP_ID=912e0214-5706-4407-a299-b3796a57cf56
CLIENT_ID=61225bef-d2db-4ab0-82f6-d28c0f11911d
CLIENT_SECRET=94c2b3df-b0ab-4abd-84bf-1823d63b944c
and restart your NodeJS server. Do not restart ngrok or the URL will change (unless your are using a paid account)
Install your SmartApp using the SmartThings mobile app. Then visit your local web server to see and control devices and scenes.