"Colour mapping" - help needed to create RGB value

Today's quest

I receive temperature values. 30 - 75 (example)

I want to display that with a RGB LED.

Say 30 - 36 --> blue. Green starts to creep in then.
36 - 40 --> Blue fades out and becomes green. Towards the 40, red starts to come in.
40 - 75 --> Green fades out and it becomes only red.

Ok, vague. (I'm good at that. :wink: )

But can it be done nicely?
Or do I have to generate the separate R, G, and B values and create the colour?

(
What is the command to map one range to another again?
So I want to map 1 - 32 against 0 - 256.
just for an example using easy fit numbers.
)

I have kind of got something working but it isn't exact.

External node required:
node-red-contrib-fan
(A favourite of mine sorry)

The problem is the green value gets too whacky in the value it returns at the higher end.

[{"id":"7d42fbb1.1a67d4","type":"inject","z":"2a10ff07.234f98","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2210,"wires":[["6d507de2.09cefc"]]},{"id":"e09b93f7.4c35e8","type":"inject","z":"2a10ff07.234f98","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2250,"wires":[["6d507de2.09cefc"]]},{"id":"63313404.6b44dc","type":"inject","z":"2a10ff07.234f98","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2290,"wires":[["6d507de2.09cefc"]]},{"id":"860e4268.4ced7","type":"inject","z":"2a10ff07.234f98","name":"","topic":"","payload":"3","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2330,"wires":[["6d507de2.09cefc"]]},{"id":"244b914a.205bfe","type":"inject","z":"2a10ff07.234f98","name":"","topic":"","payload":"4","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2370,"wires":[["6d507de2.09cefc"]]},{"id":"b5aee483.411978","type":"inject","z":"2a10ff07.234f98","name":"","topic":"","payload":"5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2410,"wires":[["6d507de2.09cefc"]]},{"id":"f60bd946.beeaf","type":"inject","z":"2a10ff07.234f98","name":"","topic":"","payload":"6","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2450,"wires":[["6d507de2.09cefc"]]},{"id":"1fc3325.0084a4e","type":"inject","z":"2a10ff07.234f98","name":"","topic":"","payload":"7","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2490,"wires":[["6d507de2.09cefc"]]},{"id":"68dcd7ee.9a4e5","type":"inject","z":"2a10ff07.234f98","name":"","topic":"","payload":"8","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2530,"wires":[["6d507de2.09cefc"]]},{"id":"5c5b5f69.c1b398","type":"range","z":"2a10ff07.234f98","minin":"3","maxin":"0","minout":"10","maxout":"255","action":"scale","round":true,"property":"payload","name":"","x":670,"y":2210,"wires":[["a0c8109c.bb6158"]]},{"id":"3dd0621d.7a9ca6","type":"range","z":"2a10ff07.234f98","minin":"2","maxin":"6","minout":"10","maxout":"255","action":"scale","round":true,"property":"payload","name":"","x":670,"y":2300,"wires":[["cba9bd62.c4ff1"]]},{"id":"113ab30f.dcb37d","type":"range","z":"2a10ff07.234f98","minin":"5","maxin":"8","minout":"10","maxout":"255","action":"scale","round":true,"property":"payload","name":"","x":670,"y":2380,"wires":[["a868e355.1e44d8"]]},{"id":"dfae8186.12062","type":"comment","z":"2a10ff07.234f98","name":"Blue","info":"","x":660,"y":2170,"wires":[]},{"id":"db1a6edb.d8282","type":"comment","z":"2a10ff07.234f98","name":"Green","info":"","x":660,"y":2260,"wires":[]},{"id":"cea068c5.8ecd3","type":"comment","z":"2a10ff07.234f98","name":"Red","info":"","x":660,"y":2340,"wires":[]},{"id":"37f77004.edfbb","type":"change","z":"2a10ff07.234f98","name":"Blue","rules":[{"t":"move","p":"payload","pt":"msg","to":"blue","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1120,"y":2210,"wires":[["c06f345c.78f248"]]},{"id":"c32946c3.21286","type":"change","z":"2a10ff07.234f98","name":"Green","rules":[{"t":"move","p":"payload","pt":"msg","to":"green","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1120,"y":2300,"wires":[["5f00fe75.35ce1"]]},{"id":"141f69ec.ab5136","type":"change","z":"2a10ff07.234f98","name":"Red","rules":[{"t":"move","p":"payload","pt":"msg","to":"red","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1120,"y":2380,"wires":[["d2727fb0.3f166"]]},{"id":"c06f345c.78f248","type":"debug","z":"2a10ff07.234f98","name":"Blue","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"blue","targetType":"msg","x":1230,"y":2170,"wires":[]},{"id":"5f00fe75.35ce1","type":"debug","z":"2a10ff07.234f98","name":"Green","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"green","targetType":"msg","x":1230,"y":2260,"wires":[]},{"id":"d2727fb0.3f166","type":"debug","z":"2a10ff07.234f98","name":"Red","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"red","targetType":"msg","x":1230,"y":2340,"wires":[]},{"id":"6d507de2.09cefc","type":"fan","z":"2a10ff07.234f98","name":"","x":480,"y":2210,"wires":[["5c5b5f69.c1b398","3dd0621d.7a9ca6","113ab30f.dcb37d"]]},{"id":"a0c8109c.bb6158","type":"switch","z":"2a10ff07.234f98","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":820,"y":2210,"wires":[["37f77004.edfbb"],["28facc23.30dfb4"]]},{"id":"cba9bd62.c4ff1","type":"switch","z":"2a10ff07.234f98","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":820,"y":2300,"wires":[["c32946c3.21286"],["5094de08.eec4d8"]]},{"id":"a868e355.1e44d8","type":"switch","z":"2a10ff07.234f98","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":820,"y":2380,"wires":[["141f69ec.ab5136"],["5f1d37b2.7677f8"]]},{"id":"28facc23.30dfb4","type":"change","z":"2a10ff07.234f98","name":"0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":950,"y":2240,"wires":[["37f77004.edfbb"]]},{"id":"5094de08.eec4d8","type":"change","z":"2a10ff07.234f98","name":"0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":950,"y":2320,"wires":[["c32946c3.21286"]]},{"id":"5f1d37b2.7677f8","type":"change","z":"2a10ff07.234f98","name":"0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":950,"y":2400,"wires":[["141f69ec.ab5136"]]}]

The Range node will do that. Possibly use three, one for each colour, with appropriate input ranges.

Thanks Colin.

What is now biting me is that it isn't that simple. :frowning: (When is it?)

Did you look at the flow?

I kind of cheated with the blue in that it was reversed with the value.

eg:
So: blue 0 - 5 would give an output from 255 - 0 (Darkening as the value went up)
red was simple

Ok, I'll start again:

Say the input range is 30 --> 75
blue is mapped from 31 -> 44 getting darker as the number gets bigger.
red is mapped from 60 -> 75 getting brighter as the number gets bigger.
(Easy.)

green is where it all fell apart.

It is mapped from 40 -> 64.

But it is a hump/bell curve and not like the other two.

So, starting at (say) 30: It would be blue.
As it rises, the blue would reduce and the green would start to increase.
Continuing, the blue would be 0 and green would be 255 at about.... 52 (?)
Then above 52 the green would then start to fall off again and the red would start to come into the picture.

The machine using it is now off-line.

But from the flow I posted, the switch nodes weren't helping and doing some strange things sometimes with the green part.

It worked better than the original bit where it was simply a monochrome LED who's intensity varied with the value.
Simple, but a bit non-intuitive.

But thanks for the reply.

Any thoughts on what / how I can get the green part working?

Here is the new flow I have which kinda works.
But it is a dog's breakfast in how it gets things done.

[{"id":"185e272a.6b65b9","type":"range","z":"3caae529.4226aa","minin":"36","maxin":"30","minout":"10","maxout":"255","action":"scale","round":true,"property":"payload","name":"","x":1410,"y":1470,"wires":[["69551e43.d2fb7"]]},{"id":"9eea9ef1.cda11","type":"range","z":"3caae529.4226aa","minin":"34","maxin":"38","minout":"10","maxout":"255","action":"scale","round":true,"property":"payload","name":"","x":1410,"y":1560,"wires":[["dd6d5602.21f6b8","9d15db32.4e73c"]]},{"id":"31e66410.fc74ac","type":"range","z":"3caae529.4226aa","minin":"42","maxin":"75","minout":"10","maxout":"255","action":"scale","round":true,"property":"payload","name":"","x":1410,"y":1720,"wires":[["179a95bb.552dd2"]]},{"id":"1b90acb2.1f6c83","type":"comment","z":"3caae529.4226aa","name":"Blue","info":"","x":1400,"y":1430,"wires":[]},{"id":"efca4e3b.a45318","type":"comment","z":"3caae529.4226aa","name":"Green","info":"","x":1400,"y":1520,"wires":[]},{"id":"3c0d35c8.69d892","type":"comment","z":"3caae529.4226aa","name":"Red","info":"","x":1400,"y":1680,"wires":[]},{"id":"af0be7a7.3ab1e","type":"change","z":"3caae529.4226aa","name":"Blue","rules":[{"t":"move","p":"payload","pt":"msg","to":"blue","tot":"msg"},{"t":"set","p":"BLUE","pt":"flow","to":"blue","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1860,"y":1470,"wires":[["fdc3827.f99c8","fc5f5776.558a1","75a4a4a2.d1f824"]]},{"id":"861661e9.5f0e88","type":"change","z":"3caae529.4226aa","name":"Green","rules":[{"t":"move","p":"payload","pt":"msg","to":"green","tot":"msg"},{"t":"set","p":"GREEN","pt":"flow","to":"green","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1860,"y":1560,"wires":[["6d8434cc.b1ec4c","fc5f5776.558a1"]]},{"id":"ab74ba2b.4db098","type":"change","z":"3caae529.4226aa","name":"Red","rules":[{"t":"move","p":"payload","pt":"msg","to":"red","tot":"msg"},{"t":"set","p":"RED","pt":"flow","to":"red","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1860,"y":1720,"wires":[["2eeb5093.0c644","fc5f5776.558a1"]]},{"id":"fdc3827.f99c8","type":"debug","z":"3caae529.4226aa","name":"Blue","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"blue","targetType":"msg","x":1970,"y":1430,"wires":[]},{"id":"6d8434cc.b1ec4c","type":"debug","z":"3caae529.4226aa","name":"Green","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"green","targetType":"msg","x":1970,"y":1520,"wires":[]},{"id":"2eeb5093.0c644","type":"debug","z":"3caae529.4226aa","name":"Red","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"red","targetType":"msg","x":1970,"y":1680,"wires":[]},{"id":"df0f54b4.0d9e58","type":"fan","z":"3caae529.4226aa","name":"","x":1000,"y":1470,"wires":[["185e272a.6b65b9","31e66410.fc74ac","e1514380.c0a83","c9de0db3.4da8e"]]},{"id":"69551e43.d2fb7","type":"switch","z":"3caae529.4226aa","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1560,"y":1470,"wires":[["af0be7a7.3ab1e"],["3be33a29.4220de"]]},{"id":"dd6d5602.21f6b8","type":"switch","z":"3caae529.4226aa","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"255","vt":"num"},{"t":"gte","v":"0","vt":"num"},{"t":"lt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":3,"x":1560,"y":1560,"wires":[["bfc67099.654048"],["861661e9.5f0e88"],["bfc67099.654048"]]},{"id":"179a95bb.552dd2","type":"switch","z":"3caae529.4226aa","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"255","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1560,"y":1720,"wires":[["ab74ba2b.4db098"],["6d6146f2.50f0d"]]},{"id":"3be33a29.4220de","type":"change","z":"3caae529.4226aa","name":"0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1690,"y":1500,"wires":[["af0be7a7.3ab1e"]]},{"id":"bfc67099.654048","type":"change","z":"3caae529.4226aa","name":"0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1690,"y":1580,"wires":[["861661e9.5f0e88"]]},{"id":"6d6146f2.50f0d","type":"change","z":"3caae529.4226aa","name":"0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1690,"y":1740,"wires":[["ab74ba2b.4db098"]]},{"id":"83eca68f.77d5c8","type":"range","z":"3caae529.4226aa","minin":"42","maxin":"37","minout":"10","maxout":"255","action":"scale","round":true,"property":"payload","name":"","x":1410,"y":1600,"wires":[["dd6d5602.21f6b8","9d15db32.4e73c"]]},{"id":"9d15db32.4e73c","type":"debug","z":"3caae529.4226aa","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1590,"y":1640,"wires":[]},{"id":"e1514380.c0a83","type":"switch","z":"3caae529.4226aa","name":"<= 4","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"4","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1070,"y":1570,"wires":[["9eea9ef1.cda11"],["eb99f373.4d4ac8"]]},{"id":"eb99f373.4d4ac8","type":"switch","z":"3caae529.4226aa","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"5","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1250,"y":1630,"wires":[["83eca68f.77d5c8"],["bfc67099.654048"]]},{"id":"c9de0db3.4da8e","type":"debug","z":"3caae529.4226aa","name":"INPUT","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1000,"y":1420,"wires":[]},{"id":"93b63ed3.cf2b7","type":"link out","z":"3caae529.4226aa","name":"","links":["edf7b494.05e39"],"x":2495,"y":1580,"wires":[]},{"id":"f045c230.d9b2f8","type":"template","z":"3caae529.4226aa","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{Temperature_LED}},{{red}},{{green}},{{blue}}","output":"str","x":2370,"y":1580,"wires":[["f70a880e.1468a","93b63ed3.cf2b7"]]},{"id":"f70a880e.1468a","type":"debug","z":"3caae529.4226aa","name":"RGB","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":2490,"y":1400,"wires":[]},{"id":"7ef4c417.22ac3c","type":"debug","z":"3caae529.4226aa","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2250,"y":1440,"wires":[]},{"id":"9626dd05.4b254","type":"function","z":"3caae529.4226aa","name":"","func":"//node.warn(msg);\n\n//if (msg.red !== null)\n//{\n//    context.set(\"RED\",msg.red);\n//    node.warn(\"Red set\");\n//} else\n//if (msg.green !== null)\n//{\n//    context.set(\"GREEN\",msg.green);\n//    node.warn(\"Green set\");\n//} else\n//if (msg.blue !== null)\n//{\n//    context.set(\"BLUE\",msg.blue);\n//    node.warn(\"Blue set\");\n//}\nif (flow.get(\"RED\") !== null && flow.get(\"GREEN\") !== null && flow.get(\"BLUE\") !== null)\n{\n    msg = {\n        red:flow.get(\"RED\"),\n        green:flow.get(\"GREEN\"),\n        blue:flow.get(\"BLUE\"),\n        Temperature_LED:flow.get(\"Temperature_LED\")\n    }\n    flow.set(\"RED\",null);\n    flow.set(\"GREEN\",null);\n    flow.set(\"BLUE\",null);\n    return msg;\n}\n","outputs":1,"noerr":0,"x":2220,"y":1580,"wires":[["7ef4c417.22ac3c","f045c230.d9b2f8"]]},{"id":"75a4a4a2.d1f824","type":"delay","z":"3caae529.4226aa","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2050,"y":1580,"wires":[["9626dd05.4b254"]]}]

If you want one of them to go up then down again then for that colour you could use a Switch node to split the input above or below 52 to two outputs. Then use a Range node in each output configured to convert the ranges 40:52 to 0:255 and 52:64 to 255:0, or whatever the numbers are. Make sure you set all the Range nodes to Scale and Limit mode so they will clamp at 0:255.

This is the flow as I have it.

Input is the left most link node.

output is right most link node.

It uses flow context to store the RGB values.

External nodes needed:
node-red-contrib-fan
node-red-contrib-simple-gate

The other two link nodes with no names are used to control the gate node and a test input to inject values for testing.

[{"id":"83eca68f.77d5c8","type":"range","z":"3caae529.4226aa","minin":"48","maxin":"39","minout":"10","maxout":"200","action":"scale","round":true,"property":"payload","name":"48 - 39","x":1490,"y":1600,"wires":[["dd6d5602.21f6b8","9d15db32.4e73c"]]},{"id":"dd6d5602.21f6b8","type":"switch","z":"3caae529.4226aa","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"255","vt":"num"},{"t":"gte","v":"0","vt":"num"},{"t":"lt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":3,"x":1640,"y":1560,"wires":[["bfc67099.654048"],["861661e9.5f0e88"],["bfc67099.654048"]]},{"id":"9d15db32.4e73c","type":"debug","z":"3caae529.4226aa","name":"green","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1650,"y":1640,"wires":[]},{"id":"eb99f373.4d4ac8","type":"switch","z":"3caae529.4226aa","name":"<= 48","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"48","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1280,"y":1600,"wires":[["83eca68f.77d5c8"],["bfc67099.654048"]]},{"id":"9eea9ef1.cda11","type":"range","z":"3caae529.4226aa","minin":"32","maxin":"38","minout":"10","maxout":"200","action":"scale","round":true,"property":"payload","name":"34 - 38","x":1490,"y":1560,"wires":[["dd6d5602.21f6b8","9d15db32.4e73c"]]},{"id":"bfc67099.654048","type":"change","z":"3caae529.4226aa","name":"0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1770,"y":1580,"wires":[["861661e9.5f0e88"]]},{"id":"861661e9.5f0e88","type":"change","z":"3caae529.4226aa","name":"Green","rules":[{"t":"move","p":"payload","pt":"msg","to":"green","tot":"msg"},{"t":"set","p":"GREEN","pt":"flow","to":"green","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1940,"y":1560,"wires":[["6d8434cc.b1ec4c","fc5f5776.558a1"]]},{"id":"e1514380.c0a83","type":"switch","z":"3caae529.4226aa","name":"<= 38","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"38","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1140,"y":1560,"wires":[["9eea9ef1.cda11"],["eb99f373.4d4ac8"]]},{"id":"6d8434cc.b1ec4c","type":"debug","z":"3caae529.4226aa","name":"Green","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"green","targetType":"msg","x":2050,"y":1520,"wires":[]},{"id":"fc5f5776.558a1","type":"join","z":"3caae529.4226aa","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":2070,"y":1350,"wires":[[]]},{"id":"df0f54b4.0d9e58","type":"fan","z":"3caae529.4226aa","name":"","x":1000,"y":1470,"wires":[["185e272a.6b65b9","e1514380.c0a83","c9de0db3.4da8e","167b9ab5.e1a6dd"]]},{"id":"af0be7a7.3ab1e","type":"change","z":"3caae529.4226aa","name":"Blue","rules":[{"t":"move","p":"payload","pt":"msg","to":"blue","tot":"msg"},{"t":"set","p":"BLUE","pt":"flow","to":"blue","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1940,"y":1470,"wires":[["fdc3827.f99c8","fc5f5776.558a1","75a4a4a2.d1f824"]]},{"id":"ab74ba2b.4db098","type":"change","z":"3caae529.4226aa","name":"Red","rules":[{"t":"move","p":"payload","pt":"msg","to":"red","tot":"msg"},{"t":"set","p":"RED","pt":"flow","to":"red","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1940,"y":1720,"wires":[["2eeb5093.0c644","fc5f5776.558a1"]]},{"id":"185e272a.6b65b9","type":"range","z":"3caae529.4226aa","minin":"38","maxin":"30","minout":"30","maxout":"255","action":"scale","round":true,"property":"payload","name":"36 - 30","x":1490,"y":1470,"wires":[["69551e43.d2fb7","85accc97.edf778"]]},{"id":"c9de0db3.4da8e","type":"debug","z":"3caae529.4226aa","name":"INPUT","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1000,"y":1420,"wires":[]},{"id":"edf81695.bba0f","type":"gate","z":"3caae529.4226aa","name":"B","controlTopic":"CONTROL","defaultState":"open","openCmd":"STOP","closeCmd":"GO","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":870,"y":1470,"wires":[["df0f54b4.0d9e58"]]},{"id":"3dcd710f.3f182e","type":"link in","z":"3caae529.4226aa","name":"","links":["5339afc.acdd9d","6c7fc691.1f2648"],"x":885,"y":1420,"wires":[["df0f54b4.0d9e58"]]},{"id":"167b9ab5.e1a6dd","type":"switch","z":"3caae529.4226aa","name":"> 41","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"41","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1290,"y":1710,"wires":[["31e66410.fc74ac"],["31e66410.fc74ac"]]},{"id":"fdc3827.f99c8","type":"debug","z":"3caae529.4226aa","name":"Blue","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"blue","targetType":"msg","x":2050,"y":1430,"wires":[]},{"id":"69551e43.d2fb7","type":"switch","z":"3caae529.4226aa","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1640,"y":1470,"wires":[["af0be7a7.3ab1e"],["3be33a29.4220de"]]},{"id":"3be33a29.4220de","type":"change","z":"3caae529.4226aa","name":"0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1770,"y":1500,"wires":[["af0be7a7.3ab1e"]]},{"id":"75a4a4a2.d1f824","type":"delay","z":"3caae529.4226aa","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2110,"y":1470,"wires":[["9626dd05.4b254"]]},{"id":"2eeb5093.0c644","type":"debug","z":"3caae529.4226aa","name":"Red","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"red","targetType":"msg","x":2050,"y":1680,"wires":[]},{"id":"179a95bb.552dd2","type":"switch","z":"3caae529.4226aa","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"255","vt":"num"},{"t":"lt","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":1640,"y":1720,"wires":[["ab74ba2b.4db098"],["6d6146f2.50f0d"],["6d6146f2.50f0d"]]},{"id":"6d6146f2.50f0d","type":"change","z":"3caae529.4226aa","name":"0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1770,"y":1740,"wires":[["ab74ba2b.4db098"]]},{"id":"85accc97.edf778","type":"debug","z":"3caae529.4226aa","name":"blue","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1640,"y":1430,"wires":[]},{"id":"35fb326e.b2e8f6","type":"gate","z":"3caae529.4226aa","name":"B","controlTopic":"CONTROL","defaultState":"open","openCmd":"STOP","closeCmd":"GO","toggleCmd":"toggle","defaultCmd":"default","persist":true,"x":740,"y":1470,"wires":[["edf81695.bba0f"]]},{"id":"54da435d.ca930c","type":"inject","z":"3caae529.4226aa","name":"B","topic":"CONTROL","payload":"STOP","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1440,"y":1930,"wires":[["edf81695.bba0f"]]},{"id":"6e771919.9838e8","type":"inject","z":"3caae529.4226aa","name":"A","topic":"CONTROL","payload":"GO","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"1","x":1440,"y":1890,"wires":[["edf81695.bba0f"]]},{"id":"31e66410.fc74ac","type":"range","z":"3caae529.4226aa","minin":"40","maxin":"75","minout":"10","maxout":"255","action":"scale","round":true,"property":"payload","name":"40 - 75","x":1490,"y":1720,"wires":[["179a95bb.552dd2"]]},{"id":"9626dd05.4b254","type":"function","z":"3caae529.4226aa","name":"","func":"//node.warn(msg);\n\n//if (msg.red !== null)\n//{\n//    context.set(\"RED\",msg.red);\n//    node.warn(\"Red set\");\n//} else\n//if (msg.green !== null)\n//{\n//    context.set(\"GREEN\",msg.green);\n//    node.warn(\"Green set\");\n//} else\n//if (msg.blue !== null)\n//{\n//    context.set(\"BLUE\",msg.blue);\n//    node.warn(\"Blue set\");\n//}\nif (flow.get(\"RED\") !== null && flow.get(\"GREEN\") !== null && flow.get(\"BLUE\") !== null)\n{\n    msg = {\n        red:flow.get(\"RED\"),\n        green:flow.get(\"GREEN\"),\n        blue:flow.get(\"BLUE\"),\n        Temperature_LED:flow.get(\"Temperature_LED\")\n    }\n    flow.set(\"RED\",null);\n    flow.set(\"GREEN\",null);\n    flow.set(\"BLUE\",null);\n    return msg;\n}\n","outputs":1,"noerr":0,"x":2240,"y":1540,"wires":[["7ef4c417.22ac3c","f045c230.d9b2f8"]]},{"id":"7a42bcc1.3079bc","type":"link in","z":"3caae529.4226aa","name":"Temperature RX","links":["29785c87.2f304c"],"x":560,"y":1470,"wires":[["35fb326e.b2e8f6"]],"l":true},{"id":"2043a922.c8e486","type":"link in","z":"3caae529.4226aa","name":"","links":["50922805.64c078"],"x":615,"y":1420,"wires":[["35fb326e.b2e8f6"]]},{"id":"7ef4c417.22ac3c","type":"debug","z":"3caae529.4226aa","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2380,"y":1470,"wires":[]},{"id":"f045c230.d9b2f8","type":"template","z":"3caae529.4226aa","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{Temperature_LED}},{{red}},{{green}},{{blue}}","output":"str","x":2390,"y":1540,"wires":[["f70a880e.1468a","34a781d1.813346"]]},{"id":"f70a880e.1468a","type":"debug","z":"3caae529.4226aa","name":"RGB","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":2570,"y":1470,"wires":[]},{"id":"34a781d1.813346","type":"gate","z":"3caae529.4226aa","name":"B","controlTopic":"CONTROL","defaultState":"open","openCmd":"STOP","closeCmd":"GO","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":2570,"y":1540,"wires":[["93b63ed3.cf2b7"]]},{"id":"7f455dc3.82d3e4","type":"inject","z":"3caae529.4226aa","name":"B","topic":"CONTROL","payload":"STOP","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":2400,"y":1630,"wires":[["34a781d1.813346"]]},{"id":"628200d4.4588a8","type":"inject","z":"3caae529.4226aa","name":"A","topic":"CONTROL","payload":"GO","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"1","x":2400,"y":1590,"wires":[["34a781d1.813346"]]},{"id":"93b63ed3.cf2b7","type":"link out","z":"3caae529.4226aa","name":"","links":["edf7b494.05e39"],"x":2675,"y":1540,"wires":[]}]

Andrew, have a look at this post. In the colourise function is some code you can adapt to generate a heat colour.

What has all that got to do with the question here, which is how to map an input value of 30 to 75 to give you RGB values? All you need to test that is a slider (or inject nodes) and the logic to generate the colours.

Sorry, was that to me or Steve?
The reply is to me, so I am not sure.

It is to you, you posted a flow with an explanation of what it does, including using non-core nodes, but that doesn't seem to have anything to do with question you asked, which is how to map a number to RGB values.

I use non-core nodes because a: the allow me to isolate the real data from fake data I am injecting. and b) I like the fan node as it allows me to sectionalise parts of the code and allow me to move blocks easily.

I know the newer version allows something like that, but it is annoying if/when I have a node with (say) 10 inputs and it is the wrong kind.
I have to delete all the connections to it when I put in a new node and then wire all the inputs to it.
The fan node allows me to have the inputs all connected together and allow me to easily delete/replace the node.

We seem to be having a bit of misunderstanding.

Yes we do. The title of this thread is Colour mapping - help needed to create RGB value. In the first post you said you had a value in the range 30-75 and you want to map that to RGB values. The fan node and those before it and everything after the delay node appear to have nothing to do with that.
I believe you can simplify the bit in the middle dramatically if you set the range node to Scale and Limit mode, as I said earlier.
However, @Steve-Mcl's link looks as if it might give some better ideas.

I am really struggling to understand it.

I get the idea in a vague way, but seem to be failing getting the real mechanics nailed down.

I use switch nodes and range nodes to do things, but I seem to not be able to get things working as desired.

I have built several examples where I send sequential date (31 - 75 sequentially) to see how it works, but there are a lot of pot holes in it.

Thus I am asking for help.

Here is an adaption of that code.

As you didnt specify how you want RGB to be represented, I just spit out a whole bunch of different values (r,g,b,val,hex) in the payload object.

Ignore the pink image nodes at the bottom - they are to demonstrate it working.

[{"id":"e17b05ed.383be8","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"30","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":140,"wires":[["bfca8794.36a6e8"]]},{"id":"a9bd0b3b.93a0b8","type":"jimp-image","z":"f057b3e5.0b042","name":"","data":"payload","dataType":"msg","ret":"img","parameter1":"","parameter1Type":"msg","parameter2":"","parameter2Type":"msg","parameter3":"","parameter3Type":"msg","parameter4":"","parameter4Type":"msg","parameter5":"","parameter5Type":"msg","parameter6":"","parameter6Type":"msg","parameter7":"","parameter7Type":"msg","parameter8":"","parameter8Type":"msg","sendProperty":"image","sendPropertyType":"msg","parameterCount":0,"jimpFunction":"none","selectedJimpFunction":{"name":"none","fn":"none","description":"Just loads the image.","parameters":[]},"x":410,"y":340,"wires":[["e7b89dbf.316d7"]]},{"id":"e7b89dbf.316d7","type":"image viewer","z":"f057b3e5.0b042","name":"","width":"60","data":"image","dataType":"msg","x":530,"y":340,"wires":[[]]},{"id":"bfca8794.36a6e8","type":"change","z":"f057b3e5.0b042","name":"set min30 max75","rules":[{"t":"set","p":"min","pt":"msg","to":"30","tot":"num"},{"t":"set","p":"max","pt":"msg","to":"75","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":200,"wires":[["177d1506.6c226b"]]},{"id":"177d1506.6c226b","type":"function","z":"f057b3e5.0b042","name":"colorise","func":"// debugger\nvar buf = Buffer.from([0,0,0,255]);\nconst minimum = msg.min; //-10w0.0;\nconst maximum = msg.max; //400.0;\nconst val = msg.payload;\n\nmsg.payload = celcius2colours(val);\n\nreturn msg;\n\nfunction celcius2colours(value) {\n    var ratio = 2 * (value-minimum) / (maximum - minimum);\n    b = parseInt(Math.min(Math.max(0, 255*(1 - ratio)),255));\n    r = parseInt(Math.min(Math.max(0, 255*(ratio - 1)),255));\n    g = 255 - b - r;\n    let val = rgbaToInt(r,g,b,255);\n    let hex = val.toString(16);\n    let pl = {\n        r: r, g: g, b: b,\n        val: val,\n        hex: hex,\n        hex2: \"#\" + hex\n    }\n    return pl;\n}\n\n\nfunction rgbaToInt(red,green,blue,alpha){\n    \n    var r = red & 0xFF;\n    var g = green & 0xFF;\n    var b = blue & 0xFF;\n    var a = alpha & 0xFF;\n    \n    buf[0] = r;\n    buf[1] = g;\n    buf[2] = b;\n    return buf.readUInt32BE(0);\n    //return (r << 16) + (g << 8) + (b);\n    //return (r << 24 >>> 0) | (g << 16) | (b << 8) | (a);\n}\n\n\n\n\n\n\n","outputs":1,"noerr":0,"x":540,"y":200,"wires":[["e186474b.006938","f2a80a92.dce3d8"]]},{"id":"f5cf600c.fbc6c","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"40","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":180,"wires":[["bfca8794.36a6e8"]]},{"id":"e186474b.006938","type":"function","z":"f057b3e5.0b042","name":"make image with color","func":"\n\nconst colour = msg.payload.val;\nmsg.payload = {\"w\":8,\"h\":8,\"background\":colour}\nreturn msg;\n\n\n\n\n\n\n\n","outputs":1,"noerr":0,"x":440,"y":300,"wires":[["a9bd0b3b.93a0b8","b0784059.824d7"]]},{"id":"a4f2651f.394388","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"50","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":220,"wires":[["bfca8794.36a6e8"]]},{"id":"3a476896.7570e8","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"60","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":260,"wires":[["bfca8794.36a6e8"]]},{"id":"dc2d6e74.eea54","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"70","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":300,"wires":[["bfca8794.36a6e8"]]},{"id":"f2a80a92.dce3d8","type":"debug","z":"f057b3e5.0b042","name":"payload with various colour values","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":800,"y":200,"wires":[]},{"id":"b0784059.824d7","type":"debug","z":"f057b3e5.0b042","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":670,"y":300,"wires":[]},{"id":"d2dc56f9.2db4b8","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"10","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":80,"wires":[["bfca8794.36a6e8"]]},{"id":"a28fbab0.7351e8","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":360,"wires":[["bfca8794.36a6e8"]]},{"id":"b00d8b01.f51fb8","type":"comment","z":"f057b3e5.0b042","name":"test data","info":"","x":180,"y":40,"wires":[]},{"id":"7ab74bb8.209b34","type":"comment","z":"f057b3e5.0b042","name":"for demo purposes only","info":"","x":440,"y":260,"wires":[]},{"id":"135c67c6.9c4508","type":"comment","z":"f057b3e5.0b042","name":"----------------- the important bit ----------------- ","info":"","x":440,"y":160,"wires":[]}]
1 Like

@Steve-Mcl

Thanks.

It looks nice and I just gave it a quick butcher's hook (Look)

Now I have to apply it to the real world and see how it goes.

But I think you have the maths part down pat.

Much appreciated.

I may have to play with it to tweak where/when/how the colours appear.

But that does seem to do what I want.

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