Migrate Elder care app & Google sheets logger

nayelyz,
I closed the app and reopened it. Now I see the smartapp on my phone, both in iPhone and samsung android.

Is this app permanentley installed now.

Is it in the cloud or locally on my hub.

Why doesn’t this show up on smartthing IDE or with https://api.smartthings.com/v1/rules?

I doesn’t show up in postman GET https://api.smartthings.com/v1/rules?locationId=xxxxxxxx either

If this installs smartapps on the hub to run locally, I have two other needs.

I need an elder care app like SmartThingsPublic/smart-care-daily-routine.groovy at master · SmartThingsCommunity/SmartThingsPublic · GitHub

and I need a google sheets logger app like googleDocsLogging/google-sheets-logging.groovy at master · loverso-smartthings/googleDocsLogging · GitHub

Can these be installed to run local on my hub?

Yes, it is. Unless you delete it from the app or using the API.

SmartApps run in the cloud, only Rules (API) or Routines that use the Rules engine can work locally depending on them using locally executing devices and features.

AFAIK, they are not supported in the IDE. In the API, they should appear in the installedApps endpoint: API | Developer Documentation | SmartThings

No, that’s for Rules created using the POST method in the same endpoint.

No, SmartApps are not supported to run locally and Edge drivers cannot access Public Internet which is needed for the Google sheets app. The other one seems to be only using events from devices and schedules so, I don’t think there’s an issue with it.

nayelyz,

how do I get elder care and google logger running on smartthings after IDE is gone.

I don’t want to have to use IFTTT.

Hi, @wptracy

I am Andres, a member of the Developer Support team.

Sorry for the late response. Let me check this with the team and get back to you.

1 Like

Hi, @wptracy

Sorry for the delay. I had to go through the code of the Google sheets logger app and implement one on my side to see if it was possible using a SmartApp in the “new platform”.
First, as a reference SmartApps that use the NodeJS SDK can also:

  1. Create subscriptions to device events and others
  2. Send commands to devices from there depending on the scopes of its Access Token which you define during installation/update.
  3. Send REST requests as they use NodeJS.

I was able to implement a simple SmartApp to log events into a Google sheet, there are other functionalities in the original one but I omitted them for testing purposes.
Here, you’ll find the SmartApp definition I used:

As evidence, here’s the data registered in the Google Sheet compared to the device history:
image image

2 Likes

Thanks nayelyz,

This is going to take me a while to look thru and understand.

Thank you very much for all this information.

I’m sure I’ll be coming back to you for more information. I hope that’s OK.

2 Likes

Nayelyz,

Where is this script supposed to reside. On a server in my house, like my raspberry pi?

How does it get called to run.

Do I need to install @smartthings/smartapp on my raspberry pi? And also express and body-parser and axios?

And how do I get the smartapp on my phone. Is that done with Glitch.

You can host a SmartApp in two ways:

  1. As a Webhook - it would be basically a server that can be called. For example:
    In this tutorial, we use Glitch as the host to run our NodeJS code: Simple SmartApp Tutorial (SmartApp SDK)
    In my case, I started the server on my local machine and used NGROK to generate an HTTPS tunnel to the corresponding port.
    The URL you register in the Dev Workspace is the one where you can access the code in execution.

In this case, you need to install Node on your Raspberry, create the folder for your NodeJS project and install the dependencies for it to run.
You can download this sample, copy it to your raspberry, and follow the instructions in Start > Local:

  1. AWS > Lambda function. This requires an AWS account and several configurations to upload it (AWS CLI) and execute it.
    Here’s an example of a SmartApp prepared to upload it to AWS: GitHub - SmartThingsCommunity/smartapp-example-no-devices-nodejs-lambda: Give Lambda SmartApps a try without any physical devices.

The SmartApp will appear in Routines > add > Discover for you to install once you:

  1. Register it in the Developer Workspace
  2. Deploy it to test (also in the Dev WS)
  3. Enable the developer mode in the SmartThings app.
1 Like

Thanks Nayelyz,

I’ll try this and get back to you.

Nayelyz,

I’ve run the Simple SmartApp Tutorial (SmartApp SDK) twice. I installed the app on the phone and it worked for a day or so.

Why does it stop working.


This is the package.json

{
“name”: “smartthings-open-close-lighting”,
“version”: “0.0.1”,
“description”: “A simple example automation app that turns lights on when a door opens and off when it closes.”,
“main”: “server.js”,
“scripts”: {
“start”: “node server.js”
},
“dependencies”: {
“express”: “^4.17.1”,
@smartthings/smartapp”: “^2.1.0”
},
“engines”: {
“node”: “8.x”
},
“repository”: {
“url”: “Glitch :・゚✧
},
“license”: “Apache-2.0”,
“keywords”: [
“node”,
“glitch”,
“express”,
“smartthings”,
“smartapp”
]
}

This is the server.js

‘use strict’;

const express = require(‘express’);
const bodyParser = require(‘body-parser’);
const SmartApp = require(‘@smartthings/smartapp’);

const server = module.exports = express();
server.use(bodyParser.json());

const app = new SmartApp()

/* Only here for Glitch, so that GET doesn’t return an error */
server.get(‘/’, (req, res) => {
res.send(‘Simple SmartApp Example URL: https://’+ req.hostname);
});

/* Handles lifecycle events from SmartThings */
server.post(‘/’, async (req, res) => {
app.handleHttpCallback(req, res);
});

/* Defines the SmartApp */
app.enableEventLogging() // Log and pretty-print all lifecycle events and responses
.configureI18n() // Use files from locales directory for configuration page localization
.page(‘mainPage’, (context, page, configData) => {
page.section(‘sensors’, section => {
section.deviceSetting(‘sensor’).capabilities([‘contactSensor’]).required(true);
});
page.section(‘lights’, section => {
section.deviceSetting(‘lights’).capabilities([‘switch’]).multiple(true).permissions(‘rx’);
});
})
.updated(async (context, updateData) => {
await context.api.subscriptions.unsubscribeAll();
return Promise.all([
context.api.subscriptions.subscribeToDevices(context.config.sensor, ‘contactSensor’, ‘contact.open’, ‘openDeviceEventHandler’),
context.api.subscriptions.subscribeToDevices(context.config.sensor, ‘contactSensor’, ‘contact.closed’, ‘closedDeviceEventHandler’)
])
})
.subscribedEventHandler(‘openDeviceEventHandler’, (context, deviceEvent) => {
return context.api.devices.sendCommands(context.config.lights, ‘switch’, ‘on’);
})
.subscribedEventHandler(‘closedDeviceEventHandler’, (context, deviceEvent) => {
return context.api.devices.sendCommands(context.config.lights, ‘switch’, ‘off’);
});

/* Starts the server */
let port = process.env.PORT;
server.listen(port);
console.log(Open: http://127.0.0.1:${port});

when you say it stops working, do you mean it doesn’t execute the commands after a while, or only that it’s not opening?
If you’re using Glitch, it’s possible it stops working because the free version has a sleep time, it’s the second point in this picture:
image
Do you get any logs in the Glitch project?

Also, I don’t know if the issue in iOS was solved but a while ago, there was an issue where we could see two options of the same SmartApp, do you see only one?

I used the Simple SmartApp Tutorial (SmartApp SDK) to install the Simple SmartApp on my iPhone Smartthings App.

When I touch the Simple SmartApp, like in the screenshots above, I get a popup that says Something went wrong. I used the Tutorial , which uses Glitch, to do it again. The second try also stopped working a few days later.

No, the Glitch project works fine and the App installs on the phone with the procedure in the tutorial. Well the tutorial is missing something because to make it work I have to enter a URL that Glitch complains about before the Developer Workspace and Glitch cooperate. I would have to do it again to document the step where tutorial leaves out a step.

The app worked OK on the iPhone, but a day or two later it stopped working with the popup error that says something went wrong. Do smartapps need a server to run on? Is that why the smartapp on my phone stops working. Because Glitch expired my smartapp on their machine?

When I open the smartthings app on the Samsung phone I don’t see the Simple SmartApp. Shouldn’t it appear on the Samsung phone too?

I thought the smartapp was supposed to run locally on the HUB without Glitch.

Isn’t there a way to install SDK on my raspberry pi.

Would it serve the same function as Glitch.

Todd Austin has a procedure to implement SmartApps on a local Raspberry Pi using ngrok. GitHub - toddaustin07/MQTT_SmartApp: SmartThings SmartApp for Sending out device capability state updates to MQTT .

Do I need to do this to build smartapps?

Will it replace Free Glitch and integrate with developer workspace and webhook. Is that how you do this on your personal server?

Yes, they need a server to run. Glitch puts the server created for the SmartApp to sleep so, if you enter the Glitch URL of the project in a browser, it should wake it up.

It depends. Are you using the same account where you created the SmartApp project? Does the ST app in the Samsung phone has the developer mode enabled?

The SDK uses NodeJS to execute, so, you should install NodeJS and NPM in the Raspberry Pi to install the necessary packages for the project.
Here’s a tutorial I found on the web about installing NodeJS on a Raspberry Pi which could help you out: How to install Node JS and NPM on the Raspberry Pi – MakerSupplies Singapore

Please, install NodeJS and we’ll continue from there in case you have questions.

As I understand per the description in that project, that is an implementation of a SmartApp, if you check the requirements, it says you need to install NodeJS and Ngrok as well: GitHub - toddaustin07/MQTT_SmartApp: SmartThings SmartApp for Sending out device capability state updates to MQTT
So, it’s like an example but it uses the protocol MQTT which is a separate function from the SmartApp SDK.

1 Like

how do I do this? What URL. I clicked Simple SmartApp Tutorial (SmartApp SDK) and remixed it, but when I toughed the SmartThings SmartApp on my smartthings app, I get the same popup error, something went wrong.

Yes. and the Samsung phone has Developer Mode enabled.

Does this Javascript/NodeJS SDK GitHub - SmartThingsCommunity/smartapp-sdk-nodejs: Javascript/NodeJS SDK to create SmartThings SmartApps serve the same function as Glitch? Is this how you ran googlesheets code?

This is what I want to do. How do you do that?

Ok, I just made a test and it is because of Glitch. When you register the SmartApp, you use the URL in “Live Site” and it is the one you should use to wake up the app you created but, I recently tried and it is not waking up even using the URL in the browser, it just keeps loading, so, I think this won’t be solved unless you use the premium version.

This is really weird. You’re not using shared locations or something similar, right? I’ve seen sometimes they don’t appear because we’re not the owners. So, you need to use the same account and be in the same location. Try clearing the app’s cache on the Samsung phone (Android):

Go to settings > apps > find the ST app > storage > clear app cache

No, as its name says it is only a Software Development Kit, it’s like a library you can use, but it doesn’t work as a host/server by itself.
What I did which in general would be something like in Raspberry is:

  1. I use my computer to make tests, therefore, I already had NodeJS installed.
  2. I copied the content of the files in the sample and put them in a folder in the same structure. I mean these ones:
    image
  3. Then, I used the command npm install to install the dependencies included in the package.json file.
  4. Then, used the new SmartApp definition I shared above.
    a. This required me to use the command below to install axios:

    npm install axios

Note: Those commands are executed in the command line inside the project folder. You can install Visual Studio Code which provides a built-in terminal for this purpose. For example, this is the path of my project shown in the VS Code’s terminal:
image

  1. After everything is complete, I followed the instructions to create the Google sheets scripts shown in the original repository you shared and used the corresponding keys needed.
  2. Then, to start the server, I executed the command node . and we can see it starts with the port I defined (3009):
    image
  3. Then, I start Ngrok using the same port
  4. It will provide an HTTPS URL which I copy and paste into the Dev WS:
  5. When we save it, ST will send the confirmation URL which we must paste into a web browser to verify the app.
    After this, you should be able to install and use the app normally. I cannot leave the Ngrok session open for a long time, so, I don’t have more info about the expiration in case there is one…

From Glitch :・゚✧

I downloaded .locals/en.json
package.json
server.js

I also downloaded your SmartApp definition you shared above. I think you mean the nayelyzarazua-bluetrail/app.js

Do you want me to run node . on your app.js, or the server.js.

I selected server.js a ran the node . in the terminal window. Output is below the screenshot.

Are we using the Glitch Simple SmartThings Automation App’s en.json, package.json and server.js files for this?

Wouldn’t your app.js have it’s own en.json and package.json?

I don’t think I’m following this right. I don’t get the Open: http://127.0.0.1:3009

npm install with server.js selected
wptracy@wptracy:~/GoogleSheetsLogging$ npm install
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'smartthings-open-close-lighting@0.0.1',
npm WARN EBADENGINE   required: { node: '8.x' },
npm WARN EBADENGINE   current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN deprecated messageformat@2.3.0: Package renamed as '@messageformat/core', see messageformat.github.io for more details. 'messageformat@4' will eventually provide a polyfill for Intl.MessageFormat, once it's been defined by Unicode & ECMA.

added 136 packages, and audited 137 packages in 6s

9 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
wptracy@wptracy:~/GoogleSheetsLogging$ 
npm install with app.js selected
ptracy@wptracy:~/GoogleSheetsLogging$ 
wptracy@wptracy:~/GoogleSheetsLogging$ npm install
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'smartthings-open-close-lighting@0.0.1',
npm WARN EBADENGINE   required: { node: '8.x' },
npm WARN EBADENGINE   current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }

up to date, audited 137 packages in 764ms

9 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
wptracy@wptracy:~/GoogleSheetsLogging$ node .
internal/fs/utils.js:269
    throw err;
    ^

Error: ENOENT: no such file or directory, scandir './locales'
    at Object.readdirSync (fs.js:955:3)
    at guessLocales (/home/wptracy/GoogleSheetsLogging/node_modules/i18n/i18n.js:622:22)
    at Object.i18nConfigure [as configure] (/home/wptracy/GoogleSheetsLogging/node_modules/i18n/i18n.js:152:34)
    at SmartApp.configureI18n (/home/wptracy/GoogleSheetsLogging/node_modules/@smartthings/smartapp/lib/smart-app.js:164:8)
    at Object.<anonymous> (/home/wptracy/GoogleSheetsLogging/server.js:24:6)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12) {
  errno: -2,
  syscall: 'scandir',
  code: 'ENOENT',
  path: './locales'
}
wptracy@wptracy:~/GoogleSheetsLogging$ 
wptracy@wptracy:~/GoogleSheetsLogging$ npm fund
smartthings-open-close-lighting@0.0.1
├── https://github.com/sponsors/ljharb
│   └── qs@6.11.0, side-channel@1.0.4, call-bind@1.0.2, get-intrinsic@1.1.3, has-symbols@1.0.3, object-inspect@1.12.2, minimist@1.2.7
├── https://github.com/sponsors/RubenVerborgh
│   └── follow-redirects@1.15.2
└── https://github.com/sponsors/feross
    └── safe-buffer@5.2.1

wptracy@wptracy:~/GoogleSheetsLogging$ 

wptracy@wptracy:~/GoogleSheetsLogging$ node -v
v19.3.0
wptracy@wptracy:~/GoogleSheetsLogging$ npm -v
9.2.0
wptracy@wptracy:~/GoogleSheetsLogging$ 

You should replace the content of server.js with the one in app.js, sorry for the confusion.

The en.json file is important for the UI, not the functionality of the SmartApp, it can work without it as it’s used to set human-readable titles and descriptions of each setting in the app.
The package.json will be different in any project and the most important thing to check are the dependencies, in my case, I have:

"dependencies": {
    "@smartthings/smartapp": "^2.1.0",
    "axios": "^1.2.2",
    "express": "^4.17.1",
    "dotenv": "^8.2.0"
  }

ok, this error is because the directory for the en.json file must be called “locales” not “locals”.

This is because you’re missing the .env file and that’s where you define which port you’ll use, here I left it as the sample from Glitch:
image

I copied your dotenv to my dependencies and downloaded the .env file. I had to watch the video to see that he plain text to see that.

now when I npm install and node . it works right and I get the Open:http:127.0.0.1:3000

wptracy@wptracy:~/GoogleSheetsLogging$ npm install
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'smartthings-open-close-lighting@0.0.1',
npm WARN EBADENGINE   required: { node: '8.x' },
npm WARN EBADENGINE   current: { node: 'v19.3.0', npm: '9.2.0' }
npm WARN EBADENGINE }

up to date, audited 138 packages in 566ms

9 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
wptracy@wptracy:~/GoogleSheetsLogging$ node .
Open: http://127.0.0.1:3000

Am I supposed to be using ngrok http 3009 or 3000
Your screenshot is 3009


It should be used for the current open port for your server, so, if it says “Open: http://127.0.0.1:3000”, you should use 3000. There are certain ports that we can’t use (0 to 1024) but both 3000 and 3009 are valid.

I’ll do the webhook stuff in the AM. It’s almost bed time.

This has been a fun learning experience. Thanks!!!

I’ll probably be calling again tomorrow.

1 Like