Calling a function regularly


Is there is a better way of calling the function every 1 second, and send the appropriate message to a mqtt node?

Maybe something like an interrupt in the coding world? That only operates when the global variables change..


[{"id":"2d3eddbc.3f19f2","type":"function","z":"2a6827d4.9b5e18","name":"function","func":"\n//Retrieve global variables\nvar houseArmedState = global.get(\"houseArmed\");\nvar sentryModeState = global.get(\"sentryMode\");\n\n\n//If house is NOT set, AND  sentry Mode enabled, proceed.... \nif (houseArmedState == '0' & sentryModeState == '1') \n{\n    msg.payload = \"red\";\n}\n\nelse \n{\n     msg.payload = \"off\";\n    \n}\n\n\n//Send the message to MQTT node\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1480,"y":1610,"wires":[["361f31e5.70550e"]]},{"id":"20be1d8c.31f772","type":"inject","z":"2a6827d4.9b5e18","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1280,"y":1610,"wires":[["2d3eddbc.3f19f2"]]},{"id":"361f31e5.70550e","type":"debug","z":"2a6827d4.9b5e18","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1710,"y":1610,"wires":[]}]

Potentially yes. Connect your function to the part of flow that changes the global variable. Use an RBE node if necessary to avoid calling the function if the value is unchanged.

1 Like

Hi Steve,

Yes that would work, maybe a little work as I have two global variables to monitor.

But I think it can be done better if I tidy things up a bit.

Just trying not to unnecessarily load the mqtt server if it can be avoided.

Thanks :+1:

That is most unlikely to cause you problems unless you are sending hundreds of messages a second, even on something like a Pi.
Retained topics in MQTT can often make the use of global or flow variables unnecessary, with the advantage that you don't need to poll them as you automatically get given the most recent value on startup and get notified immediately if one gets modified. Often flows will be less opaque using MQTT rather than context.

1 Like

Another way to do what you want is to create a setInterval function inside your function node. Then you only need to trigger it once.

However, you might want to assign it to a variable that you save to context so that you can retrieve and cancel it. Typically, I try to retrieve the variable and cancel the function FIRST in the function node so that I know I can re-trigger the node without queuing up loads of setIntervals.

1 Like


In javascript world, what you are looking for is called an ā€œobservableā€ that will trigger things whenever some values change.

You may want to google observable and rxjs for more info on the topic

1 Like

Thanks everyone, lots of good ideas for me to try :+1:

@Colin I'm thinking about how this would work with the retained message.

As you can see in my below screenshot:

Input 1 - the alarm set state from mqtt (every 1 sec)

Input 2 - the sentry mode state from a virtual switch (Blynk app).

Then the function as before checks the state of those two, and writes the relevant payload to the final mqtt node.

Only the final output mqtt node can be set to retained=true. I can't understand how to take advantage of that how you described..


The variables you might consider keeping in MQTT retained topics are ones that represent a state of the system. So in this case possibly houseArmed and sentryMode, though I don't know anything about your system so that is just guesswork. Then get rid of the global variables and anywhere you use them feed them in from MQTT.

1 Like