# MIN/MAX from mqtt

**URL:** <https://discourse.nodered.org/t/min-max-from-mqtt/75832>\
**Category:** General\
**Created:** [27 February 2023 20:28 UTC](https://discourse.nodered.org/t/min-max-from-mqtt/75832 "2023-02-27T20:28:54Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mra2ko](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@mra2ko](https://discourse.nodered.org/u/mra2ko)\
**Post date:** [27 February 2023 20:28 UTC](https://discourse.nodered.org/t/min-max-from-mqtt/75832/1 "2023-02-27T20:28:54Z")

</div>

Hi, we have some sensors, whitch send data to mqtt. So and we like show min/max value after last 24h in LCD. Import from MQTT - make min and max values in last 24h and back send separately (2 values min and max) sent back to MQTT.  
Can you anybody help me make this please?  
Thank

---

<div class="post-metadata">

**Author:** ![znetsixe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/znetsixe/32/72588_2.png) [@znetsixe](https://discourse.nodered.org/u/znetsixe)\
**Post date:** [27 February 2023 20:34 UTC](https://discourse.nodered.org/t/min-max-from-mqtt/75832/2 "2023-02-27T20:34:49Z")

</div>

What have you tried ?

Use a function node:  
Use true statement together with a date check that keeps track of the hours and use an if statement that keeps track if lowest withing those 24 hours and highest.

Give it a go and ill look at it if you get stuck.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [27 February 2023 20:49 UTC](https://discourse.nodered.org/t/min-max-from-mqtt/75832/3 "2023-02-27T20:49:30Z")

</div>

> [@mra2ko](#):
>
> Hi, we have some sensors, which send data to mqtt.

What happens to this data which is "sent to mqtt"?  
Perhaps it is displayed on a dashboard?  
Does it then get discarded, or stored in a database of some sort?

> [@mra2ko](#):
>
> Import from MQTT - make min and max values in last 24h and back send separately (2 values min and max) sent back to MQTT

This doesn't make much sense to me. MQTT is a transport mechanism not a destination.  
Exactly how will you "import from MQTT"?

---

<div class="post-metadata">

**Author:** ![mra2ko](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@mra2ko](https://discourse.nodered.org/u/mra2ko)\
**Post date:** [27 February 2023 20:56 UTC](https://discourse.nodered.org/t/min-max-from-mqtt/75832/4 "2023-02-27T20:56:07Z")

</div>

Sensor send data over mqtt to mosqito, node read from this broker any value. This is how I sense the values ​​and display them on the LCD. But, I need show min and max value in last 24h, for example temperature... For example in schematic...

Temp senosr - mqtt to mosqito - node read mqtt read - any mechanism for MIN/MAX value in last 24h and back send to mqtt, because I am able read with LCD any value from mqtt...

Thank

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [27 February 2023 21:35 UTC](https://discourse.nodered.org/t/min-max-from-mqtt/75832/5 "2023-02-27T21:35:17Z")

</div>

Perhaps this will do. It saves the highest and lowest values seen in flow variables (which are visible only in this flow) and resets them at midnight.

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/6/c68556a2d99c0d91a28d2ce653150789020c5d51.jpeg)

```auto
[{"id":"43b59e5a75cfcb1b","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"2b8af0e167b6c4d0","type":"group","z":"43b59e5a75cfcb1b","name":"Sample data","style":{"label":true},"nodes":["a9d96f5472182af1","4a66ac7d8daef5cf","b7be208a679d0996","8e44c0113b078671"],"x":114,"y":139,"w":172,"h":202},{"id":"f01dfaed27da6ac3","type":"group","z":"43b59e5a75cfcb1b","name":"Store maximum and minimum seen","style":{"label":true},"nodes":["b027713894587173","75e3efd15c1a24eb","1838dbbceebef61f","839e1d1e7d4b2047"],"x":354,"y":179,"w":432,"h":142},{"id":"8963a0706ba051ed","type":"group","z":"43b59e5a75cfcb1b","name":"At midnight set improbably high min, improbably low max","style":{"label":true},"nodes":["175b0cadd28ebb5f","bc193267f4b84d3a"],"x":354,"y":79,"w":432,"h":82},{"id":"8402f324ba886442","type":"group","z":"43b59e5a75cfcb1b","name":"Show currently saved values","style":{"label":true},"nodes":["9010b509d463e109","59e936bf56aa415a","ce67862e6b56e82e","6069386959fe776f","3346625dde0ad8bc"],"x":354,"y":339,"w":512,"h":122},{"id":"175b0cadd28ebb5f","type":"inject","z":"43b59e5a75cfcb1b","g":"8963a0706ba051ed","name":"Midnight","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 00 * * *","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":460,"y":120,"wires":[["bc193267f4b84d3a"]]},{"id":"bc193267f4b84d3a","type":"change","z":"43b59e5a75cfcb1b","g":"8963a0706ba051ed","name":"Initialise max and min","rules":[{"t":"set","p":"max24","pt":"flow","to":"-9999","tot":"num"},{"t":"set","p":"min24","pt":"flow","to":"9999","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":120,"wires":[[]]},{"id":"b027713894587173","type":"switch","z":"43b59e5a75cfcb1b","g":"f01dfaed27da6ac3","name":"Data > stored max?","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"max24","vt":"flow"}],"checkall":"true","repair":false,"outputs":1,"x":470,"y":220,"wires":[["75e3efd15c1a24eb"]]},{"id":"75e3efd15c1a24eb","type":"change","z":"43b59e5a75cfcb1b","g":"f01dfaed27da6ac3","name":"","rules":[{"t":"set","p":"max24","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":220,"wires":[[]]},{"id":"1838dbbceebef61f","type":"switch","z":"43b59e5a75cfcb1b","g":"f01dfaed27da6ac3","name":"Data < stored min?","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"min24","vt":"flow"}],"checkall":"true","repair":false,"outputs":1,"x":470,"y":280,"wires":[["839e1d1e7d4b2047"]]},{"id":"839e1d1e7d4b2047","type":"change","z":"43b59e5a75cfcb1b","g":"f01dfaed27da6ac3","name":"","rules":[{"t":"set","p":"min24","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":280,"wires":[[]]},{"id":"a9d96f5472182af1","type":"inject","z":"43b59e5a75cfcb1b","g":"2b8af0e167b6c4d0","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"3","payloadType":"num","x":210,"y":220,"wires":[["b027713894587173","1838dbbceebef61f"]]},{"id":"4a66ac7d8daef5cf","type":"inject","z":"43b59e5a75cfcb1b","g":"2b8af0e167b6c4d0","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"24","payloadType":"num","x":210,"y":260,"wires":[["b027713894587173","1838dbbceebef61f"]]},{"id":"b7be208a679d0996","type":"inject","z":"43b59e5a75cfcb1b","g":"2b8af0e167b6c4d0","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"81","payloadType":"num","x":210,"y":300,"wires":[["b027713894587173","1838dbbceebef61f"]]},{"id":"8e44c0113b078671","type":"inject","z":"43b59e5a75cfcb1b","g":"2b8af0e167b6c4d0","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-100","payloadType":"num","x":210,"y":180,"wires":[["b027713894587173","1838dbbceebef61f"]]},{"id":"9010b509d463e109","type":"debug","z":"43b59e5a75cfcb1b","g":"8402f324ba886442","name":"min24","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":380,"wires":[]},{"id":"59e936bf56aa415a","type":"inject","z":"43b59e5a75cfcb1b","g":"8402f324ba886442","name":"Show stored values","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":490,"y":400,"wires":[["ce67862e6b56e82e","6069386959fe776f"]]},{"id":"ce67862e6b56e82e","type":"change","z":"43b59e5a75cfcb1b","g":"8402f324ba886442","name":"min24","rules":[{"t":"set","p":"payload","pt":"msg","to":"min24","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":380,"wires":[["9010b509d463e109"]]},{"id":"6069386959fe776f","type":"change","z":"43b59e5a75cfcb1b","g":"8402f324ba886442","name":"max24","rules":[{"t":"set","p":"payload","pt":"msg","to":"min24","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":420,"wires":[["3346625dde0ad8bc"]]},{"id":"3346625dde0ad8bc","type":"debug","z":"43b59e5a75cfcb1b","g":"8402f324ba886442","name":"max24","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":420,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![mra2ko](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@mra2ko](https://discourse.nodered.org/u/mra2ko)\
**Post date:** [28 February 2023 09:24 UTC](https://discourse.nodered.org/t/min-max-from-mqtt/75832/6 "2023-02-28T09:24:01Z")

</div>

Hi, thank! But something is wrong, min/max values are reset randomly, no in 0:00, any idea why ?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [28 February 2023 09:51 UTC](https://discourse.nodered.org/t/min-max-from-mqtt/75832/7 "2023-02-28T09:51:38Z")

</div>

> [@mra2ko](#):
>
> min/max values are reset randomly, not at 0:00

Reset as in reset to +/-999 unexpectedly, or reset as in they become null?

---

<div class="post-metadata">

**Author:** ![mra2ko](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@mra2ko](https://discourse.nodered.org/u/mra2ko)\
**Post date:** [28 February 2023 10:29 UTC](https://discourse.nodered.org/t/min-max-from-mqtt/75832/8 "2023-02-28T10:29:59Z")

</div>

I think randomly... I put over button value below zero, for example -2,1. Now is temperature +2. After some time the value of -2.1 is overwritten to some value in plus

[image]

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [28 February 2023 13:58 UTC](https://discourse.nodered.org/t/min-max-from-mqtt/75832/9 "2023-02-28T13:58:42Z")

</div>

Do you have your context stored to file ?  
If not values may be lost on NR restart / deploy

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [28 February 2023 16:14 UTC](https://discourse.nodered.org/t/min-max-from-mqtt/75832/10 "2023-02-28T16:14:11Z")

</div>

There is a small typo in your max24 'change' node. It should read flow.max24 (not flow.min24)  
 ![max24](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/6/6675b3f1b94327dded38322ad2462907ee4428ef.png)

---

<div class="post-metadata">

**Author:** ![mra2ko](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@mra2ko](https://discourse.nodered.org/u/mra2ko)\
**Post date:** [1 March 2023 09:24 UTC](https://discourse.nodered.org/t/min-max-from-mqtt/75832/11 "2023-03-01T09:24:17Z")

</div>

Huh, sry I dont know, what you mean. What is need change.. ?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 March 2023 09:55 UTC](https://discourse.nodered.org/t/min-max-from-mqtt/75832/12 "2023-03-01T09:55:24Z")

</div>

The example flow I posted has some weaknesses:

1. If Node-red is restarted/deployed, the stored max and min may be lost. As @Sean-McG says, you can fix this by using persistent storage (search the forum for more info)
2. If there is no stored max and min, the switch node comparisons against the current message will fail. That's why the midnight bit of the flow stores extreme values rather than deleting the context variables. Also why this inject is set to fire once at startup. But a more robust approach is to test if these flow variables are null every time new sensor data arrives.
3. As @dynamicdave says, there is a copy/paste error in one of the change nodes.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [1 March 2023 10:50 UTC](https://discourse.nodered.org/t/min-max-from-mqtt/75832/13 "2023-03-01T10:50:08Z")

</div>

If you look inside the 'change' node labelled 'max24' the 'to the value' box should read... flow.max24
