Need help with comparing RGB values

I am new to NodeRED. I am trying to create a very simple routine. I am injecting 2 types of RGB values: reading and set RGB values. I am trying to compare the reading RGB values to the actual/expected RGB values and get the difference between where I will then add the difference to the reading values and display the correction made.
For example: if set values are 250,251,252 and reading values are 240,245,242 then after comparing the difference would be 10,6,10 which then will add to the reading values to correct the RGB values to match the set values and display the correction made.
the reading values can be different numbers, but set values will stay the same (think of the set values as in if they are stored in database).

Here is my flow:

[{"id":"7d082d1a.9093f4","type":"inject","z":"350d0535.68069a","name":"Reading_Red ","props":[{"p":"red","v":"240","vt":"num"},{"p":"green","v":"235","vt":"num"},{"p":"blue","v":"245","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":40,"wires":[["cda5c480.719bd8"]]},{"id":"cda5c480.719bd8","type":"function","z":"350d0535.68069a","name":"Reading input ","func":"msg.red = {\"getRed\":msg.payload};\nmsg.green = {\"getGreen\":msg.payload};\nmsg.blue = {\"getBlue\":msg.payload};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":380,"y":40,"wires":[["e67c519.4adb1b"]]},{"id":"40865fae.d521d","type":"function","z":"350d0535.68069a","name":"Actual_RGB","func":"msg.red1 = {\"getRed1\":msg.payload};\nmsg.green1 = {\"getGreen1\":msg.payload};\nmsg.blue1 = {\"getBlue1\":msg.payload};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":390,"y":120,"wires":[["e67c519.4adb1b"]]},{"id":"9372e14b.293ac","type":"inject","z":"350d0535.68069a","name":"Expected_Values","props":[{"p":"red1","v":"250","vt":"num"},{"p":"green1","v":"251","vt":"num"},{"p":"blue1","v":"252","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":220,"y":120,"wires":[["40865fae.d521d"]]},{"id":"2e7f2f7b.0712a","type":"function","z":"350d0535.68069a","name":"Comparison ","func":"var rred = context.get(\"rred\");\nvar red_1 = context.get(\"red_1\");\nvar ggreen = context.get(\"ggreen\");\nvar green_1 = context.get(\"green_1\");\nvar bblue = context.get(\"bblue\");\nvar blue_1 = context.get(\"blue_1\");\nvar correction = 0;\n\n// msg.red get the red value then \nif (msg.red(\"getRed\")){\n    // rred will be equla to the getRed value\n    rred = msg.red.getRed;\n    // and it will set the rred to that value\n    context.set(\"rred\", rred);\n}\nif (msg.green(\"getGreen\")){\n    ggreen = msg.green.getGreen;\n    context.set(\"ggreen\", ggreen);\n}\nif (msg.blue(\"getBlue\")){\n    bblue = msg.blue.getBlue;\n    context.set(\"bblue\", bblue);\n}\n// for the expected value\nif (msg.red1(\"getRed1\")){\n    // red_1 will be equal to the getRed1 value\n    red_1 = msg.red1.getRed1;\n    // and it will set the red_1 to that value\n    context.set(\"red_1\", red_1);\n}\nif (msg.green1(\"getGreen1\")){\n    green_1 = msg.green1.getGreen1;\n    context.set(\"green_1\", green_1);\n}\nif (msg.blue1(\"getBlue1\")){\n    blue_1 = msg.blue1.getBlue1;\n    context.set(\"blue_1\", blue_1);\n}\n\n// if else to compare\n\nif (rred < red_1){\n    correction = getRed1 - getRed;\n    msg.red = correction + getRed;\n    return msg;\n}\n\nif (ggreen < green_1){\n    correction = getGreen1 - getGreen;\n    msg.green = correction + getGreen;\n    return msg;\n}\nif (bblue < blue_1){\n    correction = geBlue1 - getBlue;\n    msg.blue = correction + getBlue;\n    return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":730,"y":80,"wires":[["68033f6b.5746"]]},{"id":"68033f6b.5746","type":"debug","z":"350d0535.68069a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":870,"y":80,"wires":[ ]},{"id":"e67c519.4adb1b","type":"split","z":"350d0535.68069a","name":"","splt":",","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":530,"y":80,"wires":[["2e7f2f7b.0712a"]]}]

Any help would be appreciated because I am super confused.

Hi, in order to make code more readable and importable it is important to surround your code with three backticks
```
like this
```

You can edit and correct your post by clicking the pencil icon.

See this post for more details - How to share code or flow json

1 Like

I think you might be wise to spend a bit time learning about node-red before trying to tackle this.

Canned text : I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

some clues however...

1 Like

Here is something to get you on the right track (but please watch that playlist from my earlier post)...

Results from the function...
image

NOTE: I am not certain what you are trying to actually achieve in the function so it wont be 100% correct - you will need to adjust the equation in the function node to suit your needs

The flow...

[{"id":"2f0ecf99.f070f","type":"function","z":"c40b7683.6e65e8","name":"Comparison - outputs msg.adjusted","func":"msg.adjusted = {\n    red: msg.payload.red,\n    green: msg.payload.green,\n    blue: msg.payload.blue,\n};\n\nif (msg.payload.red < msg.expected.red){\n    correction = msg.expected.red - msg.payload.red;\n    msg.adjusted.red = correction + msg.expected.red;\n}\n\nif (msg.payload.green < msg.expected.green){\n    correction = msg.expected.green - msg.payload.green;\n    msg.adjusted.green = correction + msg.expected.green;\n}\n\nif (msg.payload.blue < msg.expected.blue){\n    correction = msg.expected.blue - msg.payload.blue;\n    msg.adjusted.blue = correction + msg.expected.blue;\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":780,"y":380,"wires":[["8b0ae2c3.308c4"]]},{"id":"8b0ae2c3.308c4","type":"debug","z":"c40b7683.6e65e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":440,"wires":[]},{"id":"871f53a8.d658c","type":"inject","z":"c40b7683.6e65e8","name":"Expected_Values 120 130 140","props":[{"p":"payload.red","v":"120","vt":"num"},{"p":"payload.green","v":"130","vt":"num"},{"p":"payload.blue","v":"140","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":220,"y":160,"wires":[["44c86769.d5c988","b7b148c8.40b7b8"]]},{"id":"44c86769.d5c988","type":"change","z":"c40b7683.6e65e8","name":"Store expected RGB in flow context","rules":[{"t":"set","p":"expected","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":160,"wires":[[]]},{"id":"c6844ac6.6b3af8","type":"inject","z":"c40b7683.6e65e8","name":"Test Values 240, 255, 100","props":[{"p":"payload.red","v":"240","vt":"num"},{"p":"payload.green","v":"255","vt":"num"},{"p":"payload.blue","v":"100","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":360,"wires":[["af4480f3.8cdb2"]]},{"id":"af4480f3.8cdb2","type":"change","z":"c40b7683.6e65e8","name":"pick up flow.expected","rules":[{"t":"set","p":"expected","pt":"msg","to":"expected","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":380,"wires":[["2f0ecf99.f070f","a95fe187.3e54d"]]},{"id":"f93c111c.6abae","type":"inject","z":"c40b7683.6e65e8","name":"Test Values 100, 150, 200","props":[{"p":"payload.red","v":"100","vt":"num"},{"p":"payload.green","v":"150","vt":"num"},{"p":"payload.blue","v":"200","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":400,"wires":[["af4480f3.8cdb2"]]},{"id":"a95fe187.3e54d","type":"debug","z":"c40b7683.6e65e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":440,"wires":[]},{"id":"b7b148c8.40b7b8","type":"debug","z":"c40b7683.6e65e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":450,"y":200,"wires":[]},{"id":"1249e0dc.9ff6ff","type":"comment","z":"c40b7683.6e65e8","name":"Store expected RGB values in flow.context","info":"","x":360,"y":120,"wires":[]},{"id":"16ba3964.5c4a07","type":"comment","z":"c40b7683.6e65e8","name":"grab RGB values from flow.context","info":"","x":520,"y":311,"wires":[]},{"id":"5522577.acfc4a8","type":"comment","z":"c40b7683.6e65e8","name":"compute differences between ","info":"","x":800,"y":312,"wires":[]},{"id":"63b7bcb.eddc144","type":"comment","z":"c40b7683.6e65e8","name":"Test data","info":"","x":160,"y":320,"wires":[]},{"id":"3ba5f504.4f194a","type":"comment","z":"c40b7683.6e65e8","name":"and add it to the msg.expected","info":"","x":510,"y":340,"wires":[]},{"id":"d7d963b8.78ec9","type":"comment","z":"c40b7683.6e65e8","name":"msg.payload.xxx  and  msg.expected.xxx","info":"","x":830,"y":340,"wires":[]}]
1 Like

Thank you so much I will definitely go and watch the video because I will be using nodered for the next few months for sure and I was jus trying to understand by practically performing simple flows.

Thank you so much this makes so much more sense, I will go over it to understand and implement myself. Thank you so much.

Thank you, I did not no about this. I was a bit confused on how to make it look less bulky post. I will definitely keep this in mind for future reference.

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