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?
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.
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:
Select a motion sensor (you can also change it to be able to select more than one)
Create the subscriptions for the “active” and “inactive” states of the motion sensor
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);