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);