# MQTT Publish Error

**URL:** <https://forum.inductiveautomation.com/t/mqtt-publish-error/46072>\
**Category:** Maker\
**Created:** [April 27, 2021, 4:20pm UTC](https://forum.inductiveautomation.com/t/mqtt-publish-error/46072 "2021-04-27T16:20:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew.ayre](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matthew.ayre/32/19499_2.png) [@matthew.ayre](https://forum.inductiveautomation.com/u/matthew.ayre)\
**Post date:** [April 27, 2021, 4:20pm UTC](https://forum.inductiveautomation.com/t/mqtt-publish-error/46072/1 "2021-04-27T16:20:43Z")

</div>

I am publishing to an MQTT device using system.cirruslink.engine.publish(). Everything works fine in terms of read/writes with the device. The issue I have is that I get the following error in the Gateway logs when running the publish function:

`Error processing edit for tag path '[Device Tag Path]/relay/0/command': Bad_Unsupported("The target path '[Device Tag Path]/relay/0' cannot accept children tags.")`

Any pointers into where to start looking? I understand that the issue lies with the command child topic, but this is the required path to write commands to the device.

I’m running on v8.1.4 of Maker Edition.

---

<div class="post-metadata">

**Author:** ![matthew.ayre](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matthew.ayre/32/19499_2.png) [@matthew.ayre](https://forum.inductiveautomation.com/u/matthew.ayre)\
**Post date:** [May 4, 2021, 8:14am UTC](https://forum.inductiveautomation.com/t/mqtt-publish-error/46072/2 "2021-05-04T08:14:47Z")

</div>

@wes0johnson, sorry to drag you in to this but you seem to be the go-to for MQTT 🙂

Any advice would be greatly appreciated. Also, I am using v4.0.6 of the various Cirrus Link modules.

---

<div class="post-metadata">

**Author:** ![wes0johnson](https://avatars.discourse-cdn.com/v4/letter/w/7ab992/32.png) [@wes0johnson](https://forum.inductiveautomation.com/u/wes0johnson)\
**Post date:** [May 4, 2021, 8:54pm UTC](https://forum.inductiveautomation.com/t/mqtt-publish-error/46072/3 "2021-05-04T20:54:21Z")

</div>

I’m not sure any of this has anything to do with MQTT specifically. First off, your tag path is wrong. You are using this:

```auto
[Device Tag Path]/relay/0/command

```

You should be using (note no slash after the tag provider):

```auto
[Device Tag Path]relay/0/command

```

Also, that error is coming from Ignition itself and suggests you are trying to add a ‘child tag’ to an existing leaf tag. You can only add child tags to UDTs and folders.

---

<div class="post-metadata">

**Author:** ![matthew.ayre](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matthew.ayre/32/19499_2.png) [@matthew.ayre](https://forum.inductiveautomation.com/u/matthew.ayre)\
**Post date:** [May 5, 2021, 7:05am UTC](https://forum.inductiveautomation.com/t/mqtt-publish-error/46072/4 "2021-05-05T07:05:46Z")

</div>

Thank you for the feedback @wes0johnson 🙂 I was just grasping at straws.

> [@wes0johnson](#):
>
> First off, your tag path is wrong.

Apologies, I was trying to be concise and instead caused confusion 😅. A true representation of the tag path would be: `[TagProvider]devices/model-deviceID/relay/0/command`

> [@wes0johnson](#):
>
> add a ‘child tag’ to an existing leaf tag

This is the clue I needed. It didn't flag in my brain that the `0` was a memory tag and couldn't have children.

The MQTT engine auto-generated the structure so that `0` was the last child (in the form of a memory tag). `.../relay/0` is the topic to read the value from. `.../relay/0/command` is the topic to control the state of the device. In your opinion what would be be the best way to handle this situation?

Sorry if it seems like a dumb question, this is still a bit new to me 🙃

---

<div class="post-metadata">

**Author:** ![wes0johnson](https://avatars.discourse-cdn.com/v4/letter/w/7ab992/32.png) [@wes0johnson](https://forum.inductiveautomation.com/u/wes0johnson)\
**Post date:** [May 5, 2021, 3:23pm UTC](https://forum.inductiveautomation.com/t/mqtt-publish-error/46072/5 "2021-05-05T15:23:51Z")

</div>

So, to read from the tag - as you noted you just read it using the path you denoted ending in  
`.../relay/0`

When you ‘write to the tag’ you really aren’t ‘writing to the tag’ from an Ignition perspective. You need to publish an MQTT message to tell the remote device to update the tag. I don’t know the details of your device, but it sounds like you need to do something like this in an Ignition script.

```auto
system.cirruslink.engine.publish("Chariot SCADA", "the/topic/representing/write/to/relay/0", str("some required payload for the device").encode(), 0, 0)

```

You will need to review your device documentation to determine what the topic and payload must be in order to result in a write to that output relay. Note in this case there is no tag that represents a ‘command’. It is all done via scripting and the publish API in MQTT Engine.

---

<div class="post-metadata">

**Author:** ![matthew.ayre](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matthew.ayre/32/19499_2.png) [@matthew.ayre](https://forum.inductiveautomation.com/u/matthew.ayre)\
**Post date:** [May 5, 2021, 4:17pm UTC](https://forum.inductiveautomation.com/t/mqtt-publish-error/46072/6 "2021-05-05T16:17:13Z")

</div>

I am using the publish function in my script 🙂 , here is an example of what I am doing:

```auto
def pubToDevice(device, command):
	# Dictionary of commands just in case they change in future firmware (easier to change in one place)
	commands = {
		'on':'on',
		'off':'off',
		'toggle':'toggle',
		'update':'update'
		}
	
	# Get the appropriate values
	topic = getDevice(device) + '/relay/0/command'
	pubCommand = str(commands[command]).encode()
	
	# Publish the command to the device
	system.cirruslink.engine.publish('Chariot SCADA', topic, pubCommand, 0, 0)

```

Publishing to the topic this way does work (e.g. updates the state of the device). The only issue I am having is that the error appears in the Gateway log.

This seems to be an issue with the way the device topics are setup more than anything else. For reference the devices are Shelly’s and [here](https://shelly-api-docs.shelly.cloud/#shelly1-1pm-mqtt) is the associated documentation.

Like I say it’s not a major issue as it’s not effecting functionality, it’s just spamming the Gateway event log with errors.

---

<div class="post-metadata">

**Author:** ![wes0johnson](https://avatars.discourse-cdn.com/v4/letter/w/7ab992/32.png) [@wes0johnson](https://forum.inductiveautomation.com/u/wes0johnson)\
**Post date:** [May 5, 2021, 4:38pm UTC](https://forum.inductiveautomation.com/t/mqtt-publish-error/46072/7 "2021-05-05T16:38:14Z")

</div>

Ah - I think I see what is happening. I think you probably have not scoped your custom namespace subscription in MQTT Engine narrowly enough. MQTT Engine sees the publishes occurring from the device, presumably on a topic like this:

```auto
deviceId/relay/0

```

But, you are publishing on a topic like:

```auto
deviceId/relay/0/command

```

I would guess you have a subscription for the custom namespace something like one of these or some other subscription that results in you getting both of these messages back and MQTT Engine:

```auto
deviceId/#
deviceId/relay/#

```

You could fix this by scoping the topic subscription of the custom namespace to exclude the command messages. For example, something like either of these would work.

```auto
deviceId/+/+
+/+/+

```

I don’t know the full set of topics you may want to subscribe to so you may need to adjust this. The real issue here is that MQTT Engine sees the command topic and tries to add the new tag(s) to the tag tree. But, a leaf tag already exists above this path and it fails with this error message. So, by limiting the topic subscription it would prevent MQTT Engine from ever seeing those (and it doesn’t really need to anyway).

---

<div class="post-metadata">

**Author:** ![matthew.ayre](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matthew.ayre/32/19499_2.png) [@matthew.ayre](https://forum.inductiveautomation.com/u/matthew.ayre)\
**Post date:** [May 5, 2021, 7:26pm UTC](https://forum.inductiveautomation.com/t/mqtt-publish-error/46072/8 "2021-05-05T19:26:28Z")

</div>

Now I’ve had a good read and a play, I understand what you are saying.

Thank you for taking the time to explain what’s going on 🙂 I’m a little less of a newb every day 😆
