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

Hello @alinetusi,

I am developing a blueprint dashboard with multiple MQTT devices. In this application, I have a unknown number of sensor nodes (and unknown tokens), each one named with your own MAC address in device and bucket.
To publish and subscribe, I use MQTT topics with this structure: “<mac_address>/data” and “<mac_address>/cfg”. I did it sucessfully with normal dashboard and one sensor node (with generated token).
I still do not figure out how to send MQTT data to my related devices when token is not known in advance, and how to publish info from Tago choosing a blueprint device in device’s list.
There is a limit of 20 blueprint devices in a single dashboard, so what can be done to provide a list of devices to be choosen and watched in dashboard? Is it possible to share some hints, tutorials and examples in order to help me build this application?
Thank you!

You would need to have an Analysis to split up the data to the correct devices.

In a few lines,

  • You need to create a Network inside Network (tago.io), and enable the Serial option,
  • Then create a connector Connector (tago.io) to be able to create devices within your network.
  • Inside your network, you will see a Token tab, where you will generate a Token for your network.
  • Then you can use this analysis template and replace the tokens in the Environment Variable tab.
  • Create an action of MQTT type, then set it to run the analysis for your topics.
  • Now you need to update the Analysis code to get the device EUI and run the lines 51 to 59 to redirect the data to the correct device.

Notice that this will use 1 Analysis run for each data you uplink.

2 Likes

Hello @vitor!
Thanks for your help! I followed your instructions and could make it work as expected.
In line 37 of analysis template, the content received by MQTT is only recognized in scope[0] when each variable has quotation marks [{ “devaddr”: “122444”, “data”: “B0A10012BA”, “rssi”: -12 }]

2 Likes