Edit Document tag type with script in perspective

Hi,

i'm trying to read a document type tag and edit a part of it adding a value to an element with a script on a button

right now i'm struggling because i keep getting types error.
i would like to "append" the value.

this is the document structure:

{
  "readOnly": true,
  "dataType": "Document",
  "name": "PerspectiveDisplayParams",
  "value": {
    "auxiliaries": {
      "template": "auxiliaries",
      "amount": 1,
      "displayName": "Auxiliaries",
      "iconPath": "icons/Perspective/Auxiliaries.png",
      "parameters": {
        "exception": [
        ],
        "popupTags": [
          {
            "title": "Three-phase Active power",
            "unit": "W",
            "tagPath": "/sts/PowerEqAux"
          },
          {
            "title": "Consumed Energy",
            "unit": "kWh",
            "tagPath": "/sts/EnergyAux"
          },
          {
            "title": "L1-N Tension",
            "unit": "V",
            "tagPath": "/sts/VoltageL1NAux"
          },
          {
            "title": "L1 Current",
            "unit": "A",
            "tagPath": "/sts/CurrentL1Aux"
          },
          {
            "title": "Power factor",
            "unit": "",
            "tagPath": "/sts/CosphiAux"
          },
          {
            "title": "Produced Energy",
            "unit": "kWh",
            "tagPath": "/sts/NegEnergyAux"
          }
        ],
        "amount": 30
      }
    }
  },
  "tagType": "AtomicTag"
}

and i would like to add my value in "exception".

i've tried to convert it to a list and then back to a pyList or pyArray but i can't "overwrite" on the "exception" section.

i can't find anything about this issue, does someone has an idea on how to manage this situation?

Read the value of the tag, then set your new value, then write the tag. Should really be that easy.

tagValue = system.tag.readBlocking(['pathToDocumentTag'])[0].value
tagValue['value']['auxiliaries']['parameters']['exception'] = 'Your new exception value'
system.tag.writeBlocking(['pathToDocumentTag'],[tagValue])

already tried, but i get:

TypeError: 'com.inductiveautomation.ignition.common.script.adapters.PyDocumentObjectAdapter' object is unsubscriptable

Show your actual script.

sure,

here it is

def runAction(self, event):

    user = self.session.props.auth.user.userName 
    DisplayParamsPath = '[default]ctrlXboost/' + user + '/DesignParameters/PerspectiveDisplayParams'
    i = int (self.getSibling("SelectedAux").props.text)
    
    tagValue = system.tag.readBlocking(DisplayParamsPath)[0].value
    tagValue['value']['auxiliaries']['parameters']['exception'] = i
    system.tag.writeBlocking(DisplayParamsPath,tagValue)

i'm getting the error on tagValue['value']['auxiliaries']['parameters']['exception'] = i

i can print the value of exception using

params.value.auxiliaries.parameters.exception

to access to it, but i can't give it a value using append or simply doing

params.value.auxiliaries.parameters.exception = value

Okay.

So this is a new one on me, has something to do with the way the object wrapper handles arrays. Either way here is a solution, I couldn't figure out a more graceful way. Perhaps @PGriffith will come along and whip out a way to do it with the Java types, I was unsuccessful.

def runAction(self, event):

    user = self.session.props.auth.user.userName 
    DisplayParamsPath = ['[default]ctrlXboost/' + user + '/DesignParameters/PerspectiveDisplayParams']
    i = int (self.getSibling("SelectedAux").props.text)
    
    tagValue = system.tag.readBlocking(DisplayParamsPath)[0].value
    jsonValue = system.util.jsonDecode(tagValue.toString())
    jsonValue['value']['auxiliaries']['parameters']['exception'].append(i)
    tagValue = system.util.jsonEncode(jsonValue)
    system.tag.writeBlocking(DisplayParamsPath,[tagValue])
2 Likes

PyJsonObjectAdapter has a toDict() method on it you can use. It predates my involvement in this area of the scripting system, so it's not as good at pretending to be a dictionary - that's why you can't assign a new value to it, only read from it as if it were immutable.

So you could change the script to avoid the slow stringify/json parsing operation:

    tagValue = system.tag.readBlocking(DisplayParamsPath)[0].value
    jsonValue = tagValue.toDict()
    jsonValue['value']['auxiliaries']['parameters']['exception'].append(i)
    # may or may not be needed:
    # tagValue = system.util.jsonEncode(jsonValue)
    system.tag.writeBlocking(DisplayParamsPath, [tagValue])
2 Likes

thank you so much for your help.

little corrections:

  • i had to use jsonValue['auxiliaries']['parameters']['exception'].append(i) since the script is already reading in "value" section

  • tagValue = system.util.jsonEncode(jsonValue) is needed or it will not write in the document tag

i've tried with to Dict() but gave me errors sigh

1 Like

I assumed that the json structure you posted was the structure for the document, not the tag json.

So, in the case that your document structure also contained a value key, then you would need to use value. Just to clarify for any who come along after the fact.

In otherwords, the .value at the end of the readBlocking() call is not the same as tagValue['value']

3 Likes

you're right, my bad.
with "structure" i meant what is inside the document tag.

thank's for the correction