It’s hard to understand why you need to use the widget’s title in the analysis, it’s a fixed value.
But I suggest you configure a text field, insert text on it, and mark that option “default value” as “last” so you keep the default configure value every time you submit data.
I have two widgets on my dashboard; switch (Input control), both are used to send MQTT commands to the same device, 1st for ON/OFF Pump#1, 2nd for ON/OFF Pump#2.
If I use “Push to MQTT from dashboard” from the example, I need to determine which one was switched.
From the example, in the object I get the topic (predefined), the variable (predefined) and the value of the switch state (true/false). From that information I can’t recognize which switch is changing state.
My idea was to get the device name or something else for additional information that can be used to identify the switch.
I can solve this by doing a separate analysis for each “Input Control” widget, but that doesn’t make sense…
I hope that is clear now.
Additionally, I’ve found that if two or more switches with different Title are configured the same, they clone each other’s behavior, pressing 1st results in the 2nd switch on the board turning on, etc.
you can use the profile token to handle many device with one analysis. Just try the following code:
if (!scope[0]) return context.log("This analysis must be triggered by a dashboard.");context.log('Analysis started');// Get the environment variables.const environment_variables = Utils.envToJson(context.environment);if (!environment_variables.account_token) return context.log('Missing "account_token" environment variable');else if (environment_variables.account_token.length !== 36) return context.log('Invalid "account_token" in the environment variable');// Instance the Account classconst account = new Account({ token: environment_variables.account_token });const myData = scope.find((x) => x.variable === "push_payload") || scope[0];if (!myData) {return context.log("Couldn't find any variable in the scope.");}const device = await Utils.getDevice(account, myData.device);// todo what you want...
I have analysis from example with litle modification (some changes for const myDataObject):
/* ** Analysis Example ** Get Device List ** * Snippet to push data to MQTT. Follow this pattern within your application * If you want more details about MQTT, search "MQTT" in TagoIO help center. * You can find plenty of documentation about this topic. * TagoIO Team. ** ** How to use? ** In order to trigger this analysis you must setup a Dashboard. ** Create a Widget "Form" and enter the variable 'push_payload' for the device you want to push with the MQTT. ** In User Control, select this Analysis in the Analysis Option. ** Save and use the form. */const { Analysis, Services } = require("@tago-io/sdk");async function mqttPushExample(context, scope) { if (!scope.length) { return context.log("This analysis must be triggered by a dashboard."); } const myData = scope.find((x) => x.variable === "push_payload") || scope[0]; if (!myData) { return context.log("Couldn't find any variable in the scope."); } // Create your data object to push to MQTT // In this case we're sending a JSON object. // You can send anything you want. // Example: // const myDataObject = 'This is a string'; const myDataObject = { variable: "switch_state", value: myData, }; // Create a object with the options you chooses const options = { retain: false, qos: 0, }; // Publishing to MQTT const MQTT = new Services({ token: context.token }).MQTT; MQTT.publish({ // bucket: myData.bucket, // for legacy devices bucket: myData.device, // for immutable/mutable devices message: JSON.stringify(myDataObject), topic: "tago/my_topic", options, }).then(context.log, context.log)}module.exports = new Analysis(mqttPushExample);// To run analysis on your machine (external)// module.exports = new Analysis(mqttPushExample, { token: "YOUR-TOKEN" });
I send MQTT to Node-RED. This is picture from debug:
You receive the widget_id in the context of your analysis. See the example:
const { Analysis, Utils } = require("@tago-io/sdk");// The function myAnalysis will run when you execute your analysisasync function myAnalysis(context, scope) {const env = Utils.envToJson(context.environment);console.log("widget id:", env._widget_id);}module.exports = new Analysis(myAnalysis, "your-analysis-token");
Print the “env” to see all environment data that you receive when submitting the input.
I implemented that in your code, I hope it can help you.
/*** Analysis Example** Get Device List*** Snippet to push data to MQTT. Follow this pattern within your application* If you want more details about MQTT, search "MQTT" in TagoIO help center.* You can find plenty of documentation about this topic.* TagoIO Team.**** How to use?** In order to trigger this analysis you must setup a Dashboard.** Create a Widget "Form" and enter the variable 'push_payload' for the device you want to push with the MQTT.** In User Control, select this Analysis in the Analysis Option.** Save and use the form.*/const { Analysis, Services, Utils } = require("@tago-io/sdk");async function mqttPushExample(context, scope) {if (!scope.length) {return context.log("This analysis must be triggered by a dashboard.");}const env = Utils.envToJson(context.environment);console.log("widget id:", env._widget_id); // todo widget_idconst myData = scope.find((x) => x.variable === "push_payload") || scope[0];if (!myData) {return context.log("Couldn't find any variable in the scope.");}// Create your data object to push to MQTT// In this case we're sending a JSON object.// You can send anything you want.// Example:// const myDataObject = 'This is a string';const myDataObject = {variable: "switch_state",value: myData,};// Create a object with the options you choosesconst options = {retain: false,qos: 0,};// Publishing to MQTTconst MQTT = new Services({ token: context.token }).MQTT;MQTT.publish({// bucket: myData.bucket, // for legacy devicesbucket: myData.device, // for immutable/mutable devicesmessage: JSON.stringify(myDataObject),topic: "tago/my_topic",options,}).then(context.log, context.log)}module.exports = new Analysis(mqttPushExample);// To run analysis on your machine (external)// module.exports = new Analysis(mqttPushExample, { token: "YOUR-TOKEN" });