A mistake with Entity and Schema while changing duct fitting coefficient

Hello everyone! Now I’m working on Revit ventilation systems. I face a problem – there’re now ASHRAE coefficient for many transition fittings and tee (tap) fittings. So I created a system in my families that count a coefficient by Dynamo Scripts and it’s all OK with it. I found 2 Phyton Scripts on forum – first of them change DUCT_FITTING_LOSS_METHOD_SERVER_PARAM for what I want (I want coefficient), and second rewrite an old value by calculated value from family. And when I start my script on a new duct system, it always made a mistake. When I start again, there’re no mistake and coefficient is rewriting correctly. The mistake is:

After second Play there’s no mistake. Can anyone please understand me what’s a problem and how to improve it? Unfortunately I can’t attach

Hi,

Did you close dynamo after the first run?
You can also use the player.

Yes, I do. When I start the script again, all is OK. When I use Dynamo Player, there’s nothing difference – mistake for a first time and then all is OK. I want to understand what type of mistake is it, how to deal with it and finally to do a script which doesn’t disturb my engineers by strange mistakes.

Can you share youre script and youre python script?

Sorry, I can’t attach a file because I’ve just registerede yesterday. But I can attach a code. This code I take from that: Change Duct Loss Coefficient to Another Fitting in ASHRAE Table - #21 by til.shviger and changed it for specific coefficient

import clr
clr.AddReference(“RevitAPI”)
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.ExternalService import *
from Autodesk.Revit.DB.ExtensibleStorage import *
from System.Collections.Generic import *

clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *

clr.AddReference(“RevitNodes”)
import Revit
clr.ImportExtensions(Revit.Elements)

clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

def getServerById(serverGUID, serviceId):
service = ExternalServiceRegistry.GetService(serviceId)
if service != “null” and serverGUID != “null”:
server = service.GetServer(serverGUID)
if server != “null”:
return server
return null
lc=
def getLossMethods(serviceId):
service = ExternalServiceRegistry.GetService(serviceId)
serverIds = service.GetRegisteredServerIds()
list=ListElementId
for serverId in serverIds:
server = getServerById(serverId, serviceId)
#ductServer = server.IDuctFittingAndAccessoryPressureDropServer

	id=serverId
	name=server.GetName()
	lc.append(id)
	lc.append(name)
	lc.append(server)
	
return lc

elems_=UnwrapElement(IN[0])
SetNew = IN[1]
elem=

elems=zip(elems_, SetNew)

for el in elems:
eleId = el[0].Id
fitting = doc.GetElement(eleId)
param = fitting.get_Parameter(BuiltInParameter.RBS_DUCT_FITTING_LOSS_METHOD_SERVER_PARAM)
lc = getLossMethods(ExternalServices.BuiltInExternalServices.DuctFittingAndAccessoryPressureDropService)
schema = lc[8].GetDataSchema()
field = schema.GetField(“Coefficient”)
entity=fitting.GetEntity(schema)

oldval = entity.Get[field.ValueType](field) # obtaining values

TransactionManager.Instance.EnsureInTransaction(doc)
entity.Set[field.ValueType](field, el[1]) # installation of a new coefficient value of certain
fitting.SetEntity(entity)
TransactionManager.Instance.TransactionTaskDone()
elem.append(fitting)

OUT = elem ,lc

#OUT = [UnwrapElement(i).FieldName for i in schema.ListField