Need help with a function

I'm trying to achieve something that I think can only be done by a function node.

The input will be a number which is the desired position on a scale.
The output needs to be a series of either true or false messages (true = increment, false = decrement) to reach the desired position on the scale.

So let's say it's a 0-10 scale and we start at 0. a 5 comes in which will need 5 true messages to get to 5, then that position needs to be persistent. The next number comes in - 2, which needs 3 decrement messages to drop down to 2 on the scale.

I hope this makes sense. any input is appreciated.

Perhaps there's an option to do it with some node combinations - yet I'd go as well the way you already indicated via a Function node.

Some hints:

Something like this...

chrome_r9nQx4KJxG

Demo flow (use CTRL+I to import)

[{"id":"2d0c1bfb3ab06fa2","type":"inject","z":"3b62e67b4dfcce5d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":1570,"y":160,"wires":[["99f1ce078c48901d"]]},{"id":"414d347bf295674d","type":"inject","z":"3b62e67b4dfcce5d","name":"","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"clear","payload":"0","payloadType":"num","x":1570,"y":120,"wires":[["99f1ce078c48901d"]]},{"id":"80887daa64a7361f","type":"inject","z":"3b62e67b4dfcce5d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","x":1570,"y":200,"wires":[["99f1ce078c48901d"]]},{"id":"aafb3422922422ec","type":"inject","z":"3b62e67b4dfcce5d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":1570,"y":240,"wires":[["99f1ce078c48901d"]]},{"id":"4bb1d40e658309c0","type":"inject","z":"3b62e67b4dfcce5d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":1570,"y":280,"wires":[["99f1ce078c48901d"]]},{"id":"bc029db47bfbdb6b","type":"function","z":"3b62e67b4dfcce5d","name":"pulse gen","func":"if(msg.topic === 'clear') {\n    context.set('position', 0)\n    node.status({ fill: \"gray\", shape: \"ring\", text: 0 });\n    return // stop msg flow\n}\nconst target = msg.payload\nconst position = context.get('position')\nconst direction = target > position\nconst dif = Math.abs(position - target)\nif (dif === 0) {\n    return null // no change\n}\ncontext.set('position', target)\nnode.status({ fill: \"yellow\", shape: \"ring\", text: target });\nmsg.payload = Array(dif).fill(direction)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1960,"y":120,"wires":[["9fa4bf8c34c55a29"]]},{"id":"99f1ce078c48901d","type":"switch","z":"3b62e67b4dfcce5d","name":"In range?","property":"payload","propertyType":"msg","rules":[{"t":"btwn","v":"0","vt":"num","v2":"9","v2t":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1780,"y":200,"wires":[["bc029db47bfbdb6b"],["419055dad640a91a"]]},{"id":"0bb9cfed62314c83","type":"debug","z":"3b62e67b4dfcce5d","name":"pulses","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2130,"y":180,"wires":[]},{"id":"419055dad640a91a","type":"debug","z":"3b62e67b4dfcce5d","name":"TARGET ERROR!","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1990,"y":240,"wires":[]},{"id":"9fa4bf8c34c55a29","type":"split","z":"3b62e67b4dfcce5d","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":2090,"y":120,"wires":[["c87e024f6d4b8a72"]]},{"id":"c87e024f6d4b8a72","type":"delay","z":"3b62e67b4dfcce5d","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"5","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1990,"y":180,"wires":[["0bb9cfed62314c83"]]}]

this works apart from the 10 outputs the value

Just change the limits in the "switch" node.

NOTE: you dont have to do anything with the 2nd output of the "switch node", it is there purely to filter out bad values.

spot on, thanks

Another solution, this one obviously will contain silicon Pixies, as they are off Xmas duties. It also forces out of range numbers to min and max (0, 10), it shloud also reject strings unless "0" - "10".

[{"id":"be451ec43efa387e","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":150,"y":1800,"wires":[["35ceb9c1e232bb1d"]]},{"id":"35ceb9c1e232bb1d","type":"range","z":"da8a6ef0b3c9a5c8","minin":"0","maxin":"10","minout":"0","maxout":"10","action":"clamp","round":true,"property":"payload","name":"","x":310,"y":1900,"wires":[["0dde7af39bb21306"]]},{"id":"e1e8d7cc1dc239af","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":150,"y":1920,"wires":[["35ceb9c1e232bb1d"]]},{"id":"f823969bdae166b3","type":"inject","z":"da8a6ef0b3c9a5c8","name":"2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","x":150,"y":1880,"wires":[["35ceb9c1e232bb1d"]]},{"id":"06f4c0dfcf3869b0","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":150,"y":2000,"wires":[["35ceb9c1e232bb1d"]]},{"id":"b906141c19e3a477","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"9","payloadType":"num","x":150,"y":1960,"wires":[["35ceb9c1e232bb1d"]]},{"id":"c65bc84b78187b7a","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"uyuu11","payloadType":"str","x":150,"y":2040,"wires":[["35ceb9c1e232bb1d"]]},{"id":"573ea6deac1f4df4","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":150,"y":1840,"wires":[["35ceb9c1e232bb1d"]]},{"id":"0dde7af39bb21306","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"position","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"(\t    $steps := $$.position - $sum([$flowContext(\"old_position\")]);\t    $bool := $steps >= 0 ? true : false;\t    ([1,1,1,1,1,1,1,1,1,1][[0..($abs($steps)-1)]]).($bool)[]\t)","tot":"jsonata"},{"t":"set","p":"old_position","pt":"flow","to":"position","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":1900,"wires":[["8c268a4bc25b31e7","7cd9321974e09a3a"]]},{"id":"8c268a4bc25b31e7","type":"split","z":"da8a6ef0b3c9a5c8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":610,"y":1900,"wires":[["f608bdd6c5e18b38"]]},{"id":"7cd9321974e09a3a","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 221","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"position","targetType":"msg","statusVal":"postion","statusType":"auto","x":610,"y":1840,"wires":[]},{"id":"f608bdd6c5e18b38","type":"delay","z":"da8a6ef0b3c9a5c8","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"10","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":760,"y":1900,"wires":[["4dfb3005f7869f53"]]},{"id":"4dfb3005f7869f53","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 220","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":1980,"wires":[]}]

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