Hello All,
I’m using a Script transform in indirect tag binding the goal is to whenever a tag value not changed for more than 5 sec transform value should return InActive for this i have collected the timestamp of the tag and got the current timestamp using system.date.now() i compared both these time using system.date.secondsBetween(a,b) whenver the tag value not changed for five sec it’s not updating
how to resolve this issue, Below is my code
Thanks & Regards,
Sri Haran
a=system.tag.read("[MQTT Engine]Data_Factory/Health/"+str(self.view.params.asset_code))
a=a.getTimestamp()
# print(a)
b=system.date.now()
# print(b)
time = system.date.secondsBetween(a,b)
if time <= 5 :
return("Active")
elif 5< time <300:
return ("InActive")
elif time >=300:
return("InActive")
else:
return("Error")
I believe your script should work. I think the reason it may not appear to be working is the indirect tag binding.
If you have the binding on the tag you are trying to monitor your time value will always be zero. You will need to bind to something that is continually polling such as [System]Gateway/CurrentDateTime. You can also then use the tag value in your script by replacing b=system.date.now() with b = value
As User77 says, the script should be okay, but you can verify it using the script console. But tag bindings will only trigger if the tag they are bound to changes. You can set your binding to an expression binding, for example:
which will run the script every second (as an expression will run if any part of it changes) and return the time difference. You can then have an expression ‘if’ statement do the rest. For some reason, though I don’t if others have had this, I get performance issues if I use a lot of scripts for my tag bindings but expressions tend to work better.
Hi All,
I solved this by binding an direct tag of “[System]Gateway/CurrentDateTime” which will update for every second and then i changed my script slightly like this
Thanks & Regards,
Sri
a=system.tag.read(“[MQTT Engine]Data_Factory/Health/”+str(self.view.params.asset_code))
a=a.getTimestamp()
# print(a)
b=system.tag.read(“[System]Gateway/CurrentDateTime”)
b= b.getTimestamp()
# print(b)
time = system.date.secondsBetween(a,b)
if time <= 5 :
return(“Active”)
elif 5< time <300 :
return(“InActive”)
elif time >=300:
return(“InActive”)