How to convert to a function

#1

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}]

0 Likes

#2

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

0 Likes

#3

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?

0 Likes

#4

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

0 Likes

#5

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.

0 Likes

#6

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

0 Likes

#7

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

0 Likes

#8

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

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

0 Likes

#9

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

0 Likes

#10

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

0 Likes

#11

See original post over on slack https://node-red.slack.com/archives/C1DH77X1V/p1544665595083600?thread_ts=1544665595.083600&cid=C1DH77X1V
For the original requirement

0 Likes

#12

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)

0 Likes

#13

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?

0 Likes

#14

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

0 Likes

#15

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.

0 Likes

#16

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

0 Likes

#17

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

0 Likes

#18

is this threat not about what i asked ?

0 Likes

#19

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.

0 Likes

#20

any help on this would be great

0 Likes