Hello @jamesbc619,
Perhaps you could try using the SmartApp SDK, below is a similar example where the configuration helps you to:
- Select a device to receive its events and send commands to it.
- Set the time expiration (in seconds).
- See the remaining time (in seconds).
- When an event is received:
a. ON: Start the timer.
b. OFF: Pause the timer. - When the time expires, turn off the device.
const express = require('express');
const SmartApp = require('@smartthings/smartapp');
const bodyParser = require('body-parser');
const Timer = require('easytimer.js').Timer;
const server = express();
server.use(bodyParser.json());
const app = new SmartApp();
let timeLeft=0;
let timer = null;
/* Defines the SmartApp */
app.configureI18n()
.page('mainPage', (context, page, configData) => {
page.section('switchDev', section => {
section.deviceSetting('switchD').capabilities(['switch']).required(true).permissions('rx');
});
page.section('preferences', (section) => {
section.numberSetting('aftertime').defaultValue("0");
});
page.section('remaining', (section) => {
section.paragraphSetting('remainingT').text(`${timeLeft}`).description("seconds");
});
})
.updated(async (context, updateData) => {
await context.api.subscriptions.unsubscribeAll();
return Promise.all([
context.api.subscriptions.subscribeToDevices(context.config.switchD, 'switch', 'switch.on', 'switchOnEventHandler'),
context.api.subscriptions.subscribeToDevices(context.config.switchD, 'switch', 'switch.off', 'switchOffEventHandler')
]);
})
.subscribedEventHandler('switchOnEventHandler', (context, deviceEvent) => {
timeLeft=context.configNumberValue('aftertime');
timer = new Timer();
timer.start({precision: 'seconds'});
timer.addEventListener('secondsUpdated', function (e) {
timeLeft--;
if(timeLeft==0){
return context.api.devices.sendCommands(context.config.switchD, "switch", "off")
}
});
})
.subscribedEventHandler('switchOffEventHandler', (context, deviceEvent) => {
timer.pause();
});
server.post('/', async (req, res) => {
app.handleHttpCallback(req, res);
});
/* Starts the server */
let port = 3000;
server.listen(port, () =>
console.log(`Server is up and running on port ${port}`)
);
For more information see the sample of a Simple SmartApp and the settings type of the SmartApp SDK .
Let me know if you have any doubts.