# How to convert to a function

**URL:** <https://discourse.nodered.org/t/how-to-convert-to-a-function/6170>\
**Category:** General\
**Created:** [27 December 2018 16:49 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170 "2018-12-27T16:49:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [27 December 2018 16:49 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/1 "2018-12-27T16:49:05Z")

</div>

can anyone tell me how i would convert this in to a function so I can set varibles for the tigger times and delays etc?

[{"id":"3640f1c3.dbf8de","type":"trigger","z":"ff81c2c4.ef9ce8","op1":"true","op2":"false","op1type":"bool","op2type":"bool","duration":"10","extend":true,"units":"s","reset":"false","bytopic":"all","name":"","x":310,"y":1340,"wires":[["9304594e.7dab1"]]},{"id":"c45583c1.0452","type":"rpi-gpio out","z":"ff81c2c4.ef9ce8","name":"","pin":"11","set":true,"level":"0","freq":"","out":"out","x":860,"y":1240,"wires":},{"id":"d37c989.ee56068","type":"ui\_text","z":"ff81c2c4.ef9ce8","group":"2f45f695.6435ba","order":4,"width":"1","height":"1","name":"","label":"","format":"{{msg.payload}}","layout":"row-spread","x":850,"y":1320,"wires":},{"id":"aca68c3b.4c646","type":"trigger","z":"ff81c2c4.ef9ce8","op1":"true","op2":"false","op1type":"bool","op2type":"bool","duration":"-200","extend":false,"units":"ms","reset":"false","bytopic":"all","name":"","x":520,"y":1240,"wires":[["a2323876.991ec8","d37c989.ee56068","c45583c1.0452"]]},{"id":"9304594e.7dab1","type":"ui\_switch","z":"ff81c2c4.ef9ce8","name":"cam flash","label":"flash","group":"2f45f695.6435ba","order":5,"width":"2","height":"1","passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":310,"y":1240,"wires":[["aca68c3b.4c646","3640f1c3.dbf8de"]]},{"id":"152a5f4b.d77c11","type":"inject","z":"ff81c2c4.ef9ce8","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":1240,"wires":[["9304594e.7dab1"]]},{"id":"a4564367.fba","type":"inject","z":"ff81c2c4.ef9ce8","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":100,"y":1280,"wires":[["9304594e.7dab1"]]},{"id":"a2323876.991ec8","type":"delay","z":"ff81c2c4.ef9ce8","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":550,"y":1280,"wires":[["67a10a3d.fd0094"]]},{"id":"67a10a3d.fd0094","type":"change","z":"ff81c2c4.ef9ce8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":1320,"wires":[["d37c989.ee56068","c45583c1.0452"]]},{"id":"ed3d4738.2751e","type":"comment","z":"ff81c2c4.ef9ce8","name":"flash camera light for a set time","info":"","x":150,"y":1200,"wires":},{"id":"2f45f695.6435ba","type":"ui\_group","z":"","name":"Train Controls","tab":"22e251db.19b2fa","order":3,"disp":true,"width":"7","collapse":false},{"id":"22e251db.19b2fa","type":"ui\_tab","z":"","name":"Roller Coaster","icon":"dashboard","order":1}]

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d22f48c4c85cc99be1dbc9a436188f590178d9f0.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/0d7f46aa9f36c38f43699ffa0586b1b9ae7bdc50.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 December 2018 18:12 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/2 "2018-12-27T18:12:23Z")

</div>

Hi @gbond

It would help if you could describe what this flow does. Right now you are asking us to import your flow, then reverse engineer what it does without any information.

When it comes to sharing flows, please also read this post about how to format them properly: [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [27 December 2018 18:13 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/3 "2018-12-27T18:13:17Z")

</div>

Hi Garrath, thanks for posting this on the forum ;\*)

When you say "convert to a function", do you mean a `function` node with some javascript code? Or are you wanting to create a custom contrib node (with its own config node) that replaces all this wiring/logic?

In case you have not yet looked at subflows, you can also select the nodes in this flow, convert them to a subflow node, and then re-use it anywhere you want the same logic... but since there is only one definition of each subflow, you can modify the timings once and its behavior will be updated everywhere it is used.

Nick, I was not able to find any docs on subflows... am i just being daft?

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [27 December 2018 20:29 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/4 "2018-12-27T20:29:12Z")

</div>

sorry you lost me as i said how do i make it so that instead at changing the timing in every trigger if i had 10 then i would have to do it 20 times  
I guess some how you can convert it to funtion so you can use varible so you dont have to keep changes the same value over and over

sorry thought it is farily self expanatry by looking at what i have code and said

hope this make sense

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 December 2018 20:44 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/5 "2018-12-27T20:44:52Z")

</div>

If you set a node property to the text `${MY_TIMEOUT}` then the runtime will substitute the value of the environment variable `MY_TIMEOUT`. It cannot be changed dynamically - you have to restart Node-RED to pickup any changes to the env var. But it would let you set all of your trigger nodes to the same timeout value using a single env var.

If you want to dynamically change the value whilst the flow is running, then you'd need to reimplement your flow logic in a function node. If you want help doing that, you will need to describe the behaviour of your flow so we don't have to spend time trying to figure it out.

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [27 December 2018 20:53 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/6 "2018-12-27T20:53:41Z")

</div>

as i have said it is just a light flash I dont need to change anytning while it is running

I dont know what values to use  
I might want it to loop 10 times or may be 3 or indefently while the switch is no(all depends on how it looks)

but with in that loop i might want a quick or slow 3 or 5 flash

It does work but if I decide to change the times i then have to go thought all the same values

In basic i would just use inbedded "for next" loops with the one value I asume there must be a way to do some thing similar with in a function but dont know how, and how you would include the gpio node etc

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [27 December 2018 20:57 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/7 "2018-12-27T20:57:54Z")

</div>

the 2 images show different trials to give you some idea of what i mean

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 December 2018 21:04 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/8 "2018-12-27T21:04:34Z")

</div>

> [@gbond](#):
>
> as i have said it is just a light flash I dont need to change anytning while it is running

Sorry, you haven't said what the flow is for anywhere in this thread.

> [@gbond](#):
>
> It does work but if I decide to change the times i then have to go thought all the same values

Then you could use the environment variable technique i mentioned - giving you one place to change the timings.

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [27 December 2018 21:06 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/9 "2018-12-27T21:06:27Z")

</div>

sounds good but how do you get the varibles into the trigger values etc?

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [27 December 2018 21:08 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/10 "2018-12-27T21:08:07Z")

</div>

and how do you do the loop without using multiple triggers etc  
i take it that is why u use a funtion?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [27 December 2018 21:18 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/11 "2018-12-27T21:18:34Z")

</div>

See original post over on slack [https://node-red.slack.com/archives/C1DH77X1V/p1544665595083600?thread\_ts=1544665595.083600&cid=C1DH77X1V](https://node-red.slack.com/archives/C1DH77X1V/p1544665595083600?thread_ts=1544665595.083600&cid=C1DH77X1V)  
For the original requirement

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [27 December 2018 21:27 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/12 "2018-12-27T21:27:09Z")

</div>

now why have you looped me back to a thread of a different issue (now totally confused what I am suppost to be looking at)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 December 2018 21:28 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/13 "2018-12-27T21:28:57Z")

</div>

I'm away from my laptop for the next few days, so can't provide any concrete examples.

I've mentioned the env var technique and how you do it.

You could rewrite it all as a single JavaScript function, but I can't help with that from my phone. Perhaps someone else could give some guidance on that.

But it really needs a concrete problem description. Is it to just blink an led X times, with the led being on for Y seconds and off for Z seconds? Where X, Y and Z can be easily customized?

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [27 December 2018 21:31 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/14 "2018-12-27T21:31:49Z")

</div>

Yes basically that does sound about right  
It sounds so simple when you put it like that

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 December 2018 21:38 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/15 "2018-12-27T21:38:59Z")

</div>

> [@gbond](#):
>
> now why have you looped me back to a thread of a different issue (now totally confused what I am suppost to be looking at)

That post from Dave was not for your benefit - it was for anyone reading this thread unaware you had already asked on slack and there had been some discussion there with more context to your question.

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [27 December 2018 21:40 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/16 "2018-12-27T21:40:16Z")

</div>

ok dont understand forums no idea whos talking to who !!!!

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 December 2018 21:50 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/17 "2018-12-27T21:50:04Z")

</div>

@gbond indeed, it would often help if people @mentioned the person they were replying to if it isn't a general reply.

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [27 December 2018 21:51 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/18 "2018-12-27T21:51:26Z")

</div>

is this threat not about what i asked ?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 December 2018 21:56 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/19 "2018-12-27T21:56:41Z")

</div>

The link Dave shared was to where you asked this same question on Slack.

I feel like we're getting off topic however. So, let's see if anyone else is in a better position than I am to provide more concrete assistance.

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [27 December 2018 22:22 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/20 "2018-12-27T22:22:13Z")

</div>

any help on this would be great

[Next page](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170.md?page=2)
