Analysis to send MQTT message

Hello
I´m working on an analysis to send a MQTT message to my device under topic “button”; I Have a variable called “push_payload” that is updated when a form is updated.
I have already tested an action triggered by the variable update and it works fine.
However, when I try to trigger the analisys to do the same (send MQTT message) it does not work. I may be missing something on Script. The only indication I have is the analisys log that shows the errors :
[2020-03-22 17:27:17] ReferenceError: listDevicesByTag is not defined

[2020-03-22 17:27:17] Starting analysis 5e77c8af1f8834001b659d6c

The analisys script is below ( I used the example on platform) ;

async function mqttPushExample(context, scope) {
console.log(‘my context:’, context);
console.log(‘my scope:’, 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(‘Couldnt 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: 'push_payload',

value: 'teste'

};

// Create a object with the options you choosed

const options = {

retain: false,

qos: 0,

};

// Publishing to MQTT

const MQTT = new Services(context.token).MQTT;

await MQTT.publish(‘button’, JSON.stringify(myDataObject), myData.bucket, options).catch(context.log);

}

Hi @adams.zeidler,

I just tested your function here and it works fine. It’s a syntax error. I’m going to let my code here to you compare.

const Analysis = require(‘tago/analysis’);

const Services = require(‘tago/services’);

async function mqttPushExample(context, scope) {

console.log(‘my context:’, context);

console.log(‘my scope:’, 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(‘Couldnt 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: ‘push_payload’,

value: ‘teste’

};

// Create a object with the options you choosed

const options = {

retain: false,

qos: 0,

};

// Publishing to MQTT

const MQTT = new Services(context.token).MQTT;

await MQTT.publish(‘button’, JSON.stringify(myDataObject), myData.bucket, options).then(context.log).catch(context.log);

}

module.exports = new Analysis(mqttPushExample, ‘YOUR ANALYSIS TOKEN HERE’);

Thanks for the help, but I tested with your script again and still not able to send the MQTT message to my device.
I believe it´s missing the link for the script and the device . Still have the message below. Very strange as I don´t see log other log messages. In fact , I receive a message that the Analysys run correctly . But no MQTT message received or message on log,

[2020-03-25 13:26:41] TypeError: Cannot read property ‘run’ of undefined
[2020-03-25 13:26:41] Starting analysis 5e77c8af1f8834001b659d6c

Hi @adams.zeidler It’s a sintaxe error again. Can you send me the full code of your script?

Hello Fabio , Here it goes.

In Summary : The variable that triggers the analisys is “push_payload”. I want to send the value of this variable to device via MQTT
I undestand I need to extract the variable “push_upload” from scope and save it on the myData.
Next we create myDataObject (JSON payload to be sent)
Final step is publish on “button” topic .

const TagoUtils = require(‘tago/utils’);
const TagoAccount = require(‘tago/account’);
const Analysis = require(‘tago/analysis’);
const Services = require(‘tago/services’);

async function mqttPushExample(context, scope) {

console.log(‘my context:’, context);
console.log(‘my scope:’, 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(‘Couldnt find any variable in the scope.’);

const myDataObject = {
variable: ‘push_payload’,
value: myData.value ,
};

// Create a object with the options you choosed

const options = {
retain: false,
qos: 0,
};

// Publishing to MQTT
const MQTT = new Services(context.token).MQTT;
await MQTT.publish(‘button’, JSON.stringify(myDataObject), myData.bucket, options).catch(context.log);

}

module.exports = new Analysis(listDevicesByTag, ‘ANALYSIS TOKEN HERE’);

Hi @adams.zeidler,

You need to change your module.exports function. The listDevicesByTag doesn’t exist. You need to export the function mqttPushExample.

Just update the last line of code to: module.exports = new Analysis(mqttPushExample, ‘ANALYSIS TOKEN HERE’);

Thanks. I see progreess;
I have made the change but now I have the below error message :

[2020-03-27 18:45:12] Invalid bucket
[2020-03-27 18:45:11] Starting analysis 5e77c8af1f8834001b659d6c

I have also changed Cosolo.Log by Context.Log and I have other messages now; it seems that the scope is not being passed , despite I have manually saved as

What is “token” on contaxt ? should it be the device token?

[
{
“variable”: “push_payload”,
“value”: “mensagem teste”
}
]

[2020-03-27 19:14:14] Invalid bucket
[2020-03-27 19:14:14] my context: {“token”:“0667ec72-763d-4a6e-b5b9-157fe4560a8c”,“environment”:[],“analysis_id”:“5e77c8af1f8834001b659d6c”}
[2020-03-27 19:14:14] my scope: [object Object]
[2020-03-27 19:14:14] Starting analysis 5e77c8af1f8834001b659d6c

IT WORKS !!!
For some reason its working after I edit the action that triggers the analysis and select it again.

Thanks!!