Repeating a message?

1,121 views
Skip to first unread message

Nick Barnett

unread,
Feb 9, 2017, 7:06:22 PM2/9/17
to Node-RED
What is the best way to repeat an mqtt message? I have built an IR blaster out of an esp8266-01 and I'm using it to control the volume on a stereo. It is easy enough to hit the button in the dashboard 5 times, but I want to add a button(s) that turns it up (and down) at a higher rate because sometimes I need the volume to react much faster. I'm also interested in using this method to turn the volume 100% down so I know what volume I'm starting with... That way I can figure out how many "blasts" to get to 25%, 50%, and 75% of Max volume.

Colin Law

unread,
Feb 10, 2017, 4:13:46 AM2/10/17
to node...@googlegroups.com
You can repeat a message multiple times with a simple function node
containing, for example
return [[msg,msg,msg,msg]];
which will send it four times.

Alternatively to send it a variable number of times you can do write a
loop sending it multiple times, for example
var msgCount = 4;
for (i = 0; i < msgCount; i++) {
node.send( msg );
}

Colin

On 10 February 2017 at 00:06, Nick Barnett <nicksb...@gmail.com> wrote:
> What is the best way to repeat an mqtt message? I have built an IR blaster out of an esp8266-01 and I'm using it to control the volume on a stereo. It is easy enough to hit the button in the dashboard 5 times, but I want to add a button(s) that turns it up (and down) at a higher rate because sometimes I need the volume to react much faster. I'm also interested in using this method to turn the volume 100% down so I know what volume I'm starting with... That way I can figure out how many "blasts" to get to 25%, 50%, and 75% of Max volume.
>
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
> To post to this group, send an email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/e610da0f-8ec8-4f5c-9734-29f8bae48834%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Mark Setrem

unread,
Feb 10, 2017, 5:50:45 AM2/10/17
to Node-RED
Alternatively somewhere in your flow take a second output from a node, run that through a delay node and rejoin the input for the next node.

Which will result in the downstream nodes seeing your original message followed, after the delay, by the delayed copy of your message.

You can configure the length of the delay

Walter Kraembring

unread,
Feb 10, 2017, 7:40:46 AM2/10/17
to Node-RED
Another contribution, if you want a delay in between repetition

var i = 0, howManyTimes = 5;
function f() {
    node.send(msg);
    i++;
    if( i < howManyTimes ){
        setTimeout( f, 3000 );
    }
}
f();


Nick O'Leary

unread,
Feb 10, 2017, 7:59:59 AM2/10/17
to Node-RED Mailing List
For all of this suggestions, you should really use:

    node.send(RED.utils.cloneMessage(msg));

otherwise you are sending a reference to the same msg object each time which can have undesirable side effects.

Nick

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.

Colin Law

unread,
Feb 10, 2017, 8:37:56 AM2/10/17
to node...@googlegroups.com
I wondered about that Nick. What sort of side effects might there be?

Colin

On 10 February 2017 at 12:59, Nick O'Leary <nick....@gmail.com> wrote:
> For all of this suggestions, you should really use:
>
> node.send(RED.utils.cloneMessage(msg));
>
> otherwise you are sending a reference to the same msg object each time which
> can have undesirable side effects.
>
> Nick
>
> On 10 February 2017 at 12:40, Walter Kraembring <walter.k...@gmail.com>
> wrote:
>>
>> Another contribution, if you want a delay in between repetition
>>
>> var i = 0, howManyTimes = 5;
>> function f() {
>> node.send(msg);
>> i++;
>> if( i < howManyTimes ){
>> setTimeout( f, 3000 );
>> }
>> }
>> f();
>>
>>
>> --
>> http://nodered.org
>>
>> Join us on Slack to continue the conversation: http://nodered.org/slack
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Node-RED" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to node-red+u...@googlegroups.com.
>> To post to this group, send email to node...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/node-red.
>> To view this discussion on the web, visit
>> https://groups.google.com/d/msgid/node-red/793d2b04-f364-4636-b513-333cf721f6bf%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/CAF%3DvhqcLyGCAaCO4DMJ44kMm7bO7suoomdqqhaUn-deTxXtuAg%40mail.gmail.com.

Nick O'Leary

unread,
Feb 10, 2017, 8:42:52 AM2/10/17
to Node-RED Mailing List
Lets say the message sent by the function is:
 {
    payload: []
 }

And that Function is wired to another function that does:
  msg.payload.push("Hello");
  return msg;


The first time the message is sent you'll get:
{
   payload: [ "Hello" ]
}

If the first function then sends the msg object again, you'll get:
{
   payload: [ "Hello", "Hello" ]
}
because it is passing a reference to the same array as the first message.

And so on...

If a function node passes multiple messages to node.send, we automatically clone them. It is only when you pass a single message to node.send that we don't automatically clone - this means in the most common cases we can avoid the overhead of cloning messages.

Nick





>> To post to this group, send email to node...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/node-red.
>> To view this discussion on the web, visit
>> https://groups.google.com/d/msgid/node-red/793d2b04-f364-4636-b513-333cf721f6bf%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an

> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/CAF%3DvhqcLyGCAaCO4DMJ44kMm7bO7suoomdqqhaUn-deTxXtuAg%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.
--
http://nodered.org

Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send an email to node...@googlegroups.com.

Colin Law

unread,
Feb 10, 2017, 8:50:41 AM2/10/17
to node...@googlegroups.com
I see, thanks.

Colin
>> >> email to node-red+u...@googlegroups.com.
>> >> To post to this group, send email to node...@googlegroups.com.
>> >> Visit this group at https://groups.google.com/group/node-red.
>> >> To view this discussion on the web, visit
>> >>
>> >> https://groups.google.com/d/msgid/node-red/793d2b04-f364-4636-b513-333cf721f6bf%40googlegroups.com.
>> >> For more options, visit https://groups.google.com/d/optout.
>> >
>> >
>> > --
>> > http://nodered.org
>> >
>> > Join us on Slack to continue the conversation: http://nodered.org/slack
>> > ---
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "Node-RED" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to node-red+u...@googlegroups.com.
>> > To post to this group, send email to node...@googlegroups.com.
>> > Visit this group at https://groups.google.com/group/node-red.
>> > To view this discussion on the web, visit
>> >
>> > https://groups.google.com/d/msgid/node-red/CAF%3DvhqcLyGCAaCO4DMJ44kMm7bO7suoomdqqhaUn-deTxXtuAg%40mail.gmail.com.
>> >
>> > For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> http://nodered.org
>>
>> Join us on Slack to continue the conversation: http://nodered.org/slack
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Node-RED" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to node-red+u...@googlegroups.com.
>> To post to this group, send an email to node...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/node-red.
>> To view this discussion on the web, visit
>> https://groups.google.com/d/msgid/node-red/CAL%3D0gLvp-nSQW9g1ygT24B5cQEMCZQqgWAqdbwzbMYi9RTvk-w%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/CAF%3DvhqdqSdoaVJiraDmyru2XJQ5Vi-B%3D0s-pD2X_uKsSEqhH5g%40mail.gmail.com.

Nick Barnett

unread,
Feb 10, 2017, 4:17:49 PM2/10/17
to Node-RED
Fantastic info. Thank you everyone.
Reply all
Reply to author
Forward
0 new messages