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

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:

[{"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":""}]

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

1 Like

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.

1 Like

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?

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

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.

1 Like

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

[{"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"]]}]
1 Like

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).

First you say

Then you say...

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...

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...

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

Use delay in rate-limit mode ?

[{"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:

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;
1 Like

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!

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