# Cannot publish MQTT message from analysis script

**URL:** https://community.tago.io/t/cannot-publish-mqtt-message-from-analysis-script/1612
**Category:** Analysis and Actions
**Created:** [June 20, 2024, 3:46am UTC](https://community.tago.io/t/cannot-publish-mqtt-message-from-analysis-script/1612 "2024-06-20T03:46:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![soto.carlosf](https://avatars.discourse-cdn.com/v4/letter/s/b5ac83/32.png) [@soto.carlosf](https://community.tago.io/u/soto.carlosf)
#### Post date: [June 20, 2024, 3:46am UTC](https://community.tago.io/t/cannot-publish-mqtt-message-from-analysis-script/1612/1 "2024-06-20T03:46:01Z")

</div>

Hello, I’ve been following instructions from: [https://help.tago.io/portal/en/kb/articles/12-mqtt-process-data-publish-it-and-subscribe-to-a-topic#Processing\_the\_data](https://help.tago.io/portal/en/kb/articles/12-mqtt-process-data-publish-it-and-subscribe-to-a-topic#Processing_the_data)

I want to publish a mqtt message on “GUIMEL/SENSORS/GCUBE01/SETTINGS” after I receive a message on “GUIMEL/SENSORS/GCUBE01/DATA” . Now, the analysis script is being triggered and prints “Message Sent”, but I cannot see the message being actually published neither on the Live Inspector nor in my MQTT client. The code I’m using on the script is the following:

const { Analysis, Services } = require(“@tago-io/sdk”);

const DEVICE\_ID = “8fb6cc7d-4abd-4582-8bd4-05ae0aac04fb”;

async function mqttPushExample(context, scope) {

console.log(context, scope)

if (!scope.length) {

return context.log(“Missing values”);

}

const myData = scope.find((x) =\> x.variable === “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: “payload”,

value: “30000,30000,30000,30000,30000,60000”,

unit: “C”,

};

// Create a object with the options you chooses

const options = {

retain: false,

qos: 0,

};

// Publishing to MQTT

const mqtt = new Services().mqtt;

context.log(“Will send to:”);

context.log(DEVICE\_ID);

context.log( mqtt.publish);

mqtt.publish({

```
 bucket: DEVICE\_ID,

 message: JSON.stringify(myDataObject),

 topic: "GUIMEL/SENSORS/GCUBE01/SETTINGS",

 options,

```

}).then(context.log, context.log)

}

module.exports = new Analysis(mqttPushExample);

And this is the log I’m getting after the script is triggered:

[2024-06-19 21:38:45] Duration: 1.1s Billed Duration: 2s.

[2024-06-19 21:38:45] Analysis finished.

[2024-06-19 21:38:45] Message sent

[2024-06-19 21:38:45] Will send 8fb6cc7d-4abd-4582-8bd4-05ae0aac04fb [AsyncFunction: publish]

[2024-06-19 21:38:45] { log: [Function: log], token: ‘a-ecad0d81-3516-4e05-8c78-fe3e3df0d252’, environment: [{ key: ‘’, value: ‘’ }, { key: ‘\_action\_id’, value: ‘66721df7864a2b00081d6290’ }, { key: ‘\_action\_type’, value: ‘mqtt\_topic’ }, { key: ‘\_action\_state’, value: ‘unlocked’ }, { key: ‘\_device\_id’, value: ‘6669ed3894c0e80009a715c5’ }], analysis\_id: ‘66722187ed404a0009e7d9d5’ } [{ variable: ‘payload’, value: ‘GCUBE01 189.00 159.00 20.00 40.00 141.00 107.00 false 90.00’, metadata: { mqtt\_topic: ‘GUIMEL/SENSORS/GCUBE01/DATA’ } }]

[2024-06-19 21:38:44] Starting Analysis…

---

<div class="post-metadata">

### Author: ![soto.carlosf](https://avatars.discourse-cdn.com/v4/letter/s/b5ac83/32.png) [@soto.carlosf](https://community.tago.io/u/soto.carlosf)
#### Post date: [June 20, 2024, 5:03pm UTC](https://community.tago.io/t/cannot-publish-mqtt-message-from-analysis-script/1612/2 "2024-06-20T17:03:17Z")

</div>

I have solved this. I was using the device token instead of the device ID.
