Publish to MQTT to multiple devices

Hi, I have a blueprint dashboard with multiple devices (one for each user), and through an Action I want to trigger that when a Variable changes, an MQTT is published to the device whose variable changed. Before using blueprint, I was creating one action for each device and it worked fine. Now I´d like to know how to make this with only one action, where the device whose variable triggered the Action is the device getting the MQTT. Or at least what I need is that all devices can subscribe to that MQTT topic and inside the payload I can define which one should receive the message. The problem I´m facing is that when I configure the Action in Tago, I am requested to put a Bucket, and since the device that triggered the action is not known by me in advance, I don´t know which Bucket to choose. Please I need your help with this as soon as possible, as this is blocking a core part of my solution. Thank you!

Hi @ecamarotti,

Just to share the solution here.

In this case, you need to call an analysis script and there make the script that will publish to the topic. So will need to change your action to run an analysis and build your script.

Here is an analysis example of how to send a message to a topic via analysis:

const Services = require('tago/services');
function myanalysis(context, scope) {​
  if (!scope.length) return context.log('no data');
  const myData = scope[0];
  const dataInC = {​
      variable: 'temperature_celsius',
      value: (myData.value - 32) * ( 5 / 9 ),
      unit: 'C',
  }​;
  const options = {​
      retain: false,
      qos: 0,
  }​;
  dataInC.value = dataInC.value.toPrecision(3);
  const MQTT = new Services(context.token).MQTT;
  MQTT.publish('tago/my_topic', JSON.stringify(dataInC), myData.bucket, options).then(context.log).catch(context.log);
}​
module.exports = new Analysis(myanalysis, 'YOUR ANALYSIS TOKEN HERE');