I have an indicator for a conveyor. it uses 3 different tags from the plc to drive various states. I tried to create an expression that grabs the name of the component to dynamically change the names of these tags so that I can just copy and paste the indicator then change the name. I found that the expression function does not like this.
Code that works looks like this:
if ({[default]ROOT/MAPS/OVLD/S6810_OVLD} = 1,
4,
if ({[default]ROOT/MAPS/FULL/S6810_FULL} = 1,
3,
if ({[default]ROOT/MAPS/FULL/S6810_AUX} = 1,
1,
0)))
I want this:
if ({[default]ROOT/MAPS/OVLD/ + component.name +_OVLD} = 1,
4,
if ({[default]ROOT/MAPS/FULL/ + component.name +_FULL} = 1,
3,
if ({[default]ROOT/MAPS/FULL/ + component.name +_AUX} = 1,
1,
0)))
If this can't work is there a different way to make this happen or will I need to copy paste the script and change the names in the script every time I want a new indicator?
Caution: While as @Transistor shows, the tag() expression will work, it is strongly recommended that you avoid it's use for basically anything outside of expression tags. Enough of this in the UI will kill your performance.
The proper way to accomplish this type of thing is to use indirect tag bindings. Something like:
Create a custom property to hold the value of each tag.
For each custom property use an indirect tag binding to reference the component name in the tag path.
Then you can change your Expression to the following:
NOTE: Order in the binEnc is important. I would be very tempted to change the result for FULL from 3 to 2. Then you could just simply use a binEnc() expression so long as other logic insures that only one of the tags are ever true.
If you want to emulate exactly what your if() expression does that would look like:
If I were to use the path using the root container then it would go against the purpose of making the custom properties in an effort to be able to copy, paste then rename the indicator.
You can make the properties on the comment itself, or better yet create a template and create the properties there. Just make sure that you make the device name property external so you can set it from outside the template.
Is your template named "component"? If not, that won't work. When you pick a property for the indirection, make sure you're using the picker instead of freely typing in something
Edit: Also, remove your quotes on the indirect tag.