# Pushing data from Ignition

**URL:** <https://forum.inductiveautomation.com/t/pushing-data-from-ignition/94480>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [August 11, 2024, 11:43pm UTC](https://forum.inductiveautomation.com/t/pushing-data-from-ignition/94480 "2024-08-11T23:43:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel.C](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Daniel.C](https://forum.inductiveautomation.com/u/Daniel.C)\
**Post date:** [August 11, 2024, 11:43pm UTC](https://forum.inductiveautomation.com/t/pushing-data-from-ignition/94480/1 "2024-08-11T23:43:31Z")

</div>

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.

```python
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

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 12, 2024, 12:38am UTC](https://forum.inductiveautomation.com/t/pushing-data-from-ignition/94480/2 "2024-08-12T00:38:07Z")

</div>

What doesn't work?

---

<div class="post-metadata">

**Author:** ![Daniel.C](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Daniel.C](https://forum.inductiveautomation.com/u/Daniel.C)\
**Post date:** [August 12, 2024, 12:50am UTC](https://forum.inductiveautomation.com/t/pushing-data-from-ignition/94480/3 "2024-08-12T00:50:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 12, 2024, 12:52am UTC](https://forum.inductiveautomation.com/t/pushing-data-from-ignition/94480/4 "2024-08-12T00:52:11Z")

</div>

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

Help us help you.

---

<div class="post-metadata">

**Author:** ![Daniel.C](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Daniel.C](https://forum.inductiveautomation.com/u/Daniel.C)\
**Post date:** [August 12, 2024, 1:01am UTC](https://forum.inductiveautomation.com/t/pushing-data-from-ignition/94480/5 "2024-08-12T01:01:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [August 13, 2024, 2:25pm UTC](https://forum.inductiveautomation.com/t/pushing-data-from-ignition/94480/6 "2024-08-13T14:25:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 13, 2024, 2:48pm UTC](https://forum.inductiveautomation.com/t/pushing-data-from-ignition/94480/7 "2024-08-13T14:48:03Z")

</div>

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/f/4fe89bb95916597f0320094b305b3d53c147867e.png)

Find your logger and make sure the level is set to "trace"  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/5/15085eb836a112a9800c1edb96a4c13cb24ff8a4.png)

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.

---

<div class="post-metadata">

**Author:** ![cos](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cos/32/66963_2.png) [@cos](https://forum.inductiveautomation.com/u/cos)\
**Post date:** [August 13, 2024, 10:02pm UTC](https://forum.inductiveautomation.com/t/pushing-data-from-ignition/94480/8 "2024-08-13T22:02:51Z")

</div>

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.

> **[Error Handling | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/platform/scripting/python-scripting/error-handling#displaying-error-text)**
>
> What is Error Handling

Also: just use jsonEncode

> **[system.util.jsonEncode | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-util/system-util-jsonEncode)**
>
> Takes a Python object such as a list or dictionary and converts into a JSON string.

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

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 14, 2024, 7:35am UTC](https://forum.inductiveautomation.com/t/pushing-data-from-ignition/94480/9 "2024-08-14T07:35:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![simeonw](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@simeonw](https://forum.inductiveautomation.com/u/simeonw)\
**Post date:** [August 15, 2024, 2:32am UTC](https://forum.inductiveautomation.com/t/pushing-data-from-ignition/94480/10 "2024-08-15T02:32:05Z")

</div>

> [@Daniel.C](#):
>
> ` system.cirruslink.engine.publish`

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)](https://docs.chariot.io/display/CLD80/MQTT+Publishing+via+MQTT+Engine)

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](https://forum.inductiveautomation.com/t/publish-to-topics-using-system-cirruslink-engine-publish/33993)
