Invoke REST API when Motion detected

Hi all.

Apologies, I am new to ST and am still learning.
I have searched online and the forum but I can’t find steps for the usecase “Invoke (GET) REST API when Motion detected”.
Can anyone help? I have the samsung motion sensor.

From what I have read, I need to create a SmartApp, and there is sample code, but how do I get the motion to trigger this?

https://docs.smartthings.com/en/latest/smartapp-developers-guide/calling-web-services-in-smartapps.html

Depending on what it is you are trying to do it may be easier to just use webcore.
There is also IFTTT, present the motion device. To IFTTT then IF motion THEN trigger api via maker channel.

With the imminent retiring of Groovy, it may also be wise to hear something official about the future of webCoRE.

Okay, I figured it out so posting here incase it helps anyone else:

/**

  • Tablet On Motion
  • Copyright 2020 …
  • Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except
  • in compliance with the License. You may obtain a copy of the License at:
  •  http://www.apache.org/licenses/LICENSE-2.0
    
  • Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
  • on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
  • for the specific language governing permissions and limitations under the License.

*/
definition(
name: “…”,
namespace: “…”,
author: “…”,
description: “…”,
category: “My Apps”,
iconUrl: “…”,
iconX2Url: “…”,
iconX3Url: “…”)

preferences {
section(“Turn on when motion detected:”){
input “motion1”, “capability.motionSensor”, title: “Which sensor?”
}
}

def installed() {
subscribe(motion1, “motion.active”, motionHandler)
}

def updated() {
unsubscribe()
subscribe(motion1, “motion.active”, motionHandler)
}

def motionHandler(evt) {
httpGet(uri: “https://…”)
}

I then went to SmartApps and used my custom smartapp. The UI lets me choose the sensor.

Hello @DJX,

Glad to hear that, excellent job!

I would recommend to start looking forward and create a SmartApp using the SDKs available on SmartThings Github. The sample below is based on the smartapp-sdk-nodejs and the configuration helps you to:

  1. Select a motion sensor (you can also change it to be able to select more than one)
  2. Create the subscriptions for the “active” and “inactive” states of the motion sensor
  3. Make a request to an external REST API, on the “active” subscription handler you can find an example (in this case, using Axios)
const express = require('express');
const bodyParser = require('body-parser');
const SmartApp = require('@smartthings/smartapp');
const axios = require('axios');
const server = express();
require('dotenv').config();

server.use(bodyParser.json());

const app = new SmartApp()

/* 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(['motionSensor']).required(true);
       });
   })
   .updated(async (context, updateData) => {
       await context.api.subscriptions.unsubscribeAll();
       return Promise.all([
           context.api.subscriptions.subscribeToDevices(context.config.sensor, 'motionSensor', 'motion.active', 'motionActiveEventHandler'),
           context.api.subscriptions.subscribeToDevices(context.config.sensor, 'motionSensor', 'motion.inactive', 'motionInactiveEventHandler')
       ])
   })
   .subscribedEventHandler('motionActiveEventHandler', async (context, deviceEvent) => {
       let httpget=await axios.get('http://...');
   })
   .subscribedEventHandler('motionInactiveEventHandler', (context, deviceEvent) => {
       //Add some actions
   });    

/* Starts the server */
let port = process.env.PORT;
server.listen(port);

For more information, visit: