# Error when writing to chained derived Siemens OPC document tag but works with memory document tag

**URL:** <https://forum.inductiveautomation.com/t/error-when-writing-to-chained-derived-siemens-opc-document-tag-but-works-with-memory-document-tag/62905>\
**Category:** Ignition\
**Created:** [August 11, 2022, 6:19am UTC](https://forum.inductiveautomation.com/t/error-when-writing-to-chained-derived-siemens-opc-document-tag-but-works-with-memory-document-tag/62905 "2022-08-11T06:19:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 11, 2022, 6:19am UTC](https://forum.inductiveautomation.com/t/error-when-writing-to-chained-derived-siemens-opc-document-tag-but-works-with-memory-document-tag/62905/1 "2022-08-11T06:19:52Z")

</div>

A colleague is trying to use a Siemens PLC via onboard OPC-UA to read a nested UDT within a datablock. He’s reading the top level datablock in as a Document tag and then using a derived tag with jsonGet to read the nested UDT, then a derived integer tag with jsonGet to read a key from this tag. Essentially it’s doing `jsonGet(jsonGet({source}, "Seq"), "Command")`.  
Example tag JSON below which is in the format:  
**base** tag = opc tag, the root datablock tag  
**seq** tag = get “Seq” object from **base** tag  
**cmd** tag = get “Command” integer tag from **seq** tag (essentially, get `Seq.Command` from **base** tag)

When trying to write to the **cmd** tag, he’s seeing this error:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/6/e6b184493a1d45359b4d41b66e0409af50f14d01.jpeg)

```auto
INFO | jvm 1 | 2022/08/11 15:58:20 | W [o.e.m.o.s.c.s.OpcUaBinaryStreamEncoder] [05:58:19]: Not a built-in type: class com.inductiveautomation.ignition.common.document.Document 
INFO | jvm 1 | 2022/08/11 15:58:20 | E [o.e.m.o.s.c.t.u.UascClientMessageHandler] [05:58:19]: Error serializing WriteRequest(requestHeader=RequestHeader(authenticationToken=NodeId{ns=0, id=217572643}, timestamp=DateTime{utcTime=133046710999830000, javaDate=Thu Aug 11 15:58:19 AEST 2022}, requestHandle=6617, returnDiagnostics=0, auditEntryId=null, timeoutHint=60000, additionalHeader=null), nodesToWrite=[WriteValue(nodeId=NodeId{ns=3, id="SQ110_WM5"."SQ110_WM5"}, attributeId=13, indexRange=null, value=DataValue{value=Variant{value={
INFO | jvm 1 | 2022/08/11 15:58:20 | "Seq":{\n "\"Command\": 30\n}"
INFO | jvm 1 | 2022/08/11 15:58:20 | }}, status=null})]): unknown builtin type: -1 
INFO | jvm 1 | 2022/08/11 15:58:20 | org.eclipse.milo.opcua.stack.core.UaSerializationException: unknown builtin type: -1
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder.writeBuiltinType(OpcUaBinaryStreamEncoder.java:736)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder.writeValue(OpcUaBinaryStreamEncoder.java:654)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder.writeVariant(OpcUaBinaryStreamEncoder.java:637)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder.writeDataValue(OpcUaBinaryStreamEncoder.java:323)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder.writeDataValue(OpcUaBinaryStreamEncoder.java:879)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.types.structured.WriteValue$Codec.encode(WriteValue.java:106)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.types.structured.WriteValue$Codec.encode(WriteValue.java:86)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.serialization.codecs.GenericDataTypeCodec$GenericBinaryDataTypeCodec.encode(GenericDataTypeCodec.java:54)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder.writeStruct(OpcUaBinaryStreamEncoder.java:941)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder.lambda$writeStructArray$3(OpcUaBinaryStreamEncoder.java:1107)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder.writeArray(OpcUaBinaryStreamEncoder.java:83)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder.writeStructArray(OpcUaBinaryStreamEncoder.java:1107)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder.writeStructArray(OpcUaBinaryStreamEncoder.java:1123)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.types.structured.WriteRequest$Codec.encode(WriteRequest.java:85)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.types.structured.WriteRequest$Codec.encode(WriteRequest.java:69)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.serialization.codecs.GenericDataTypeCodec$GenericBinaryDataTypeCodec.encode(GenericDataTypeCodec.java:54)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder.writeMessage(OpcUaBinaryStreamEncoder.java:918)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.client.transport.uasc.UascClientMessageHandler.lambda$encode$9(UascClientMessageHandler.java:334)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.channel.SerializationQueue.lambda$encode$0(SerializationQueue.java:52)
INFO | jvm 1 | 2022/08/11 15:58:20 | at org.eclipse.milo.opcua.stack.core.util.ExecutionQueue$Task.run(ExecutionQueue.java:119)
INFO | jvm 1 | 2022/08/11 15:58:20 | at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
INFO | jvm 1 | 2022/08/11 15:58:20 | at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

```

If he converts the **base** opc tag into a memory tag and writes the same document object to the memory tag, then the write to the **cmd** tag works fine.

JSON for the OPC tag and derived tags where the **cmd** tag write fails:

```auto
{
  "tags": [
    {
  "valueSource": "memory",
  "opcItemPath": "nsu\u003dhttp://www.siemens.com/simatic-s7-opcua;s\u003d\"SQ110_WM5\".\"SQ110_WM5\"",
  "dataType": "Document",
  "name": "base",
  "value": {
    "Seq": {
      "Command": 5
  },
  "tagType": "AtomicTag",
  "opcServer": "BBD_PLC2"
},
    {
      "valueSource": "derived",
      "deriveExpressionGetter": "jsonGet({source}, \u0027Command\u0027)",
      "dataType": "Int4",
      "deriveExpressionSetter": "jsonSet({source}, \"Command\", {value})",
      "sourceTagPath": "[.]seq",
      "name": "cmd",
      "tagType": "AtomicTag"
    },
    {
      "valueSource": "derived",
      "deriveExpressionGetter": "jsonGet({source}, \u0027Seq\u0027)",
      "dataType": "Document",
      "deriveExpressionSetter": "jsonSet({source}, \"Seq\", {value})",
      "sourceTagPath": "[.]base",
      "name": "seq",
      "tagType": "AtomicTag"
    }
  ]
}

```

JSON for the memory tag + derived tags where the **cmd** tag writes successfully

```auto
{
  "tags": [
    {
      "valueSource": "memory",
      "dataType": "Document",
      "name": "base",
      "value": {
        "Seq": "{\n \"Command\": 8}"
      },
      "tagType": "AtomicTag"
    },
    {
      "valueSource": "derived",
      "deriveExpressionGetter": "jsonGet({source}, \u0027Command\u0027)",
      "dataType": "Int4",
      "deriveExpressionSetter": "jsonSet({source}, \"Command\", {value})",
      "sourceTagPath": "[.]seq",
      "name": "cmd",
      "tagType": "AtomicTag"
    },
    {
      "valueSource": "derived",
      "deriveExpressionGetter": "jsonGet({source}, \u0027Seq\u0027)",
      "dataType": "Document",
      "deriveExpressionSetter": "jsonSet({source}, \"Seq\", {value})",
      "sourceTagPath": "[.]base",
      "name": "seq",
      "tagType": "AtomicTag"
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![deon.korb](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@deon.korb](https://forum.inductiveautomation.com/u/deon.korb)\
**Post date:** [August 11, 2022, 11:55am UTC](https://forum.inductiveautomation.com/t/error-when-writing-to-chained-derived-siemens-opc-document-tag-but-works-with-memory-document-tag/62905/2 "2022-08-11T11:55:39Z")

</div>

Does `jsonGet({source}, "Seq.Command")` 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 11, 2022, 1:42pm UTC](https://forum.inductiveautomation.com/t/error-when-writing-to-chained-derived-siemens-opc-document-tag-but-works-with-memory-document-tag/62905/3 "2022-08-11T13:42:44Z")

</div>

> [@nminchin](#):
>
> `"Seq":{\n "\"Command\": 30\n}"`

The problem is that this is the value that ends up at the OPC client layer, which isn't complete and can't be turned into a struct.

I can't tell if you omitted other members for the sake of the example or if they don't exist, but you have to fully define every member of both the host and nested types with derived tags and json get/set if you're going to be writing to any of the tags. When we write to a structure, we have to write the whole structure, and this derived tag scheme can't assemble the whole structure unless you've defined all of the tags.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 12, 2022, 12:31am UTC](https://forum.inductiveautomation.com/t/error-when-writing-to-chained-derived-siemens-opc-document-tag-but-works-with-memory-document-tag/62905/4 "2022-08-12T00:31:45Z")

</div>

> [@deon.korb](#):
>
> `jsonGet({source}, "Seq.Command")`

This works! or rather, the write version works (the read was already working). Thanks 😃

```auto
jsonSet({source}, "Seq.Command", {value})

```

> [@Kevin.Herron](#):
>
> I can’t tell if you omitted other members for the sake of the example or if they don’t exist

Correct, I reduced (or tried to) the structure to simplify my example and may have stuffed it up 🙂 ; the actual structure has a lot more tags.

---

<div class="post-metadata">

**Author:** ![jbartsch](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jbartsch](https://forum.inductiveautomation.com/u/jbartsch)\
**Post date:** [September 1, 2022, 8:10pm UTC](https://forum.inductiveautomation.com/t/error-when-writing-to-chained-derived-siemens-opc-document-tag-but-works-with-memory-document-tag/62905/5 "2022-09-01T20:10:08Z")

</div>

> [@nminchin](#):
>
> r the OPC tag and derived tags where the **cmd**

I came across this post in a hunt for a solution that's very similar to this post. Only in my case the value is nested in one more structure.  
This expression fails with "Bad\_EndcodingError:..." :  
jsonSet({source}, "ProcessResults.CNC.MachiningCenter", {value})

But if I hard code the value to 10 it will write 10 when the tag is modified  
jsonSet({source}, "ProcessResults.CNC.MachiningCenter", 10)

Any thoughts in what I maybe missing here?

---

<div class="post-metadata">

**Author:** ![jbartsch](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jbartsch](https://forum.inductiveautomation.com/u/jbartsch)\
**Post date:** [September 1, 2022, 8:22pm UTC](https://forum.inductiveautomation.com/t/error-when-writing-to-chained-derived-siemens-opc-document-tag-but-works-with-memory-document-tag/62905/6 "2022-09-01T20:22:06Z")

</div>

🤪 my bad… Tag Data Type was set to “string”, when I set it correctly to “integer” the {value} works
