Modify "function node" to create "Loop"

[{"id":"7401e57d.659e4c","type":"function","z":"f5d4feda.70164","name":"dimming","func":"x = msg.payload\nfor(var x=50; x<=255; x++)\n{\n    node.send({\"bright\":x})\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":260,"wires":[["4bb6c99b.16b0b8"]]},{"id":"4bb6c99b.16b0b8","type":"delay","z":"f5d4feda.70164","name":"","pauseType":"rate","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"0.05","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":670,"y":260,"wires":[["f888b1de.2446a"]]},{"id":"f888b1de.2446a","type":"change","z":"f5d4feda.70164","name":"bright to payload","rules":[{"t":"move","p":"bright","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":890,"y":260,"wires":[["451189b4.7844f8"]]},{"id":"451189b4.7844f8","type":"debug","z":"f5d4feda.70164","name":"bright","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1090,"y":260,"wires":[]},{"id":"dd723f23.5e6c3","type":"inject","z":"f5d4feda.70164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"50","payloadType":"num","x":290,"y":260,"wires":[["7401e57d.659e4c"]]},{"id":"413d0494.b8896c","type":"function","z":"f5d4feda.70164","name":"dimming","func":"x = msg.payload\nfor(var x=255; x>=50; x--)\n{\n    node.send({\"bright\":x})\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":320,"wires":[["4bb6c99b.16b0b8"]]},{"id":"f15efa87.c3bb28","type":"inject","z":"f5d4feda.70164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"255","payloadType":"num","x":290,"y":320,"wires":[["413d0494.b8896c"]]}]

There are two inject node(50, 255).
When "50" is entered,

x = msg.payload
for(var x=50; x<=255; x++)
{
node.send({"bright":x})
}

When "255" is entered,

x = msg.payload
for(var x=255; x>=50; x--)
{
node.send({"bright":x})
}

Function node activates.

I want to repeat the last message on the node until the other data comes in.
(For example, input "50" -> function node ( x=50; x<=255; x++ ) -> Repeat "255"(last message) indefinitely until new "255" is entered.)

Which node should I add? loop? repeat?
Or how should I modify the function node?

If you just want to keep repeating the previous message then you can do that with a Trigger node configured like this.

image

1 Like

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