How to output exactly the same object input?

Hi, my goal is to take only the last message in a certain period of time, th first message triggers the counter, if there is no other messages during that time the last one will output as it come in input, the others will be discarded.
For doing this I'm using a function, it outputs on th first output a reset to the next delay object, the second output will pass the input object. If there will not be any other messages the delay will not be reset and the last message will output.
My problem is that if I use this code I'll have the input object incapsulated into the msg object, how to avoid it? Thanks

msg2={msg};
msg={reset:true};
return [msg,msg2];

a
the object is inside msg

return [{reset:true},msg]; should do the trick.

Or:

var msgReset={reset:true};
return [msgReset,msg];

The curly brackets around msg gives you the a object including msg. But be aware objects variables only pointing to the object not holding the object itself. Keep this in mind when doing something like var msg2=msg Both are now pointing to the same data so if you change msg.payload then msg2.payload results the same changed value. Placing curly brackets does not clone an object. If you need that you can use var clonedMsg= RED.util.cloneMessage(msg)

RED

  • RED.util.cloneMessage(..) : safely clones a message object so it can be reused
1 Like

thank you a lot!! :slight_smile: really really helpful!

PS: there is any node who can do itself what I need? Throttle node has different functions but not mine, it eventually outputs the first message discarding the following ones. Thanks

I only imagine what your setup is. I thing you have a slider controlling the brightness of a rgb light and if you slide it send to many messages to your light? Perhaps like this:

https://cookbook.nodered.org/basic/rate-limit-message-stream

well, a kind of ))
my goal is to limit the number of packes, some come duplicated or too fast changes, and also Alexa has the habit to sent 2 packets when the brightness command is requested, if the light was switched off it switch on and then send intensity, for my purposes I would receive only the intensity command, and manage myself with the proper delays the switch on then change brighness.

For this reason I need something that let out only the last message received, which is supposed the most important, and the final decision only

The trigger node can be set to initially send nothing, then the last message received after a period of time...

1 Like

great, I'll test it!

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