Execute code on smartthings locally

Hi All,
sorry for a possibly noob question, I recently setup smartthings with an aqara sensor - trying to get on with my smart home.
now - I would like to create this logic, when my aqara sensor triggers an open door, I would like to retrieve an image from a network camera (using an rtsp url) and then do something with this picture (not sure what my options are… email? show on the app?)
now, to my understanding, smartapps are executed on the cloud and not locally, my cameras are local network not accessible from the outside! (192.168…) so how would I go about creating this kind of logic? Can someone please point me at the right direction?
Thanks!

Welcome to the SmartThings Community, @ranh!
If I understand you correctly, the Aqara sensor is in your SmartThings app but the cameras are just part of your local network and they cannot be added to ST?

correct, aqara door sensor is zigbee and added in the app, seems to work well.
I got IP cameras in my home network (not a brand i can add to ST) and I want to develop logic on my own to perform some kind of a HTTP GET from inside my network upon door open ( download the picture in this scenario from one of the cameras).
so first question would be - how do I write this code? (emphasizing i’m an ST beginner so please elbaorate :slight_smile: )
after first step is conquered… is there anything in ST I can do with this picture? display it anywhere? and if not - perhaps i can just email it?
Thanks

ok, you can take the Aqara sensor as a trigger of automation but you need to define how you will communicate with the camera:

  • Check if the access to the device is through a closed cloud or an API (and the type of authentication required)
    • If it’s through a closed cloud, it means that you have to enter some credentials and it’s not possible to get authorization from another place
    • If it’s through an API, how do you get authorization, and which endpoints does it offer?
    • Or, based on your previous statement, do you only use that IP to access them and start the streaming or how is the interface?
  • See if your device is able to take pictures (for example, using a specific command) and save them in a place you can access (from the API or FTP server)

Hi!
The camera is accessible through API (no cloud). I am capable of retrieving pictures (or videos, doesn’t matter) using simple API in the sort of:
http://user:password@192.168.1.1/getPicture
so it is possible to run a script to retrieve the picture upon demand.

Thanks for the info.
The image cannot be shown in the app because the camera is not integrated there and also, the related plugin is a work in progress.
I can think of the following solution:

  1. Create a WebHook SmartApp using the SmartApp SDK
    b. This tutorial of a Simple SmartApp can help you to understand this type of integration:
  1. Define a config input for the contact sensor and the subscription should correspond to the open event. Eg.
//Entry for the config page
page.section('sensors', section => {
    section.deviceSetting('sensor').capabilities(['contactSensor']).required(true);
});

//Subscription to the "open" event
context.api.subscriptions.subscribeToDevices(context.config.sensor, 'contactSensor', 'contact.open', 'openDeviceEventHandler')
  1. As this is NodeJS, you can include some libraries to send an email from there.
    a. I think Nodemailer could be helpful (I was able to send an email with this tool)
    b. If you’ll use Gmail, you can add an app password in your account to improve security in the authorization.
    c. You would have to check how to include attachments in the email but I read it’s possible.

Thanks, I will go over these steps and tutorials you mentioned. I already went over the getting started documentation and sadly did not quite understand what a webhook is and how it works exactly.
Where does this webhook logic sit? Where is it executed?
I’m asking in order to make sure that it will be able to access an HTTP GET in the local network (192.168…)

It can be hosted in different places:

  • In the examples, Glitch is used for testing purposes. However, the free version has some restrictions regarding the execution.
  • It can also be hosted in AWS, this is an example of it
    https://github.com/SmartThingsCommunity/smartapp-example-no-devices-nodejs-lambda
  • I’ve seen that other developers use different clouds but I don’t know much about their experience.
  • You can also run it on your local PC and use a tool that creates secure tunnels such as Ngrok. The free version defines a timeout of 2 hrs for every tunnel.

Vero happens to have a very good article on what a webhook is and how it differs from an API.

That doesn’t get you any further as far as your specific use case, but at least it should help explain the concepts. :thinking:

2 Likes

Dear All,
Following all of the above correspondent it seem to me that your suggestion to use a webhook is a remote service that can receive sensors indications.
But my issue is that I need certain (very specific) logic to be executed from inside the network. I need to perform an HTTP GET request from inside my network.
In other words - I want the HUB to execute this logic. Can it ?
Did I misunderstood your suggestion or did I not explain my needs well enough?
Looking forward further insutrctions
Thanks

“Inside the network” is not the same as “executes on the SmartThings hub,” which is probably where the confusion lies.

At the present time we as customers cannot create smartapps which will run on the hub.

We can use the rules engine that is available through the SmartThings app to create automations, some of which can run on the hub, but those do not at present offer the functionality you would need for your use case.

We can, through a current beta offering, write Edge Drivers which run on the hub, but again the functionality doesn’t match what you want to do.

So you could add a new device to your network to act as a local server, like a PC or a raspberry pi, and run code on THAT device to receive the webhook and act on it, and that device could do some local communication with the hub. You would thus have code that runs “in the network” but is not executing on the SmartThings hub.

There are some people who have this kind of setup for various reasons but it does require significant technical skill to operate and it’s pretty far outside the scope of what most ST users would do.

So the short answer to your original question is that the SmartThings app doesn’t offer any method for importing images from an outside system. If you want to set up an additional device to act as a local server you could probably create a setup to get a camera image from an outside system based on a ST sensor, but you will have to write your own code and that code will run on your server device, not on the SmartThings hub. You would only interact with the ST hub to get the sensor notification, but not to process the camera image. You could keep everything on the local network, but only by adding the additional server.

(BTW, I’m not even sure the Aqara sensor would run without cloud processing at the present time, but there are other similar sensors which can.)

The original ST hub was designed primarily as the gateway to the ST cloud. Over time, they have moved more operations to the hub, but it’s still not the same as other hubs which let you run your own code.

To be honest, for what you describe, either Hubitat or Homeseer would be a better fit. You can accomplish the same result with SmartThings, but it requires the additional server device and is a more convoluted process in my opinion.

Thank you @JDRoberts for the elaborated response.
Just to clarify - if I go about writing my own local server to retrieve events from ST (webhook, right?)
Will ST be able to communicate with this device from within the network? (HUB <-> RPI)
If so, can you direct me to a minimal implementation of such a webhook (preferably in python), and an explanation of how to link ST with my newly written webhook?
Thanks again for your time and effort :slight_smile:

I’m sorry, I can’t, but hopefully others can. (I am quadriparetic, and have to do everything by voice. I used to program before I got sick, but no longer do.)

@JDRoberts you help is much appreciated!
I will continue trying to figure things out :slight_smile:

1 Like

As far as communications between the hub and the RPI, that’s what the initial link that @nayelyz posted was about. Here it is again:

You do have to sign up for a developer account (it will look like it’s only for companies, but individuals can do that also) and do some initializing stuff in the cloud in order to get your hub to accept the communications. But after that the process between the hub and your local server can run locally. But ask any questions in that other thread, the people there will be able to give you much more help than I can. :sunglasses:

1 Like

Awesome explanation and recommendation, @JDRoberts! It’s highly appreciated.

Adding JD’s comments, you have the following options:

  1. The Weebhook SmartApp where the flow would be like this:
    a. You install it in your SmartThings app which would create a subscription to the sensor (this depends on your configuration)
    b. When the event from the sensor is received, you can send the picture by email
  2. Integrate an RPI to the platform, in both options, you integrate your device but the image capture event and the processing would have to be handled by the RPI, not ST as JD said.
    a. Using Edge Drivers (Lua). There’s a sample of a LAN integration using the ESP8266 which would help you understand the communication flow.
    b. Using the Direct-Connected integration (Python). Take a look at this integration shared by another Community member:
1 Like