# Expression: Find Lowest Non-Zero Number?

**URL:** <https://forum.inductiveautomation.com/t/expression-find-lowest-non-zero-number/52496>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [October 29, 2021, 1:58pm UTC](https://forum.inductiveautomation.com/t/expression-find-lowest-non-zero-number/52496 "2021-10-29T13:58:06Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![downerczx](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/downerczx/32/11649_2.png) [@downerczx](https://forum.inductiveautomation.com/u/downerczx)\
**Post date:** [October 29, 2021, 1:58pm UTC](https://forum.inductiveautomation.com/t/expression-find-lowest-non-zero-number/52496/1 "2021-10-29T13:58:06Z")

</div>

Hello, I have a set of tags with values, [0,2,0,1].

I need to find the lowest non-zero number from my set in expression language. The min() expression always brings back the zeros.

Any ideas? Thank you.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [October 29, 2021, 2:00pm UTC](https://forum.inductiveautomation.com/t/expression-find-lowest-non-zero-number/52496/2 "2021-10-29T14:00:58Z")

</div>

Hmm… not that it’s any use to you now, but maybe a `filter` expression function that operates on lists would be a good idea…

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [October 29, 2021, 2:04pm UTC](https://forum.inductiveautomation.com/t/expression-find-lowest-non-zero-number/52496/3 "2021-10-29T14:04:03Z")

</div>

Anyway you probably have to do this in script and if you really need the resulting value in an expression for some reason invoke that function via `runScript`.

---

<div class="post-metadata">

**Author:** ![downerczx](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/downerczx/32/11649_2.png) [@downerczx](https://forum.inductiveautomation.com/u/downerczx)\
**Post date:** [October 29, 2021, 2:05pm UTC](https://forum.inductiveautomation.com/t/expression-find-lowest-non-zero-number/52496/4 "2021-10-29T14:05:52Z")

</div>

Thanks Kevin,

I think I’ve figured it out…

min(  
if({[.]tag1}=0,999,{[.]tag1}),  
if({[.]tag2}=0,999,{[.]tag2}),  
if({[.]tag3}=0,999,{[.]tag3}),  
if({[.]tag4}=0,999,{[.]tag4}),  
999)

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [October 29, 2021, 2:06pm UTC](https://forum.inductiveautomation.com/t/expression-find-lowest-non-zero-number/52496/5 "2021-10-29T14:06:57Z")

</div>

Ah, there you go 🙂

Maybe another cup of coffee will make me smarter.

---

<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:** [October 29, 2021, 2:37pm UTC](https://forum.inductiveautomation.com/t/expression-find-lowest-non-zero-number/52496/6 "2021-10-29T14:37:27Z")

</div>

```auto
min(
	max({[.]tag1}, 999),
	max({[.]tag2}, 999),
	max({[.]tag3}, 999),
	max({[.]tag4}, 999),
	999
)

```

Same effect, less duplicate tag paths (thus less chance for future errors in transcription).

---

<div class="post-metadata">

**Author:** ![downerczx](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/downerczx/32/11649_2.png) [@downerczx](https://forum.inductiveautomation.com/u/downerczx)\
**Post date:** [October 29, 2021, 5:21pm UTC](https://forum.inductiveautomation.com/t/expression-find-lowest-non-zero-number/52496/7 "2021-10-29T17:21:19Z")

</div>

Actually, I think that would stomp on the other numbers non zero numbers? anything \< 999 would always be 999.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [October 29, 2021, 5:45pm UTC](https://forum.inductiveautomation.com/t/expression-find-lowest-non-zero-number/52496/8 "2021-10-29T17:45:39Z")

</div>

Using the given [0,2,0,1]  
this

> [@paul-griffith](#):
>
> ```auto
> min(
> max({[.]tag1}, 999),
> max({[.]tag2}, 999),
> max({[.]tag3}, 999),
> max({[.]tag4}, 999),
> 999
> )
> 
> ```

becomes

```auto
min(
	max(0, 999),
	max(1, 999),
	max(0, 999),
	max(2, 999),
	999
)

```

which becomes

`min(999,999,999,999,999)`

I wish I was a little more fresh with my math because I feel like this could be done strictly using min/maxes, buts right now its not coming to mind. Doing it in an expression is a little tough, you will definitely need if statements. This might be one of those times where a `runScript()` is appropriate and do your calculation via scripting.

---

<div class="post-metadata">

**Author:** ![bmusson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmusson/32/87530_2.png) [@bmusson](https://forum.inductiveautomation.com/u/bmusson)\
**Post date:** [October 29, 2021, 5:54pm UTC](https://forum.inductiveautomation.com/t/expression-find-lowest-non-zero-number/52496/9 "2021-10-29T17:54:22Z")

</div>

> [@downerczx](#):
>
> non-zero number

```auto
min(
	if({Tag1}, {Tag1}, None),
	if({Tag2}, {Tag2}, None),
	if({Tag3}, {Tag3}, None),
	if({Tag4}, {Tag4}, None)
)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 29, 2021, 5:54pm UTC](https://forum.inductiveautomation.com/t/expression-find-lowest-non-zero-number/52496/10 "2021-10-29T17:54:36Z")

</div>

Lord, these are all ugly. I would definitely use `objectScript()`, like so:

```auto
objectScript("min([999]+[x for x in args if x])", {tag1}, {tag2}, {tag3}, {tag4})

```

{ Using my free [Simulation Aids](https://inductiveautomation.com/moduleshowcase/module/automation-professionals-llc-simulation-aids) module, naturally. }

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [October 29, 2021, 6:20pm UTC](https://forum.inductiveautomation.com/t/expression-find-lowest-non-zero-number/52496/11 "2021-10-29T18:20:44Z")

</div>

Just noting a lot of these solutions, for a set of [1, -2, -3, -4] would return -4, not 1 - you definitely just want a Non-Zero min, so every other number positive and negative is fair game? Or you want the minimum of non-negative values?

Clarifying because getting a lowest non-negative number is a very common case, but getting a minimum by excluding one specific number from the entire number line, does not seem as common.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 29, 2021, 6:30pm UTC](https://forum.inductiveautomation.com/t/expression-find-lowest-non-zero-number/52496/12 "2021-10-29T18:30:01Z")

</div>

> [@bkarabinchak.psi](#):
>
> Or you want the minimum of non-negative values?

Positive values, presumably, as zero is explicitly excluded. My solution becomes:

```python
objectScript("min([999]+[x for x in args if x>0])", {tag1}, {tag2}, {tag3}, {tag4})

```

---

<div class="post-metadata">

**Author:** ![downerczx](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/downerczx/32/11649_2.png) [@downerczx](https://forum.inductiveautomation.com/u/downerczx)\
**Post date:** [October 29, 2021, 6:33pm UTC](https://forum.inductiveautomation.com/t/expression-find-lowest-non-zero-number/52496/13 "2021-10-29T18:33:31Z")

</div>

In my case, yes, non-negative non-zero #'s, also in my use case there won’t ever be negatives.
