Publishing to MQTT topic from python analysis script

Hello everyone,
I am trying to publish data entered in the dasboard to a device through a MQTT topic. My attemp was to create an analysis script (in python) which is triggered when the send button is clicked and from there the information can be published to the topic. I have found this tutorial (https://docs.tago.io/en/articles/12) which shows how to do this in javascript but I haven’t been able to implement it in python.
For now I’m just figuring out the basics but my intention is to have several inputs in the dashboard which the user can interact with and send data to the devices, by running all the logic in a python analysis script in Tago.
Is this the right approach? How can this be implemented in python?

Thank you for your help!!

Best regards,

Andrés

[UPDATE]
Hello again!
As I said I found a js script which works fine, however, I still can not make it to work with a python script. I have also found an example on this on python (https://github.com/tago-io/analysis-example-mqttpush-py/blob/master/analysis.py) (very similar of what I was trying) but still not success. I am chaging the value of a variable through a form in the dashboard and whenever this variable changes I have an action which runs the analysis script. As I said it works perfectly with the js script, dunno what I am doing wrong with python :frowning_face:
Thanks for your help!!

Andrés

Hi,

Test again, we fixed the problem on SDK!
Remember update the SDK in python.

Hope this helps, Rafael Teles.

Hello,
Thanks for the answer. I don’t know how to update the SDK since it is on tago side. I have tried the following code without success:

from tago import Analysis
from tago import Services

def myAnalysis(context,scope):
  my_data_bucket = scope[0]['bucket']
  my_data_value = scope[0]['value']
  my_data_json = {'variable': 'gear','value': my_data_value}
  #json_msg = json.dumps(my_data_json)
  topic = 'gear'
  retain = False
  qos = 0
  # Publishing to MQTT
  MQTT = Services(context.token).MQTT
  result = MQTT.publish(my_data_json,my_data_bucket,topic,retain,qos)
  context.log(result)

Best regards,

Andrés

To test your code you need to follow these steps:

How to use it?

1 - To trigger this analysis you must set up a Dashboard.
2 - Create a Widget “Form” and enter the variable ‘push_payload’ for the device you want to push with the MQTT.
3 - In button submit, select Analysis in the “Select an analysis”
4 - Save and use the form.

Because if not use this steps, this lines: my_data_bucket = scope[0][‘bucket’] and
my_data_value = scope[0][‘value’] not receive the data.

you can test the code insert the data without using the dashboard, like this:

from tago import Analysis
from tago import Services

def myAnalysis(context,scope):
  # my_data_bucket = scope[0]['bucket']
  # my_data_value = scope[0]['value']
  # my_data_json = {'variable': 'gear','value': my_data_value}
  # json_msg = json.dumps(my_data_json)
  topic = 'gear'
  retain = False
  qos = 0
  # Publishing to MQTT
  MQTT = Services(context.token).MQTT
  result = MQTT.publish("your data for test","your bucket id",topic,retain,qos)
  context.log(result)
Analysis.init(myAnalysis)

I saw in your code that it missed the last line: Analysis.init(myAnalysis)
without this line, your code not working.

Remember to create analysis in python you need to change the runtime like this:

1 Like

Hello Rafael,
thank you for your answer. However I still can’t make it work, actually, when trying to add the Analysis.init(myAnalisys) (trying the code you suggested for sending only data without using the dashboard) not only it doesn’t work but also I get this error in the analysis console: image
Note that I am trying to execute the analysis inside Tago not externally.
Also I have repeated the process exactly how you explained starting from setting up a new dasboard, creating a widget form, etc…

Hi,

Test instead of putting : Analysis.init(myAnalisys) put this : Analysis().init(myAnalisys) .

Regards, Rafael Teles.

Hi, now it works, I add the code just in case it is useful for anyone. Thank you!!

from tago import Analysis
from tago import Services
import json


def myAnalysis(context,scope):
  my_data_bucket = scope[0]['bucket']
  my_data_value = scope[0]['value']
  my_data_json = {'variable': 'gear','value': my_data_value}
  json_msg = json.dumps(my_data_json)
  topic = 'gear'
  retain = False
  qos = 0
  # Publishing to MQTT
  MQTT = Services(context.token).MQTT
  result = MQTT.publish(json_msg,my_data_bucket,topic,retain,qos)
  context.log(result)
Analysis().init(myAnalysis)

2 Likes