How to create an action when a variable changes?

I am trying to get some kind of notification when the value a variable changes. Is this kind of operation somehow possible in tago.io?

Variable is growing periodically so I can not just set it to go off when it is greater than something (unless I can somehow store a value and compare it to received on).

As my device relaying IoT messages is a Raspberry pi a quick solution was send a Telegram message as variable changed (and forget tago in this case). I could have set the device to change value to a spesific one before sending it to tago and then look for that one in the actions.

Hi @jikonen,
Yes this is possible.

You need to create an action to trigger by variable, and set it to trigger for anything. Also set it to run an Analysis.

In your analysis you can store the value in your device configuration parameters, and check if the value has changed.

Your code will look like this:

/*

 ** Environment Variables

 ** In order to use this analysis, you must setup the Environment Variable table.

 **

 ** account_token: Your account token

 **

 ** Steps to generate an account_token:

 ** 1 - Enter the following link: https://admin.tago.io/account/

 ** 2 - Select your Profile.

 ** 3 - Enter Tokens tab.

 ** 4 - Generate a new Token with Expires Never.

 ** 5 - Press the Copy Button and place at the Environment Variables tab of this analysis.

 */

const { Analysis, Account, Utils } = require("@tago-io/sdk");

async function startAnalysis(context, scope) {

  // Transform all Environment Variable to JSON.

  const envVars = Utils.envToJson(context.environment);

  if (!envVars.account_token) {

    return context.log("Missing account_token environment variable");

  }

  const account = new Account({ token: envVars.account_token });

  const myVariable = scope.find((data) => data.variable === "myVariableName");

  

  const deviceParams = await account.devices.paramList(myVariable.origin);

  const param = deviceParams.find((param) => param.key === "last_value") || { key: "last_value", value: null };

  if (param.value !== String(myVariable.value)) {

    // do something

    account.devices.paramSet(myVariable.origin, { ...param, value: String(myVariable.value) }).then(context.log, context.log);

  } else {

    // do nothing

  }

}

module.exports = new Analysis(startAnalysis);