Hi all,
I am trying to test a device with a number type capability, it is a Power Meter capability uner the reference material. I am using the node js SDK to create a smart app and basically want to turn on a switch based upon the current reading. I started off with a simpler example using two smart plugs when one turned off it turned off the other and the same for turning on, the code snippet is below.
// Define the updated functionality
// This handler is called when the app is updated by the user
// Will also be used if no "Installed" functinality is decalred when the user first installs the app
app.updated(async (context, updateData) => {
// First unsubscribe from all events as settings have changed
await context.api.subscriptions.unsubscribeAll();
// Promise expects an array of jobs, it will return once all are evlauated
return Promise.all([
// subscribeToDevices Inputs:
// - The name given to the device in Config
// - The capabality that is of interest
// - The state of the capability
// - The callback for when this event occurs
context.api.subscriptions.subscribeToDevices(context.config.sensor,'switch','switch.on','onDeviceEventHandler'),
context.api.subscriptions.subscribeToDevices(context.config.sensor,'switch','switch.off','offDeviceEventHandler')
])
})
// Define the event handlers for subscriptions
app.subscribedEventHandler('onDeviceEventHandler', (context, deviceEvent) => {
return context.api.devices.sendCommands(context.config.actuator, 'switch', 'on');
})
app.subscribedEventHandler('offDeviceEventHandler', (context, deviceEvent) => {
return context.api.devices.sendCommands(context.config.actuator, 'switch', 'off');
})
Now I want to change it so my actuator
device is controlled just by the measurement of the Power Meter. For the subscrbedEventHandler
I am unsure what event to subscribe to as with the switch I knew if it was either switch.on
or switch.off
. Maybe some way of reading the attribute of the Power Meter and when it changes enter the callback but cant find how to test attributes in the docs
The code snippet below might give you more of an idea of what I am trying for (it does not work but gives idea).
app.updated(async (context, updateData) => {
await context.api.subscriptions.unsubscribeAll();
return Promise.all([//Unsure here but subsribe so when power meter changes enter callback
context.api.subscriptions.subscribeToDevices(context.config.sensor,'powerMeter','powerEventHandler')
])
})
// Define the event handlers for subscriptions
// Called when Power Meter Changes
app.subscribedEventHandler('powerEventHandler', (context, deviceEvent) => {
if (context.config.sensor.capabilities(['powerMeter']) > 1) { //TEST THE ATTRIBUTE
return context.api.devices.sendCommands(context.config.actuator, 'switch', 'on'); //TURN ACTUATOR ON
}
else {
return context.api.devices.sendCommands(context.config.actuator, 'switch', 'off'); // TURN ACTUATOR OFF
}
})