Using JINA2 substitutions in Node-Red Nodes?

Hi,
How does one use JINJA2 replacements in Node-Red nodes? (and can it be done?)
e.g. the looptimer node is expecting a number in the loop every field (see screenshot)


But instead of that number, I want to use a variable or an attribute of the message
How would one code it?
I have searched the documentation but I could not find an answer.
I tried single and double braces, global.get and $ etc, etc.
thanks so much,
chris

You almost never need JINJA templates with node-red. if you use a trigger node or current state, whatever variable you need can be extracted from the message object, for example msg.payload or msg.data.attributes.XXX

That being said, unfortunately you can’t change the loop timer for that node programmatically. That’s a limitation of than node. Maybe you can find another looptimer node that is capable of doing it. or alternatively, a function node. https://discourse.nodered.org/t/can-i-have-a-delay-sleep-inside-function-node/3101

1 Like

thank you @serkank
Jinja2-replacement:
I was hoping that Jinja2-replacement would happen before the data is processed by the node and the node would never see the “variable”. But I’m not a programmer.

Function node with a delay:
I also tried the other path that you suggest; using a function node to make a delay. There I get stuck on the way “sleep” is handled in JavaScript. It is my understanding that there is no such thing as “sleep” in JS. And the timeout is asynchronous.
I tried :

setTimeout(() => {  return msg;  }, 2000);

but that didn’t work. Anyone any tips on why ?

I want to achieve the following:
first hit= delay 2sec, second hit = delay 4 sec, then 8, then 16, then 32, then 1 minute,…

If I were only smarter, :innocent:

chrisV

wait wait wait…
Replacing return msg; by node.send(msg); as indicated in the discourse link by @serkank seems to work :slight_smile:

setTimeout(() => { node.send(msg)  }, 20000);

The above waits 20 seconds before continuing
Note to self: READ before WRITE chrisV…
Exploring this further but looking very promising so far
chrisV

glad to be help… I guess… :slight_smile:

here is another idea, after explained what you want to achieve. delay node can help here. it looks ugly but no need a function node.

[{"id":"e69c797a.e2e748","type":"delay","z":"e967fc6e.96e16","name":"","pauseType":"delayv","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":400,"y":440,"wires":[["7fee3fd5.55009","b5eafa29.f043c8"]]},{"id":"873f45d4.d6c088","type":"inject","z":"e967fc6e.96e16","name":"","props":[{"p":"payload"},{"p":"delay","v":"2000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":440,"wires":[["e69c797a.e2e748"]]},{"id":"7fee3fd5.55009","type":"debug","z":"e967fc6e.96e16","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":610,"y":440,"wires":[]},{"id":"b5eafa29.f043c8","type":"change","z":"e967fc6e.96e16","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"msg.delay*2","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":540,"wires":[["e69c797a.e2e748"]]},{"id":"95bf33b3.23e6b","type":"inject","z":"e967fc6e.96e16","name":"reset","props":[{"p":"payload"},{"p":"reset","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":201,"y":389,"wires":[["e69c797a.e2e748"]]}]
1 Like

Hi @serkank
I was not aware there was a delay node
And…JSONata expressions… that is something I need to explore
awesome information
Learning exiting new things here :slight_smile:
thank you for sharing
chrisV