# Select minimum of two values

**URL:** <https://discourse.nodered.org/t/select-minimum-of-two-values/44775>\
**Category:** General\
**Created:** [26 April 2021 18:26 UTC](https://discourse.nodered.org/t/select-minimum-of-two-values/44775 "2021-04-26T18:26:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gazzat5](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gazzat5/32/36097_2.png) [@gazzat5](https://discourse.nodered.org/u/gazzat5)\
**Post date:** [26 April 2021 18:26 UTC](https://discourse.nodered.org/t/select-minimum-of-two-values/44775/1 "2021-04-26T18:26:21Z")

</div>

Hi there,

I am receiving two messages from mqtt, what I want to do is select the lesser of the two and output it to influxdb.

I'm guessing I need to use a function but am unsure how to address two incoming message payloads inside the function.

Can anyone please point me in the right direction?

TIA

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [26 April 2021 19:11 UTC](https://discourse.nodered.org/t/select-minimum-of-two-values/44775/2 "2021-04-26T19:11:35Z")

</div>

You don't really need to write a function, you can use 2 standard nodes;

1. join node - to combine both messages into an array
2. change node with a jsonata expression to select the lowest number in the array

![largest](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e701a325482abdc53c70f19d54b46da323adb7e0.jpeg)

```auto
[{"id":"9b80fac1.4d62e8","type":"join","z":"dbe9689e.fc98f8","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":295,"y":790,"wires":[["9d32943.7172068"]]},{"id":"f53d49.5158e2b8","type":"inject","z":"dbe9689e.fc98f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","x":145,"y":765,"wires":[["9b80fac1.4d62e8"]]},{"id":"b4903047.af082","type":"inject","z":"dbe9689e.fc98f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":142,"y":811,"wires":[["9b80fac1.4d62e8"]]},{"id":"9d32943.7172068","type":"change","z":"dbe9689e.fc98f8","name":"change","rules":[{"t":"set","p":"payload","pt":"msg","to":"$min(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":435,"y":790,"wires":[["c2d9cbe7.35c678"]]},{"id":"c2d9cbe7.35c678","type":"debug","z":"dbe9689e.fc98f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":790,"wires":[]}]

```

EDIT - flow updated to include `$min(payload)` as @E1cid's better suggestion below.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 April 2021 20:22 UTC](https://discourse.nodered.org/t/select-minimum-of-two-values/44775/3 "2021-04-26T20:22:43Z")

</div>

Or `$min(payload)`

---

<div class="post-metadata">

**Author:** ![gazzat5](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gazzat5/32/36097_2.png) [@gazzat5](https://discourse.nodered.org/u/gazzat5)\
**Post date:** [27 April 2021 20:19 UTC](https://discourse.nodered.org/t/select-minimum-of-two-values/44775/4 "2021-04-27T20:19:47Z")

</div>

Okay that looks good thanks.

Though both my inputs are mqtt....will it still work if the messages come in at different times or is there a way to wait until both input have values (and then presumably clear them after)

The messages arrive roughly every one minute, with about 30 seconds between them:

 ![outtemp_graph](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/0/70d9210467bc3ef0e55d16791b815d587c0ac709.png)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [27 April 2021 22:02 UTC](https://discourse.nodered.org/t/select-minimum-of-two-values/44775/5 "2021-04-27T22:02:22Z")

</div>

> [@gazzat5](#):
>
> will it still work if the messages come in at different times

Have you tried the flow I posted?

---

<div class="post-metadata">

**Author:** ![gazzat5](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gazzat5/32/36097_2.png) [@gazzat5](https://discourse.nodered.org/u/gazzat5)\
**Post date:** [28 April 2021 07:38 UTC](https://discourse.nodered.org/t/select-minimum-of-two-values/44775/6 "2021-04-28T07:38:22Z")

</div>

Hi yes i've tested it. (I had to add a parsefloat conversion function from the mqtt input). The output seems to just be an integer (the decimals are lost) and it does not appear to pick between the two incoming data points, i believe due to the issue mentioned above, about the inputs coming at offset times.

see resulting graphs below:

 ![minouttemps](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/0407e5ec2013667ca3ad469b50c3a06860686564.png)

any other suggestions?  
THanks for your help so far 🙂

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [28 April 2021 07:50 UTC](https://discourse.nodered.org/t/select-minimum-of-two-values/44775/7 "2021-04-28T07:50:22Z")

</div>

Try joining as key value object using the topic as the key, Then it should wait till both values have changed  
e.g.

```auto
[{"id":"f53d49.5158e2b8","type":"inject","z":"9b3f9f31.c45298","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"one","payload":"2","payloadType":"num","x":100,"y":855,"wires":[["9b80fac1.4d62e8"]]},{"id":"9b80fac1.4d62e8","type":"join","z":"9b3f9f31.c45298","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":250,"y":880,"wires":[["9d32943.7172068"]]},{"id":"9d32943.7172068","type":"change","z":"9b3f9f31.c45298","name":"change","rules":[{"t":"set","p":"payload","pt":"msg","to":"$min(payload.[*])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":880,"wires":[["c2d9cbe7.35c678"]]},{"id":"b4903047.af082","type":"inject","z":"9b3f9f31.c45298","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"two","payload":"5","payloadType":"num","x":97,"y":901,"wires":[["9b80fac1.4d62e8"]]},{"id":"c2d9cbe7.35c678","type":"debug","z":"9b3f9f31.c45298","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":585,"y":880,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 June 2021 07:50 UTC](https://discourse.nodered.org/t/select-minimum-of-two-values/44775/8 "2021-06-27T07:50:42Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
