Comparison of msg.payloads from the dashboard

I am developing an app for Iot from node red.
In this app I need to compare a sensor value with maximum and minimum limits defined from the dashboard.

It would have to work as follows: on the dashboard I would define the maximum and minimum values ​​of a sensor with a numerical node or something like that.

Example: maximum temperature: 30 ° C

minimum temperature: 10 ° C

The idea is to compare the input value of the sensor with the maximum and minimum values ​​defined in the dashboard.
If, using the previous example, the temperature is above 30 ° C and below 10 ° C a notification is sent.

I tried to use function-type nodes but he is unable to "store" the values ​​that are on the dashboard.

Would there be any way to do what I want? Is there a node that performs the operation in a simple way?

Thanks for the help.

You can use a ui_form or ui_text+ui_button to enter values.

Either way, use a change function to set flow.min and flow.max

flow1 ui_text --> change node (set flow.max to msg.payload)
flow2 ui_text --> change node (set flow.min to msg.payload)
flow3 sensor input --> function node --> notification

the function would be something like...

const absMax = 99; //hard coded safe guard
const absMin = 10; //hard coded safe guard
msg.max = flow.get("min") || absMax;
msg.min = flow.get("max") || absMin;
msg.inputValue = msg.payload; //keep a record of original payload - good for debugging!

if(msg.inputValue >= msg.max){
  msg.payload = "Too high";
else if(msg.inputValue < msg.min){
  msg.payload = "Too low";
} else {
  return null; //dont fire function node output!
return msg;
1 Like

Alternatively you can use a Join node to combine the values into one message (that is my preferred solution, but others prefer using the context). See this example from the cookbook for how that can be done.


I still have problems.

It seems that when it sends the values ​​to the node it does not store in the constants. it is as if the reference value were the values ​​set in the function (10 and 99). They don't update. Only something above 99 and below 10 is returned, it does not take into account what I mentioned in the dashboard.

The nodes are not configured correctly.

I am a beginner, if possible I would not have a more detailed example of the step by step, such as the ui_text configuration?

Export you flow Select nodes, press CTRL+E, copy then ...

paste between backticks like this

and I'll see whats wrong.

A screenshot would be good too (you can copy + paste an image straight into the reply on this forum)

1 Like

here is the flow:

[{"id":"6df7d1a7.4cd9c","type":"function","z":"cfbb6e4f.6526a","name":"","func":"const absMax = 99; //hard coded safe guard\nconst absMin = 10; //hard coded safe guard\nmsg.max = flow.get(\"min\") || absMax;\nmsg.min = flow.get(\"max\") || absMin;\nmsg.inputValue = msg.payload; //keep a record of original payload - good for debugging!\n\nif(msg.inputValue >= msg.max){\n msg.payload = \"Too high\";}\nelse if(msg.inputValue < msg.min){\n msg.payload = \"Too low\";\n} else {\n return null; //dont fire function node output!\n}\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":500,"wires":[["6dabc296.3525bc"]]},{"id":"6dabc296.3525bc","type":"debug","z":"cfbb6e4f.6526a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":670,"y":500,"wires":[]},{"id":"e93936eb.80cb48","type":"inject","z":"cfbb6e4f.6526a","name":"","topic":"inputValue","payload":"5","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":440,"wires":[["6df7d1a7.4cd9c"]]},{"id":"5b1d03a.6b0fafc","type":"inject","z":"cfbb6e4f.6526a","name":"","topic":"inputValue","payload":"50","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":560,"wires":[["6df7d1a7.4cd9c"]]},{"id":"1e758b47.efc845","type":"ui_form","z":"cfbb6e4f.6526a","name":"","label":"","group":"91cc4ff3.10673","order":2,"width":0,"height":0,"options":[{"label":"Maximum","value":"flow.max","type":"text","required":true,"rows":null},{"label":"Minimum","value":"flow.min","type":"text","required":false,"rows":null}],"formValue":{"flow.max":"","flow.min":""},"payload":"","submit":"submit","cancel":"cancel","topic":"","x":250,"y":500,"wires":[["6df7d1a7.4cd9c"]]},{"id":"91cc4ff3.10673","type":"ui_group","z":"","name":"Botão Relé 1","tab":"8f89f7a7.c74cf8","disp":true,"width":"6","collapse":false},{"id":"8f89f7a7.c74cf8","type":"ui_tab","z":"","name":"Relés","icon":"dashboard","disabled":false,"hidden":false}]

The structure you have shown is what you would do if you were using a Join node to join all the messages before testing in a function node. Using the flow context you should not feed the form into the function node but instead into a Change node that sets the flow variables.

1 Like

I managed to use the join node with an entry corresponding to the maximum (msg.max), one corresponding to the minimum (msg.min) and one corresponding to the sensor value. Right after a function node comparing the variables. If the temperatures are higher or lower than those defined, a notification is returned. Otherwise, the return is null.

Everything is working as desired. I thank everyone who helped.

the flow is attached. I intend to replace the inject with a mqtt_in node and debug for the notification.
[{"id":"97c9094b.030858","type":"debug","z":"cfbb6e4f.6526a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":640,"wires":[]},{"id":"35d180ea.771eb","type":"function","z":"cfbb6e4f.6526a","name":"","func":"const a = msg.payload.max;\nconst b = msg.payload.min;\n\nif(msg.payload.inputValue > a){\n msg.payload = \"Too high\";}\nelse if(msg.payload.inputValue < b){\n msg.payload = \"Too low\";\n} else {\n return null; //dont fire function node output!\n}\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":640,"wires":[["97c9094b.030858"]]},{"id":"531e311f.2b648","type":"join","z":"cfbb6e4f.6526a","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":350,"y":640,"wires":[["35d180ea.771eb"]]},{"id":"4e185f5e.96629","type":"inject","z":"cfbb6e4f.6526a","name":"temperature","topic":"inputValue","payload":"8","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":700,"wires":[["531e311f.2b648"]]},{"id":"940181a8.f2327","type":"ui_numeric","z":"cfbb6e4f.6526a","name":"","label":"maximum","tooltip":"","group":"91cc4ff3.10673","order":2,"width":0,"height":0,"wrap":false,"passthru":true,"topic":"max","format":"{{value}}","min":0,"max":10,"step":1,"x":140,"y":580,"wires":[["531e311f.2b648"]]},{"id":"2fff4a91.ac2376","type":"ui_numeric","z":"cfbb6e4f.6526a","name":"","label":"minimum","tooltip":"","group":"91cc4ff3.10673","order":3,"width":0,"height":0,"wrap":false,"passthru":true,"topic":"min","format":"{{value}}","min":0,"max":10,"step":1,"x":140,"y":640,"wires":[["531e311f.2b648"]]},{"id":"91cc4ff3.10673","type":"ui_group","z":"","name":"Botão Relé 1","tab":"8f89f7a7.c74cf8","disp":true,"width":"6","collapse":false},{"id":"8f89f7a7.c74cf8","type":"ui_tab","z":"","name":"Relés","icon":"dashboard","disabled":false,"hidden":false}]

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