Scaling Data From a Table

I’m pulling two consecutive registers from a Modbus device and sending them to a PAC Write node. This works great. Now I want to scale those values, both of them with the same scale. I found this great node called Range which lets me do just that. However when I plug it in with more than one value for my table, I get no output. If I just pull a single value, it scales just fine. How do I scale more than one value in a table? I could just duplicate my setup which would work fine for a couple of table elements but what if i wanted to scale 100?

Here is what my flow looks like with unscaled data (see below);

That’s the fun of open-source, you can write your own new-and-improved “range” node that can handle an array!

In the meantime, you could use a little function block and write some JavaScript that’ll do it.

Or I think you could split the array, do a range node for each element, then join them back up before writing them into that SNAP PAC numeric table.

Unfortunately, I don’t have the expertise to write my own nodes. One of the supposed appeals of Node-RED was you didn’t have to write code. You could just string the nodes together and life would be good.

Anyway, I can’t get the splitting of the array to work. It splits the array but won’t write it back to the Groov correctly. I’ll give it a little more time before I give up and head back to Opto Script.

Thanks,

Check out the Split node - it should be able to split your array into multiple messages so you can process them with range one at a time (range will get called for each entry in the array). You can then use join if you want to put them all back into an array.

Philip and Mary are correct. Feed the array into the Split node, use Range to scale the values, and Join to put the values back into an array. Finally, send the array to your PAC.

Here’s an example using an Inject node with a JSON array payload of 6 integers, splitting, ranging to 0-100%, and finally joining them back into an array:

[{"id":"3d4f2829.b249f8","type":"inject","z":"a307d805.d13d58","name":"","topic":"","payload":"[8626,8516,7734,9178,10023,1234]","payloadType":"json","repeat":"","crontab":"","once":false,"x":150,"y":280,"wires":[["561855d4.dc126c","983c5995.7e6268"]]},{"id":"1d0a1b62.595df5","type":"range","z":"a307d805.d13d58","minin":"0","maxin":"10000","minout":"0","maxout":"100","action":"scale","round":false,"name":"Scale to 0-100%","x":570,"y":280,"wires":[["aaa03dfc.f0f3","ddbfa1f.3f12c6"]],"inputLabels":["testin"],"outputLabels":["testout"]},{"id":"1c3282e8.13c95d","type":"debug","z":"a307d805.d13d58","name":"","active":true,"console":"false","complete":"true","x":910,"y":400,"wires":[]},{"id":"983c5995.7e6268","type":"split","z":"a307d805.d13d58","name":"","splt":"\\n","spltType":"str","arraySplt":"1","arraySpltType":"len","stream":false,"addname":"","x":330,"y":280,"wires":[["57773b6c.2d2c24","1d0a1b62.595df5"]]},{"id":"ddbfa1f.3f12c6","type":"join","z":"a307d805.d13d58","name":"","mode":"auto","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"10","x":790,"y":280,"wires":[["1c3282e8.13c95d"]]},{"id":"561855d4.dc126c","type":"debug","z":"a307d805.d13d58","name":"","active":true,"console":"false","complete":"true","x":290,"y":400,"wires":[]},{"id":"57773b6c.2d2c24","type":"debug","z":"a307d805.d13d58","name":"","active":false,"console":"false","complete":"true","x":470,"y":400,"wires":[]},{"id":"aaa03dfc.f0f3","type":"debug","z":"a307d805.d13d58","name":"","active":false,"console":"false","complete":"true","x":730,"y":400,"wires":[]}]

Here’s an image of the flow:

Good luck! -Benson

Thanks guys, for all your help. This Node-RED stuff is starting to make sense.

1 Like