Auto Engineering Unit

Hello,

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')

use a case

case( left({TagPrefix},1),
    'P', 'PSI',
    'F', 'MCFD', 
    'T', 'F',
    'L', '%',
    'D', 'INH2O',
    null
)
1 Like

That did the trick. Thank you for the quick reply!

2 Likes

Ewww! This may work, but it is a terrible idea. Tags have an engUnit property for a reason!

2 Likes

Add them here!

Use them like this. You can drill down to the EngUnit using the Browse Tags selector.

1 Like

That gives me Bingo! :joy:

I was so anxious to fix his expression, I skimmed over the actual application of it.

4 Likes

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

image
image

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.

You can just create a UDT param for EngUnit and then use this in all the tags’ eng unit props values needing these units

1 Like

Absolutely true but then the reality of Ignition struggling with embedding objects too many layers deep often comes into play unfortunately.

2 Likes