Issue writing value to tag in script

Try the following:

tank_sel = event.source.parent.tank                # 1
tagPath = '[default]Filler_Data/DF2_SOURCE_TANK'   # 2
sourceTank = system.tag.readBlocking([tagPath])    # 3

if sourceTank[0].value:                            # 4
    output = 0
else:
    output = 2 ** (tank_sel - 1)                   # 5

system.tag.writeBlocking([tagPath], [output])      # 6

Explanation:

  1. Original code.
  2. Readability improvement.
  3. readBlocking expects a list of tag paths. (It does work with only one value not in a list but it's not documented and could change in a future version.) readBlocking returns a list of all the "qualified values" (value, OPC quality, timestamp).
  4. Get the value (using .value) of the first (and, in this case, only) returned tag. The > 0 can be omitted because if returns True for any non-zero value.
  5. Your output value for tanks 1, 2, 3, 4 is 1, 2, 4, 8 so use the exponent to set the value in one line.
  6. As with readBlocking, writeBlocking requires lists of tag paths and values.

I think your basic problem was that you were checking the qualified tag where you needed to check .value.

See this part of the manual:

https://docs.inductiveautomation.com/display/DOC81/Reading+and+Writing+to+Tags#ReadingandWritingtoTags-ManualTagReads

1 Like