Pushing data from Ignition

Hello,
I'm trying to push data of ignition. I found a tutorial online but the code doesn't work for me. Does anyone see any glaring issues? This is in my Gateway scripting events and in the Gateway library I have the path to this script in a tag change.

import json

def publishOnMqtt(initialChange, executionCount, newValue, previousValue, event):
    # log change event with the new value and the tag path
    logger = system.util.getLogger("MqttLogger")
    tagPath = event.getTagPath().toString()
    topic = tagPath.split("/")[-1]  # extract tag path
    value = newValue.getValue()
    logger.trace("TAGCHANGE: {} changed to {}".format(topic, value))

    payload = {
        "value": value,
        "timestamp": system.date.format(newValue.getTimestamp(), "dd-MM-yyyy"),  # correct format
        "quality": str(newValue.getQuality())
    }

    qos = 1  # quality of service
    retain = False
    payloadStr = json.dumps(payload)

    # publish the message to broker
    system.cirruslink.engine.publish("Chariot SCADA", topic, payload, qos, retain)

Any help appreciated!
Thanks

What doesn't work?

The script up above, it's supposed to push my tag data out of ignition but it doesn't work.

What does "it doesn't work" mean? What calls this script? Have you looked at the logs?

Help us help you.

My bad, I'll explain in more detail. Though I've saved the project, it's as if the script doesn't exist. There is nothing connected to it in the logs and and when I check ignition it says there is no script currently running. I've checked the application I'm trying to send the data to and it's empty. I figured there must be something wrong within the script.
I copied it’s path to put in a script in a tag change and made the tag a folder in my tag browser. The tags in question are changing every second. This is what is calling the script. Thanks for the reply.

Do you have the project you're using set as the gateway scripting project in the configuration of your gateway? If not, depending on where you're calling this from, it could be that it can't get to the script because of this.

Are logs showing anything?

You're logging at the trace level, make sure the logger is set up to record logs of that level:
Go to the logs page on the gateway interface, and click the settings wheel:
image

Find your logger and make sure the level is set to "trace"
image

Otherwise you won't ever see what you're logging.

Alternatively, change the logger.trace call in your script to a higher level that will show in the logs.

Next, I suggest commenting out the whole script and just logging something to make sure that the script is actually called when you think it is.
Then we can start tracking issues.

2 Likes

For easy debugging: Use try/except and log the error using str(sys.exc_info())
And/or call your function from the script console and sprinkle some print('at LOC 12'), ... lines all over it.

Also: just use jsonEncode

And one more thing: Use a gateway tag change script.

If you don't wrap your code in try/except clauses, the errors will bubble up to the top level and show up in the logs anyway. Unless something else catches it before it reaches the surface, of course.
For debugging, I'd recommend not wrapping the code and letting the "natural" process do its job, at least for the first round.

2 Likes

From the looks of it, you are passing the publish method a dictionary for the payload.

If I'm not mistaken, the method is expecting a byte[] for the payload, not a dictionary.

system.cirruslink.engine.publish(String serverName, String mqttTopic, byte [] payload, int qos, boolean retain)

MQTT Publishing via MQTT Engine - MQTT Modules for Ignition 8.x - Confluence (chariot.io)

You are converting your dictionary to a JSON string (payloadStr), but not actually using it. Try this instead:

system.cirruslink.engine.publish("Chariot SCADA", topic, payloadStr.encode(), qos, retain)

You can also take a look at this post:

Publish to topics using system.cirruslink.engine.publish() - Ignition - Inductive Automation Forum