Analysis to send MQTT message

Analysis to send MQTT message

@Adams Zeidler

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

}