MIN/MAX from mqtt

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

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.

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?

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

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

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.

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

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

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

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]

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

There is a small typo in your max24 'change' node. It should read flow.max24 (not flow.min24)
max24

1 Like

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

The example flow I posted has some weaknesses:

  1. If Node-red is restarted/deployed, the stored max and min may be lost. As @smcgann99 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.

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