# How can i send a message every "X" time?

**URL:** <https://discourse.nodered.org/t/how-can-i-send-a-message-every-x-time/27935>\
**Category:** General\
**Created:** [7 June 2020 09:58 UTC](https://discourse.nodered.org/t/how-can-i-send-a-message-every-x-time/27935 "2020-06-07T09:58:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![DAVIDLL](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@DAVIDLL](https://discourse.nodered.org/u/DAVIDLL)\
**Post date:** [7 June 2020 09:58 UTC](https://discourse.nodered.org/t/how-can-i-send-a-message-every-x-time/27935/1 "2020-06-07T09:58:02Z")

</div>

Hi! i'm working on Node Red and ESP32, i have my mqtt node at node red that receives values constantly, and keeping that values in a data base. I want to send the mqtt message to de DB every 5 minutes. I've tried using a delay, but it just waits the first time, and didn't find anywhere if it can be restarted. I've tried using a trigger, but directly doesn't work, like if was not there. Could you help me? Thanks in advance.

(sorry for my english)

Here is my code:

```auto
[{"id":"e3ad4473.cafa78","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"c8d41688.909c08","type":"mqtt in","z":"e3ad4473.cafa78","name":"","topic":"/local/CT/00:00h","qos":"2","datatype":"auto","broker":"a1041d14.0ae6d","x":320,"y":100,"wires":[["bc0c15cc.ae3888"]]},{"id":"ba09b518.180ab8","type":"mysql","z":"e3ad4473.cafa78","mydb":"9ef090e5.cb6f7","name":"","x":1110,"y":320,"wires":[["c706ef81.119a6"]]},{"id":"bc0c15cc.ae3888","type":"function","z":"e3ad4473.cafa78","name":"MQTT to DB","func":"var newMsg1 = { payload: msg.payload };\nvar newMsg2 = { payload: msg.payload };\nvar newMsg3 = { payload: msg.payload };\nnewMsg1.topic = \"INSERT INTO `DB_ConsumosVivienda`.`Tabla` SET `Consumo Wh` = '\"+newMsg1.payload+\"'\"; \nnewMsg3.topic = \"UPDATE `DB_ConsumosVivienda`.`Tabla` SET `Hora` = '\"+newMsg3.payload+\"'\";\nnewMsg2.topic = \"UPDATE `DB_ConsumosVivienda`.`Tabla` SET `Fecha` = '\"+newMsg2.payload+\"'\"; \nreturn [newMsg1, newMsg2, newMsg3];\n\n","outputs":3,"noerr":0,"x":630,"y":100,"wires":[["318062c5.c5907e"],[],[]]},{"id":"59aa34c0.05845c","type":"moment","z":"e3ad4473.cafa78","name":"Fecha","topic":"","input":"","inputType":"date","inTz":"Europe/Madrid","adjAmount":0,"adjType":"days","adjDir":"add","format":"DD/MM/YYYY","locale":"es_ES","output":"payload","outputType":"msg","outTz":"Europe/Madrid","x":450,"y":680,"wires":[["bc0c15cc.ae3888"]],"info":"msg.payload = newDate(); \nmoment().format(\"DD,MM,YYYY\"); \nreturn msg;"},{"id":"37b48da4.ee0792","type":"moment","z":"e3ad4473.cafa78","name":"Hora","topic":"","input":"","inputType":"date","inTz":"Europe/Madrid","adjAmount":0,"adjType":"days","adjDir":"add","format":"LT","locale":"es_ES","output":"payload","outputType":"msg","outTz":"Europe/Madrid","x":450,"y":740,"wires":[["bc0c15cc.ae3888"]],"info":"msg.payload = newDate(); \nmoment().format(\"DD,MM,YYYY\"); \nreturn msg;"},{"id":"c706ef81.119a6","type":"debug","z":"e3ad4473.cafa78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1350,"y":320,"wires":[]},{"id":"14fe539d.0cb5dc","type":"delay","z":"e3ad4473.cafa78","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":860,"y":200,"wires":[[]]},{"id":"65d7db8e.de33d4","type":"inject","z":"e3ad4473.cafa78","name":"","topic":"","payload":"","payloadType":"date","repeat":"0.1","crontab":"","once":true,"onceDelay":0.1,"x":290,"y":720,"wires":[["59aa34c0.05845c","37b48da4.ee0792"]]},{"id":"318062c5.c5907e","type":"trigger","z":"e3ad4473.cafa78","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"-1","extend":false,"units":"min","reset":"","bytopic":"all","name":"","x":890,"y":100,"wires":[["ba09b518.180ab8"]]},{"id":"a1041d14.0ae6d","type":"mqtt-broker","z":"","name":"BrokerLocal","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"9ef090e5.cb6f7","type":"MySQLdatabase","z":"","name":"","host":"127.0.0.1","port":"3306","db":"DB_ConsumosVivienda","tz":""}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [7 June 2020 10:03 UTC](https://discourse.nodered.org/t/how-can-i-send-a-message-every-x-time/27935/2 "2020-06-07T10:03:10Z")

</div>

You could store the values in flow context & use an inject set to run every 5 mins to get the values from context & send them to DB

Flow 1...  
MQTT \> change node (store MQTT values)

Flow 2...  
Inject (every 5 mins) \> change node (get values from context) \> DB insert

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [7 June 2020 10:55 UTC](https://discourse.nodered.org/t/how-can-i-send-a-message-every-x-time/27935/3 "2020-06-07T10:55:06Z")

</div>

Have a look at node-red-contrib-calculate, it can average all the mqtt data and then send it ones 5 minutes to your dbase.

---

<div class="post-metadata">

**Author:** ![DAVIDLL](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@DAVIDLL](https://discourse.nodered.org/u/DAVIDLL)\
**Post date:** [7 June 2020 16:16 UTC](https://discourse.nodered.org/t/how-can-i-send-a-message-every-x-time/27935/4 "2020-06-07T16:16:59Z")

</div>

Thanks for your answer, but i don`t need to storage any value o a buffer and send it later. I mean, i just send the same value all time by mqtt, but i wanna writte it every 5 minutes. does it have logic?

---

<div class="post-metadata">

**Author:** ![DAVIDLL](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@DAVIDLL](https://discourse.nodered.org/u/DAVIDLL)\
**Post date:** [7 June 2020 16:18 UTC](https://discourse.nodered.org/t/how-can-i-send-a-message-every-x-time/27935/5 "2020-06-07T16:18:25Z")

</div>

Hi! i will check out that node, i dont need to do any calculation before sending, just send one number every 5 minutes. Thanks!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 June 2020 16:44 UTC](https://discourse.nodered.org/t/how-can-i-send-a-message-every-x-time/27935/6 "2020-06-07T16:44:58Z")

</div>

If all you want to do is to repeat a message every so often you can configure a Trigger node to repeat the last message every time period.  
Or if it is a fixed number then an Inject node can do that.

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [7 June 2020 17:03 UTC](https://discourse.nodered.org/t/how-can-i-send-a-message-every-x-time/27935/7 "2020-06-07T17:03:34Z")

</div>

Why doesn't a delay node work if set to rate limit? Example flow:

```auto
[{"id":"89d45f2a.684fa","type":"inject","z":"921f40e7.b19aa","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":80,"wires":[["6d078eb7.49b81"]]},{"id":"bf889548.c785e8","type":"debug","z":"921f40e7.b19aa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":250,"y":200,"wires":[]},{"id":"6d078eb7.49b81","type":"delay","z":"921f40e7.b19aa","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"5","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":200,"y":140,"wires":[["bf889548.c785e8"]]}]

```

---

<div class="post-metadata">

**Author:** ![DAVIDLL](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@DAVIDLL](https://discourse.nodered.org/u/DAVIDLL)\
**Post date:** [7 June 2020 18:11 UTC](https://discourse.nodered.org/t/how-can-i-send-a-message-every-x-time/27935/8 "2020-06-07T18:11:01Z")

</div>

Yass, thats a good one, thank you! But i'm sending three messages at the same time so, using that delay, it does not send them at the same time. I mean, i want to send 3 messages at the same time (that are in the same function) every "x" time (5 seconds or anyone).

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [7 June 2020 19:26 UTC](https://discourse.nodered.org/t/how-can-i-send-a-message-every-x-time/27935/9 "2020-06-07T19:26:49Z")

</div>

First you say

> [@DAVIDLL](#):
>
> But i'm sending three messages at the same time

Then you say...

> [@DAVIDLL](#):
>
> I mean, i want to send 3 messages at the same time (that are in the same function) every "x" time (5 seconds or anyone).

It's quite difficult to understand what you want.

Going by your last sentence, you can send 3 messages from a function at the same time like this...

```auto
var msg1 = {payload:"mesage 1"}; //example message
var msg2 = {payload:"mesage 2"}; //example message
var msg3 = {payload:"mesage 3"}; //example message
return [msg1,msg2,msg3]; //send all three

```

NOTE: this will actually send 3 messages out

Alternatively, you can send 3 payloads at 1 time...

```auto
msg.payload1 = "payload 1";
msg.payload2 = "payload 2";
msg.payload3 = "payload 3";
return msg;

```

---

<div class="post-metadata">

**Author:** ![pgScorpio](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pgscorpio/32/19675_2.png) [@pgScorpio](https://discourse.nodered.org/u/pgScorpio)\
**Post date:** [7 June 2020 20:54 UTC](https://discourse.nodered.org/t/how-can-i-send-a-message-every-x-time/27935/10 "2020-06-07T20:54:54Z")

</div>

Use delay in rate-limit mode ?

```auto
[{"id":"2d79e8e.d7cfc18","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"6ebdd835.553d48","type":"mqtt in","z":"2d79e8e.d7cfc18","name":"","topic":"/local/CT/00:00h","qos":"2","datatype":"auto","broker":"4f9c832a.93a4ec","x":200,"y":100,"wires":[["a62032e3.c076a"]]},{"id":"461c0028.7cc03","type":"mysql","z":"2d79e8e.d7cfc18","mydb":"3b409cec.dc0f54","name":"","x":1110,"y":320,"wires":[["531814dd.a9175c"]]},{"id":"8a118b45.d67c98","type":"function","z":"2d79e8e.d7cfc18","name":"MQTT to DB","func":"var newMsg1 = { payload: msg.payload };\nvar newMsg2 = { payload: msg.payload };\nvar newMsg3 = { payload: msg.payload };\nnewMsg1.topic = \"INSERT INTO `DB_ConsumosVivienda`.`Tabla` SET `Consumo Wh` = '\"+newMsg1.payload+\"'\"; \nnewMsg3.topic = \"UPDATE `DB_ConsumosVivienda`.`Tabla` SET `Hora` = '\"+newMsg3.payload+\"'\";\nnewMsg2.topic = \"UPDATE `DB_ConsumosVivienda`.`Tabla` SET `Fecha` = '\"+newMsg2.payload+\"'\"; \nreturn [newMsg1, newMsg2, newMsg3];\n\n","outputs":3,"noerr":0,"x":610,"y":100,"wires":[["461c0028.7cc03"],[],[]]},{"id":"87a6a497.ceecf8","type":"moment","z":"2d79e8e.d7cfc18","name":"Fecha","topic":"","input":"","inputType":"date","inTz":"Europe/Madrid","adjAmount":0,"adjType":"days","adjDir":"add","format":"DD/MM/YYYY","locale":"es_ES","output":"payload","outputType":"msg","outTz":"Europe/Madrid","x":450,"y":680,"wires":[["8a118b45.d67c98"]],"info":"msg.payload = newDate(); \nmoment().format(\"DD,MM,YYYY\"); \nreturn msg;"},{"id":"bac0b96f.345fc8","type":"moment","z":"2d79e8e.d7cfc18","name":"Hora","topic":"","input":"","inputType":"date","inTz":"Europe/Madrid","adjAmount":0,"adjType":"days","adjDir":"add","format":"LT","locale":"es_ES","output":"payload","outputType":"msg","outTz":"Europe/Madrid","x":450,"y":740,"wires":[["8a118b45.d67c98"]],"info":"msg.payload = newDate(); \nmoment().format(\"DD,MM,YYYY\"); \nreturn msg;"},{"id":"531814dd.a9175c","type":"debug","z":"2d79e8e.d7cfc18","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1350,"y":320,"wires":[]},{"id":"934132d2.8b69d","type":"inject","z":"2d79e8e.d7cfc18","name":"","topic":"","payload":"","payloadType":"date","repeat":"0.1","crontab":"","once":true,"onceDelay":0.1,"x":290,"y":720,"wires":[["87a6a497.ceecf8","bac0b96f.345fc8"]]},{"id":"a62032e3.c076a","type":"delay","z":"2d79e8e.d7cfc18","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"5","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":400,"y":100,"wires":[["8a118b45.d67c98"]]},{"id":"4f9c832a.93a4ec","type":"mqtt-broker","z":"","name":"BrokerLocal","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"3b409cec.dc0f54","type":"MySQLdatabase","z":"","name":"","host":"127.0.0.1","port":"3306","db":"DB_ConsumosVivienda","tz":""}]

```

send 3 messages at a time:

```auto
var result = [];

result[0] = { topic: "INSERT INTO `DB_ConsumosVivienda`.`Tabla` SET `Consumo Wh` = '"+msg.payload+"'", payload: msg.payload };
result[1] = { topic: "UPDATE `DB_ConsumosVivienda`.`Tabla` SET `Hora` = '"+msg.payload+"'", payload: msg.payload };
result[2] = { topic: "UPDATE `DB_ConsumosVivienda`.`Tabla` SET `Fecha` = '"+msg.payload+"'", payload: msg.payload };

return result;

```

---

<div class="post-metadata">

**Author:** ![DAVIDLL](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@DAVIDLL](https://discourse.nodered.org/u/DAVIDLL)\
**Post date:** [10 June 2020 08:48 UTC](https://discourse.nodered.org/t/how-can-i-send-a-message-every-x-time/27935/11 "2020-06-10T08:48:14Z")

</div>

Finally i used the timespamt for the date and hour and changed my arduino code for sending the mqtt message at a specific time. Thank you all!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 June 2020 08:48 UTC](https://discourse.nodered.org/t/how-can-i-send-a-message-every-x-time/27935/12 "2020-06-24T08:48:21Z")

</div>

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