How to write tag with condition

I want to write the value of a numeric label to a tag.

I usually use this script in the property change to write into a tag.

if event.propertyName == “intValue”:
value = event.source.intValue
system.tag.write(“Output1”, value)

Now I want to add a condition to write in a particular tag based on a selection like a dropdown list
as the value of the numeric label has a reference from the dropdown list.

Like if 1 is selected it will be for Output1
2 will be for Output2 …

Can anyone help me on my script…

Thanks in advance…

1 Like

Yes, sure. Here is an example:

if event.propertyName == "intValue":
	selectedValue = event.source.parent.getComponent('Dropdown').selectedValue	
	if selectedValue == 1:
		tag = "Output1"
	elif selectedValue == 2:
		tag = "Output2"
	value = event.source.intValue
	system.tag.write(tag, value)

Or more elegantly it could be done like this:

if event.propertyName == "intValue":
	selectedValue = event.source.parent.getComponent('Dropdown').selectedValue
	system.tag.write("Output%s"%selectedValue, event.source.intValue)

Thanks Nick @nmudge it is a big help for me as a newbie here… I truly appreciate it…:wink:

While probably not what you need, this may be one to keep in mind for the future.
You can use the dropdown menu’s dataset to define the tagpath and tag value to write for each item in the drop down menu. For this to work, you would need to add another column/field to the drop down menu’s data property called “TagPath” and “Value”.

ddm = event.source.parent.getComponent('Dropdown')
ds = system.dataset.toPyDataSet(ddm.data)
selectedValue = ddm.selectedValue

tagPath = ds[selectedValue]["TagPath"]
tagValue = ds[selectedValue]["Value"]
system.tag.write(tagPath, tagValue)

This will write the value in the Value field to the Tag defined by the TagPath in the dataset, for the selected index of the drop down menu.