# How to transform some tags into 1 tag easily

**URL:** <https://forum.inductiveautomation.com/t/how-to-transform-some-tags-into-1-tag-easily/98195>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [November 5, 2024, 10:33am UTC](https://forum.inductiveautomation.com/t/how-to-transform-some-tags-into-1-tag-easily/98195 "2024-11-05T10:33:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielps1818](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@danielps1818](https://forum.inductiveautomation.com/u/danielps1818)\
**Post date:** [November 5, 2024, 10:33am UTC](https://forum.inductiveautomation.com/t/how-to-transform-some-tags-into-1-tag-easily/98195/1 "2024-11-05T10:33:54Z")

</div>

Hello everyone!

I have some bool tags:  
`tag1`, `tag2` and `tag3`

And an result tag: `result`

I have some combinations that generates a different input depending on the tags. For example:

| tag1 | tag2 | tag3 | result |
| --- | --- | --- | --- |
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | - |
| 0 | 1 | 1 | - |
| 1 | 0 | 0 | 4 |
| 1 | 0 | 1 | 7 |
| 1 | 1 | 0 | 8 |
| 1 | 1 | 1 | 99 |

How could I make this transformation? I was thinking of using the `Expression Tag` but it's a bit of a nightmare with the expression language.

Any idea of how could I do this **more easily**?

Any help is appreciated.

_Note: In this example tag1, tag2 and tag3 are bool but could be int or float too._

_Edit: Not looking too much into it the most viable way i'm thinking is using runScript() from the expression language._

---

<div class="post-metadata">

**Author:** ![danielps1818](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@danielps1818](https://forum.inductiveautomation.com/u/danielps1818)\
**Post date:** [November 5, 2024, 10:40am UTC](https://forum.inductiveautomation.com/t/how-to-transform-some-tags-into-1-tag-easily/98195/2 "2024-11-05T10:40:37Z")

</div>

One person in another forum suggested using a dictionary:

```python
{
    "0":{
        "0":{
            "0":0,
            "1":0
        },
        "1":{
            "0":"-",
            "1":"-"
        }
    },
    "1":{
        "0":{
            "0":4,
            "1":7
        },
        "1":{
            "0":8,
            "1":99
        }
    }
}

```

But for some reason I really hate this approach even if it's a good idea.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 5, 2024, 10:56am UTC](https://forum.inductiveautomation.com/t/how-to-transform-some-tags-into-1-tag-easily/98195/3 "2024-11-05T10:56:22Z")

</div>

Make the result tag an expression binding and use the following expression:

```python
case(
	binEnc(
		{[.]Tag3}, // +1 LSB
		{[.]Tag2}, // +2
		{[.]Tag1} // +4 MSB
	),
	0, 0,
	1, 0,
	4, 4,
	5, 7,
	6, 8,
	7, 99,
	null
)

```

> **[binEnc | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/expression-functions/logic/binEnc)**
>
> Takes a list of booleans and treats them like the bits in a binary number.

> **[case | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/expression-functions/logic/case)**
>
> This function acts like the switch statement in C-like programming languages.

> ... but it's a bit of a nightmare with the expression language.

Nah!

---

<div class="post-metadata">

**Author:** ![danielps1818](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@danielps1818](https://forum.inductiveautomation.com/u/danielps1818)\
**Post date:** [November 5, 2024, 11:09am UTC](https://forum.inductiveautomation.com/t/how-to-transform-some-tags-into-1-tag-easily/98195/4 "2024-11-05T11:09:55Z")

</div>

Wow, didn't knew about binEnc.

However, when I have a value that is not binary and is an `Integer` I will have to transform to bits and encode thoes too.

Thanks a lot @Transistor !

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 5, 2024, 11:16am UTC](https://forum.inductiveautomation.com/t/how-to-transform-some-tags-into-1-tag-easily/98195/5 "2024-11-05T11:16:40Z")

</div>

If it's already an integer then the bits are already encoded.

If you have bits and an integer then just shift the integer bits left by multiplication:

```python
case(
	binEnc(
		{[.]Tag3}, // +1 LSB
		{[.]Tag2}, // +2
		{[.]Tag1} // +4 MSB
	)
    + {[.]IntTag} * 8, // Shift left by 3 bits.

```

---

<div class="post-metadata">

**Author:** ![Samuel\_Sueur](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@Samuel\_Sueur](https://forum.inductiveautomation.com/u/Samuel_Sueur)\
**Post date:** [November 5, 2024, 11:55am UTC](https://forum.inductiveautomation.com/t/how-to-transform-some-tags-into-1-tag-easily/98195/6 "2024-11-05T11:55:14Z")

</div>

I know you said you hated this approach, but you could do something like below:

```python
resultTable = {
    "0#0#0": 0,
    "0#0#1": 0,
    "0#1#0": "-",
    ..........
    "1#1#1": 99 
}

```

Instead of having nested dictionaries, you could just join the values of the 3 tags with the character # for instance (or any other character really, could even be a comma) and then you could just access the key that has the same value as the joined string.

```python
key = "#".join([str(valTag1), str(valTag2), str(valTag3)])
return resultTable[key]

```

I personally think this is more readable and easier to add new entries but again just my opinion...  
This would work with floats and integers too.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 5, 2024, 12:26pm UTC](https://forum.inductiveautomation.com/t/how-to-transform-some-tags-into-1-tag-easily/98195/7 "2024-11-05T12:26:43Z")

</div>

To explain your use of dictionaries fully you'd need to address where the script would go and what would trigger the update when one of the dependencies changes.

The expression does this automatically and doesn't require loading the Jython interpreter every time it's triggered and the expression is in the actual tag. Note that you can't use Perspective's Expression Structure binding and transforms on an expression tag.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 5, 2024, 4:49pm UTC](https://forum.inductiveautomation.com/t/how-to-transform-some-tags-into-1-tag-easily/98195/8 "2024-11-05T16:49:30Z")

</div>

Combine both approaches:

```python
case(
	stringFormat("%d.%d.%d", {Tag1}, {Tag2}, {Tag3}),
	"0.0.0", 0
	"1.1.1", 99
	...
	// fallback
)

```

Fast(ish), because expressions, and more readable/maintainable.

---

<div class="post-metadata">

**Author:** ![danielps1818](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@danielps1818](https://forum.inductiveautomation.com/u/danielps1818)\
**Post date:** [November 6, 2024, 10:47am UTC](https://forum.inductiveautomation.com/t/how-to-transform-some-tags-into-1-tag-easily/98195/9 "2024-11-06T10:47:37Z")

</div>

Your soultion is the one.

However, I think you mean `stringFormat()` instead of `numberFormat()`.

Could be?

```python
case(
	stringFormat(
		"%s.%s",
		{[.]tag},
		{[.]tag}
	),
	"1.1", 97,
	"5.5", 98,
	"2.2", 14,
	"3.3", 89,
	"4.4", 88,
	99 // fallback
)

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 6, 2024, 4:37pm UTC](https://forum.inductiveautomation.com/t/how-to-transform-some-tags-into-1-tag-easily/98195/10 "2024-11-06T16:37:34Z")

</div>

Yes, you're right; `numberFormat` is limited to converting a single number to a string, `stringFormat` is what you want here to concatenate + format in one operation.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 6, 2024, 8:27pm UTC](https://forum.inductiveautomation.com/t/how-to-transform-some-tags-into-1-tag-easily/98195/11 "2024-11-06T20:27:33Z")

</div>

[Posted a reference to this thread in this thread!]
