# Write Value into tag with if condition

**URL:** <https://forum.inductiveautomation.com/t/write-value-into-tag-with-if-condition/100157>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [December 23, 2024, 10:31am UTC](https://forum.inductiveautomation.com/t/write-value-into-tag-with-if-condition/100157 "2024-12-23T10:31:31Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_Glienke](https://avatars.discourse-cdn.com/v4/letter/n/f475e1/32.png) [@Nicolas\_Glienke](https://forum.inductiveautomation.com/u/Nicolas_Glienke)\
**Post date:** [December 23, 2024, 10:31am UTC](https://forum.inductiveautomation.com/t/write-value-into-tag-with-if-condition/100157/1 "2024-12-23T10:31:31Z")

</div>

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:

```python
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?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [December 23, 2024, 12:13pm UTC](https://forum.inductiveautomation.com/t/write-value-into-tag-with-if-condition/100157/2 "2024-12-23T12:13:57Z")

</div>

Please see [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831). 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.

---

<div class="post-metadata">

**Author:** ![Nicolas\_Glienke](https://avatars.discourse-cdn.com/v4/letter/n/f475e1/32.png) [@Nicolas\_Glienke](https://forum.inductiveautomation.com/u/Nicolas_Glienke)\
**Post date:** [December 23, 2024, 12:18pm UTC](https://forum.inductiveautomation.com/t/write-value-into-tag-with-if-condition/100157/3 "2024-12-23T12:18:29Z")

</div>

thanks for the tip, I edited the post

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [December 23, 2024, 12:27pm UTC](https://forum.inductiveautomation.com/t/write-value-into-tag-with-if-condition/100157/4 "2024-12-23T12:27:54Z")

</div>

> [@Nicolas\_Glienke](#):
>
> `if "COMTAC" in '[.]DeviceName': `

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:`

---

<div class="post-metadata">

**Author:** ![Benjamin\_Furlani](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/benjamin_furlani/32/66084_2.png) [@Benjamin\_Furlani](https://forum.inductiveautomation.com/u/Benjamin_Furlani)\
**Post date:** [December 23, 2024, 12:39pm UTC](https://forum.inductiveautomation.com/t/write-value-into-tag-with-if-condition/100157/5 "2024-12-23T12:39:17Z")

</div>

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.

```Python
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)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 23, 2024, 1:15pm UTC](https://forum.inductiveautomation.com/t/write-value-into-tag-with-if-condition/100157/6 "2024-12-23T13:15:39Z")

</div>

> [@Benjamin\_Furlani](#):
>
> combine your writeBlocking

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.

---

<div class="post-metadata">

**Author:** ![Nicolas\_Glienke](https://avatars.discourse-cdn.com/v4/letter/n/f475e1/32.png) [@Nicolas\_Glienke](https://forum.inductiveautomation.com/u/Nicolas_Glienke)\
**Post date:** [December 23, 2024, 1:31pm UTC](https://forum.inductiveautomation.com/t/write-value-into-tag-with-if-condition/100157/7 "2024-12-23T13:31:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Nicolas\_Glienke](https://avatars.discourse-cdn.com/v4/letter/n/f475e1/32.png) [@Nicolas\_Glienke](https://forum.inductiveautomation.com/u/Nicolas_Glienke)\
**Post date:** [December 23, 2024, 1:31pm UTC](https://forum.inductiveautomation.com/t/write-value-into-tag-with-if-condition/100157/8 "2024-12-23T13:31:55Z")

</div>

thank you for the advice, in this case I am only trying to write into memory tags.

---

<div class="post-metadata">

**Author:** ![Benjamin\_Furlani](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/benjamin_furlani/32/66084_2.png) [@Benjamin\_Furlani](https://forum.inductiveautomation.com/u/Benjamin_Furlani)\
**Post date:** [December 23, 2024, 1:50pm UTC](https://forum.inductiveautomation.com/t/write-value-into-tag-with-if-condition/100157/9 "2024-12-23T13:50:40Z")

</div>

> [@Benjamin\_Furlani](#):
>
> I would say you'd want something _like_ the following

Keep in mind I said like the following.

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.

---

<div class="post-metadata">

**Author:** ![Nicolas\_Glienke](https://avatars.discourse-cdn.com/v4/letter/n/f475e1/32.png) [@Nicolas\_Glienke](https://forum.inductiveautomation.com/u/Nicolas_Glienke)\
**Post date:** [December 23, 2024, 1:54pm UTC](https://forum.inductiveautomation.com/t/write-value-into-tag-with-if-condition/100157/10 "2024-12-23T13:54:23Z")

</div>

since I am writing my script on the DeviceName tag, i think you assume correctly

---

<div class="post-metadata">

**Author:** ![Benjamin\_Furlani](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/benjamin_furlani/32/66084_2.png) [@Benjamin\_Furlani](https://forum.inductiveautomation.com/u/Benjamin_Furlani)\
**Post date:** [December 23, 2024, 2:03pm UTC](https://forum.inductiveautomation.com/t/write-value-into-tag-with-if-condition/100157/11 "2024-12-23T14:03:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Nicolas\_Glienke](https://avatars.discourse-cdn.com/v4/letter/n/f475e1/32.png) [@Nicolas\_Glienke](https://forum.inductiveautomation.com/u/Nicolas_Glienke)\
**Post date:** [December 24, 2024, 7:31am UTC](https://forum.inductiveautomation.com/t/write-value-into-tag-with-if-condition/100157/12 "2024-12-24T07:31:21Z")

</div>

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).

`tagValues == system.tag.readBlocking([['[.]PayloadString','[.]PayloadArray']])`

I must have changed your code by trial and error approach.

Thank you very much for your help! Merry Christmas 🙂

---

<div class="post-metadata">

**Author:** ![Benjamin\_Furlani](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/benjamin_furlani/32/66084_2.png) [@Benjamin\_Furlani](https://forum.inductiveautomation.com/u/Benjamin_Furlani)\
**Post date:** [December 24, 2024, 5:55pm UTC](https://forum.inductiveautomation.com/t/write-value-into-tag-with-if-condition/100157/13 "2024-12-24T17:55:37Z")

</div>

Glad I could help!

If you found a solution through this, please mark the solution so others can see if their similar problems have been solved in this topic easily!

Same to you!
