# Pick the last value

Created the Unique ID with the define range, i want to select the output of the last Unique value it showing (refer to upload://7NYXPYznBSQJUdje4hk0Sql6SN.png)

Function:

``````if (msg.payload.Pos1_op > 21.00 & msg.payload.Pos1_op < 21.50)
{
d = new Date,
dformat = [d.getMonth()+1,
d.getDate(),
d.getFullYear()].join('')+''+
[d.getHours(),
d.getMinutes(),
d.getSeconds()].join('');

dtstmp = new Date().toString();
pld1 = "L16POS1"+ dformat +""

return msg;
}
``````

Flow Image:

Say the entire range is 0 to 5. You need to keep a history of values in some way to know which is unique. For example, array, the logic is...Each line is a new value and how it is processed or handled.

A = [ 0] first value, last unique is 0
A = [ 0,1 ] next, last unique 1
A = [ 0,1,2 ] next, last unique is 2
A = [ 0,1,2,3 ] next, last unique is 3

At this point they are all unique values right? So 0, then 1, then 2, then 3 are the result you want? As the last unique value, right?

With more values...

A = [ 0,1,2,3 ] next
A = [ 0,1,2,3,4 ] next
A = [ 0,1,2,3,4,5 ] next
A = [ 0,1,2,3,4,5,0 ] next

Now you start to narrow the unique value list? So the last unique value is 5?

So I would say the last unique value is always the last value added to the array, unless, there is already a match in the array? Also if you find a match, you need to remove that value from the array so it can become unique again...

A = [ 0,1,2,3,4,5,0 ] remove 0, A = [ 1,2,3,4,5 ] next, last unique is 5
A = [ 1,2,3,4,5,0 ] next, now unique last unique is 0

Have I stated the logic correctly? There are other ways to keep the history, but the above is one easy way to do it. You could also keep an array of counters and increment the count for each match in the stream of values, and the lowest count is always the last unique unique value. But I think the above example is easier to understand and validate.

i'm newbee.
So i need to write another function that state this?
can you please give me more clue or example of function ?

If you want the value `L16POS119262020173530` ?