I am trying to figure out a way to automatically determine my engineering unit based off the tag name that I enter in. For example, if my tag starts with a ‘T’ then I want my unit to be F*, or if my tag starts with a ‘P’ then I want my unit to be ‘PSI’ etc. I’m using an expression tag with the data type set to String inside my UDT that has the following script/code, but I can’t seem to get it to work write. Any thoughts or best practices?
If({TagPrefix} like 'P%', 'PSI', 'Null')||
If({TagPrefix} like 'F%', 'MCFD', 'Null') ||
If({TagPrefix} like 'T%', 'F', 'Null') ||
If({TagPrefix} like 'L%', '%', 'Null')||
IF({TagPrefix} like 'D%', 'INH2O', 'Null')
Mini rant but it annoys me to no end the fact that there is no way to make labels respect the formatting set in the tag and/or display the units without additional bindings. Like the data is already in the tag and it can correctly format it in the tag browser. Why isn't there a way to bind that. Having to throw 3 bindings for the pv, format, and units and then an expression to combine it all just seems like something that could be made more efficient.
I really wish we had a PV.value and a PV.valueFormatted option or something
As others have said for OP's question though, I'd ditch the case statement and just write your values into the EngUnits field and then use that. You can even run a script to set them all if you're concerned with trying to avoid manual effort
True, but you should be able to create a view taking a tag path as a parameter and have the view retrieve the value, formatString and EngUnit and render them all appropriately. Then you can quickly embed that view anywhere.