Setting the timer in node-red function node for few milliseconds

I have been looking for a way to set the timer for a few milliseconds in the function node.
Actually the scenario is that the function node is receiving the data coming from the sensor which sends this data when an event happens. It sends a stream of data that sometimes contains those values which are not correct. My requirement is to filter this data and extract the maximum value out of it. For this, I think to start the timer when the first value comes and then stop it after a few milliseconds and then extract the maximum value out of it afterward would fulfill my requirement. If there any suitable option available besides this? If not then, Could you please suggest me how to implement the timers in the function node. Thanks in advance and looking forward to your valuable suggestions.

Look at using the join node to combine all arriving payloads into one array. Combine it with a trigger node that first sends nothing and extends its delay when a new message arrives and than send a msg. Set the msg.complete property on that msg with a change node and connect it to the join node. Set the trigger to the desired timeout.
Now when no messages arrive for the configured timeout you will get an array with all the values in it from the join node. You can search your value in that array.

1 Like

Is the data coming in as a stream of individual messages? What rate are they arriving? If it is a stream of messages then can you do it over a count of messages rather than a time? If you can do it over a count then interpret them as they come in and save the maximum value in the node context as you go. Then when you have enough you can pass on the result.

1 Like

Thank you @Colin for the reply.
The data is coming in the form of a buffer from a serial node, the timeout is set there for 1 msec and it reads the complete buffer of an event within this time. In my view based on count of incoming buffer would not serve the purpose because I don't know about the exact number of events, the only thing I can define here is the time. What is your thought?

Thank you @JGKK for the reply. Let me implement and test your idea in my flow.

Can you explain further what the incoming serial data looks like? Is it a continuous stream with no gaps? Tell us a bit more about how the data is structured.

1 Like

Thank you @Colin for the reply. The data is structured in a buffer that comprises the actual data byte along with other bytes. The sensor sends the buffer as soon as the event occurs, shown in the picture. Sometimes, it continuously sends this buffer after every millisecond. For this scenario, I am interested in detecting the peak value out of these huge amounts of values.

If it is sending at maximum rate is there still a gap between the messages? If not then how do you know when one buffer ends and the next begins?

@Colin you raised a valid point, if it sends at the maximum rate, I face the problem of having invalid buffer because I have configured the split input setting of the serial in node with timeout, so when the sensor sends the data it starts filling the buffer more than the expected number of bytes. However, I cannot use the 'fixed length' setting either because the buffer size is not fixed. As far as the third option of 'on the character \n' is concerned, it also gives unexpected results sometimes. If this character comes between the data packet it breaks the buffer at that point and make the complete buffer. I have also mentioned this problem on another post and you have mentioned me to use the time out option. Therefore, I am using this option which works pretty well for a single event but when the data comes in stream with high transmission rate it shows invalid buffer at the output with too many bytes.

Do you have control of the sending software?

@Colin unfortunately I don't have that control. The only playing field available for me is Node-Red.

So how does the supplier of the device say you are supposed to be able to decode the data when it comes as a continuous stream, if there is no way of identifying the start and end of a buffer?

.. or even the start OR end of the buffer... there must be something we can get a handle on here...

@Colin @dceejay, I understood, yes the start and the end byte of the buffer are fixed which identify the starting and ending of the buffer.

Tell us the details of how you know the buffer start and end please.

In fact that may not be necessary. I think the solution is to tell the serial port to give you a buffer after a timeout time set to give you the maximum amount of data that you want in one go. Then you will get a buffer back which you can parse and extract the data. You may get to the end of the buffer in the middle of a message, in which case you need to keep the extra bit, wait for the next buffer, and add that onto the bit you kept from last time, then parse it again.

well if you know what the end byte is (and it's fixed) then you can use the split on 0xXX format to specify it - likewise the start byte... but again without any clues we'll have to leave it up to you.

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