Input Control - how to get Title value as variable?

Hi,

I am trying to modify the “Push to MQTT from Dashboard” example.

I want to use, for example, the value of Title field of the Input Control widget and use it as the variable in the object that I send to mqtt.

I need to add and modify, but for Title value:

const myData = scope.find((x) => x.variable === “push_payload”) || scope[0];

What do I need to change to retrieve the Title value? Thx!

Hi Marko,

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.

Hello, thanks for your reply!

I will try to explain a little more precisely.

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.

Hi Marko,

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...

Hi, Im not sure that I understand correctly relation between profile token and identification which of two(of more) dashboard button are pressed?!

Hi Marko,

can you show us more about your code?

it’s hard to understand what you want to handle in the code.

Ok.

I have dashboard with two switch (Input control) widget.

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:

Red is from switch1, green is from switch2.

Question is: how to know which swich send what?

Hi Marko,

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.

Thanks for the support, I hope it will be useful to someone!

I give up, I’m not good enough to understand these instructions and successfully implement them as a tagoio beginner.

Hi Marko,

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

THX!

What exactly is this supposed to do in the background?

const myData = scope.find((x) => x.variable === "push_payload") || scope[0];

I have the same behavior even if it is:

const myData = scope.find((x) => x.variable) || scope[0];

Hi Marko,

This gets the variable submitted in the form, in this case, “form_payload”, if the scope doesn’t have this variable will get the position 0.

If you are not submitting the variable with this name, change “form_payload” to your variable name.

const myData = scope.find((x) => x.variable === "your_input_variable_name") || scope[0];

The lines below don’t work, the .find function is not finding anything, and you’ll always get array position 0.

const myData = scope.find((x) => x.variable) || scope[0]