Select minimum of two values

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

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

[{"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.

1 Like

Or $min(payload)

1 Like

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:

Have you tried the flow I posted?

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:

any other suggestions?
THanks for your help so far :slight_smile:

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

[{"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":[]}]

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