Subtracting the values of the same variable

Hi all,
I need to calculate the daily and weekly usage of water, I have a variable that counts number of the total pulses from a flow meter but in order to reach daily usage, I need to subtract the values that the meter returns today from the value of the previous day at the same time, and a similar scenario for weekly usage, how can I do that?

1 Like

Hi @ali,

Basically what you need to do here is to fetch all the variables from the device and filter only the variables related to 1 day ago.

I dont know how you’re planning to do it, I will assume that you’re going to setup a action schedule to trigger by each day/week a script.

Here I’ve prepared a SAP script (its going to work but you still need to adapt in your system or maybe even do minor changes) for your need and you can make use of it.

I’m attaching the code below, hope that it find your needings.

   const {
} = require("@tago-io/sdk");

const moment = require("moment-timezone");

async function handler(context, scope) {
  context.log("Running Analysis");

  //uncomment those lines for an automatically device initiation approach, you will need to setup config_token and account_token on your analysis enviroment variables
  //your analysis must be triggered by a widget/dashboard

  // const environment = Utils.envToJson(context.environment);
  // if (!environment) {
  //   return;
  // } else if (!environment.config_token) {
  //   throw "Missing config_token environment var";
  // } else if (!environment.account_token) {
  //   throw "Missing account_token environment var";
  // }

  // const account = new Account({ token: environment.account_token });
  // const config_dev = new Device({ token: environment.config_token });

  // const device_id = scope[0].serie;
  // const customer_token = await Utils.getTokenByName(account, device_id);
  // const sensor_dev = new Device({ token: customer_token });

  //on this example we are putting the device token hardcodded, but it is possible to do it automatically, I will leave it commented above.
  //change the token for your device here
  const device = await new Device({
    token: "a9049d16-f528-4a39-9559-1232******",

  //choose the right variable here, in the example it was used "temperature"
  const data = await device.getData({ variables: "temperature", qty: "9999" });

  //change the number here 1 for 1 day / 7 for a week for example
  const wanted_data_list = data.filter(
    (x) => moment().diff(moment(x.time), "days") <= 1

  const wanted_data_list_lenght = wanted_data_list.length;

  let result;
  if (wanted_data_list_lenght) {
    result =
      wanted_data_list[0].value -
      wanted_data_list[wanted_data_list_lenght - 1].value;

  // const sum = 0;
  // wanted_data_list.forEach(x => sum = x.value + sum);
  //const daily_mean = sum/wanted_data_list_lenght


async function startAnalysis(context, scope) {
  try {
    await handler(context, scope);
  } catch (error) {
    context.log(error.message || JSON.stringify(error));

module.exports = new Analysis(startAnalysis);

 //to run locally uncomment those lines
//module.exports = new Analysis(startAnalysis, {
//  token: "7d17d523-3ed3-4813-84b0-a5c6d410***",

Always a pleasure Ali,

Guilherme Oliveira

1 Like

That’s a great help for something this end too! Thanks for posting