# Need help with a function

**URL:** <https://discourse.nodered.org/t/need-help-with-a-function/73018>\
**Category:** General\
**Tags:** function-node\
**Created:** [1 January 2023 13:05 UTC](https://discourse.nodered.org/t/need-help-with-a-function/73018 "2023-01-01T13:05:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigmac5753](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bigmac5753/32/15733_2.png) [@bigmac5753](https://discourse.nodered.org/u/bigmac5753)\
**Post date:** [1 January 2023 13:05 UTC](https://discourse.nodered.org/t/need-help-with-a-function/73018/1 "2023-01-01T13:05:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [1 January 2023 13:27 UTC](https://discourse.nodered.org/t/need-help-with-a-function/73018/2 "2023-01-01T13:27:59Z")

</div>

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:

- To store values between node runs, use a [context](https://nodered.org/docs/user-guide/writing-functions#context) store.
- To emit multiple messages from a single output, create an [appropriately structured](https://nodered.org/docs/user-guide/writing-functions#multiple-messages) `msg` object.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 January 2023 13:29 UTC](https://discourse.nodered.org/t/need-help-with-a-function/73018/3 "2023-01-01T13:29:19Z")

</div>

Something like this...

![chrome_r9nQx4KJxG](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/a/2a5122181159d60170d1c2aeaf8cb5bdca1582f1.gif)

Demo flow (use `CTRL+I` to import)

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![bigmac5753](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bigmac5753/32/15733_2.png) [@bigmac5753](https://discourse.nodered.org/u/bigmac5753)\
**Post date:** [1 January 2023 13:33 UTC](https://discourse.nodered.org/t/need-help-with-a-function/73018/4 "2023-01-01T13:33:04Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> [{"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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 January 2023 13:34 UTC](https://discourse.nodered.org/t/need-help-with-a-function/73018/5 "2023-01-01T13:34:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bigmac5753](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bigmac5753/32/15733_2.png) [@bigmac5753](https://discourse.nodered.org/u/bigmac5753)\
**Post date:** [1 January 2023 13:40 UTC](https://discourse.nodered.org/t/need-help-with-a-function/73018/6 "2023-01-01T13:40:19Z")

</div>

spot on, thanks

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [1 January 2023 15:14 UTC](https://discourse.nodered.org/t/need-help-with-a-function/73018/7 "2023-01-01T15:14:16Z")

</div>

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".

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [15 January 2023 15:14 UTC](https://discourse.nodered.org/t/need-help-with-a-function/73018/8 "2023-01-15T15:14:51Z")

</div>

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