Multiple Devices Trigger Unlock through Analysis

Hi, I have this problem where my analysis script will send 1 SMS & EMAIL notification to user, on which device battery level is less than 3.5V, every 2 hours (uplink period). Is there a way through analysis that I can script, so that once the ACTION detect that this certain device battery level is less than 3.5V, it just sends 1 SMS & EMAIL notification, and does not repeatedly send the notification on every uplink?
Screenshot (156)
I’m aware that there is trigger unlock for single devices but not for multiple devices, so I’m wondering how, perhaps in ANALYSIS, I can just trigger this action ONCE for that specific device when battery level was detected to be lower than 3.5V? Then ultimately, user won’t receive multiple notifications for the same device?
I’d appreciate any replies, thank you in advance.

In the action itself you can’t perform this operation for multiple devices.

What we usually do is to move the “unlock” logic to inside the widget. You can do that using Configuration Parameters. But you will need to run an analysis for every battery that arrives, instead of setting a condition.

async function startAnalysis(scope, context) {
  const device_id = scope[0].origin;
  const account = new Account({ token: "acc-token" });
  
  // get battery from the scope that just triggered the analysis.
  const battery = scope.find((data) => data.variable === "battery");
  
  // get the configuration parameter list from the device.
  const parameters = await account.devices.paramList(device_id);
  
  // find the battery_alert configuration parameter.
  let battery_alert = parameters.find((param) => param.key === "battery_alert");
  if (!battery_alert) {
    battery_alert = { key: "battery_alert", value: "not used", sent: false }
  }
  
  if (battery.value < 3.5 && battery_alert.sent === true) {
    // alert was already sent, even that battery is lower than 3.5
    return;
  } else if (battery.value > 3.5) {
    if (battery_alert.sent === true) {
      // reset the battery status
      account.devices.paramSet(device_id, { ...battery_alert, sent: false});
    }
    return;
  }
  
  // send your email alert, notification etc.
  
  // set the parameter sent status to true
  account.devices.paramSet(device_id, { ...battery_alert, sent: true });
}

1 Like