Hello all,
I am trying to write the value of a tag into another tag if a condition is fulfilled. I cannot seem to get it to work.
Background: I have 8 MQTT Brokers that send data with the same topic. To split their signals, I thought I could make a script on the DeviceName tag that writes into a memory tag when the correct device name is the most recent value of DeviceName. This is the script:
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
if "COMTAC" in '[.]DeviceName':
system.tag.writeBlocking(['[.]Office_06/PayloadString_06'],['[.]PayloadString.value'])
system.tag.writeBlocking(['[.]Office_06/PayloadArray_06'],['[.]PayloadArray.value'])
DeviceName is a reference tag referencing the MQTT tag where the device name is stored (string).
PayloadArray is a reference tag referencing the MQTT tag where the data is stored as JSON coded. I configured PayloadArray as binary data and get an array where I can grab/maniopulate my data so that I can interpret it.
PayloadString is referencing the same MQTT tag but is configured as string.
Unfortunately I am not experienced with Python/Jython and the script console did not help me formulate the if statement correctly. Can anyone point me in the right direction?
Please see Wiki - how to post code on this forum. We need formatted code to check for indentation errors, etc., and to make it easier to read. There's an edit button below your post so you can fix it.
This is looking for the string "COMTAC" inside the string '[.]DeviceName'. It will always return False.
Shouldn't it be something like, if "COMTAC" in currentValue.value:
I would say you'd want something like the following. You will need to read the tag values with system.tag.readBlocking and then write them over. Also combine your writeBlocking into one statement rather than two.
if "COMTAC" in currentValue.value:
tagValues = system.tag.readBlocking([['[.]PayloadString','[.]PayloadArray']])
system.tag.writeBlocking(
['[.]Office_06/PayloadString_06','[.]Office_06/PayloadArray_06'],
[tagValues[0].value,tagValues[1].value])
You may also consider an expression tag that looks for a condition.
We use expression tags or reference tags sometimes to pull data from the MQTT provider via Cirrus Link (If you are using that or wherever your MQTT payloads are coming in from)
This, but note that tag valueChange events should only use writeBlocking for memory tags. Other kinds of tags can stall tag event processing while communicating--use writeAsync to write to tags that point at external equipment.
I have copied Benjamin's code but unfortunately none of the memory tags get written in. I would assume the error is on my side. Could I have blocked write access while configuring memory tags or something similar?
I just assumed currentValue.value would be the device name, but that may not be the case. You may need to read the device name from that tag for it to work.
I would make sure you are getting an output at all from the value change script and what that output is. You can do some logging to see the changes. I typically use system.util.getLogger just because I like to use the diagnostics popup.
Then verify that you are seeing the value expected for that device name.
Hello Benjamin,
I'm not sure if I got the logging function to work but I found the problem. When checking the Tag Diagnostics from the Tag Editor, I noticed the error message "NameError: global name 'tagValues' is not defined." After using Google for an answer I found out that the problem was that I had the code like this ( "=" twice instead of once).