# Array selection help - Selecting last item in array

**URL:** <https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496>\
**Category:** General\
**Created:** [31 October 2023 14:51 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496 "2023-10-31T14:51:05Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![XcOM987](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xcom987/32/58677_2.png) [@XcOM987](https://discourse.nodered.org/u/XcOM987)\
**Post date:** [31 October 2023 14:51 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/1 "2023-10-31T14:51:05Z")

</div>

Hi,

I am trying to pull an array from a home assistant sensors attribute, what I want to do is just pass along the last array entry, the issue is the array naming structure is different every time:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/0/e001088a0d63ae408e15d71cffa8672df49c74e2.png)

```auto
[{"id":"8617395361a98887","type":"inject","z":"4fbb0f02.0e5f6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":1240,"wires":[["2da2ba8af5dd5e7a"]]},{"id":"2da2ba8af5dd5e7a","type":"api-current-state","z":"4fbb0f02.0e5f6","name":"Rates","server":"b3a3253e.d12568","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"octopusagile.rates","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":290,"y":1240,"wires":[["2f73f4624b193e82","5f25a119a3c6af29","edeb3970e07530dc"]]},{"id":"f805ddc9be792893","type":"debug","z":"4fbb0f02.0e5f6","name":"Rates","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":1200,"wires":[]},{"id":"5f25a119a3c6af29","type":"function","z":"4fbb0f02.0e5f6","name":"function 11","func":"\n\nconst x = msg.data.attributes[-1]\nmsg.payload = x\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":1240,"wires":[["f805ddc9be792893"]]},{"id":"edeb3970e07530dc","type":"debug","z":"4fbb0f02.0e5f6","name":"Rates","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":750,"y":1240,"wires":[]},{"id":"b3a3253e.d12568","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

```

My ideal solution is read the last attribute entry and write it to an input helper, at the moment I am just parsing to a debug node before I start sending the entry elsewhere for use, I've had a nosie and found a few similar things, but none of them have worked and/or were unrelated just enough to not be what I am after.

Can anyone point me in a general direction at all please?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [31 October 2023 15:07 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/2 "2023-10-31T15:07:26Z")

</div>

Do you mean msg.data.attributes? It's an object not an array.

What do you mean by the last item - in your example is it 18.06 at 22:30 or 22.512 at 14:30?

---

<div class="post-metadata">

**Author:** ![XcOM987](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xcom987/32/58677_2.png) [@XcOM987](https://discourse.nodered.org/u/XcOM987)\
**Post date:** [31 October 2023 15:11 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/3 "2023-10-31T15:11:40Z")

</div>

Ahh yes, it's an object sorry, the attrib treats it like an array, I think I can convert it to an array though by splitting and joining it.

But yes, in my example it will be 14:30, but that will change every 30 minutes, basically I want to pull the last entry every 30 minutes.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 October 2023 15:37 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/4 "2023-10-31T15:37:58Z")

</div>

> [@XcOM987](#):
>
> in my example it will be 14:30,

Even though that is the earliest timestamp, not the most recent?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [31 October 2023 15:43 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/5 "2023-10-31T15:43:44Z")

</div>

Maybe this will work?

```auto
msg.payload = msg.data[Object.keys(msg.data)[Object.keys(msg.data).length - 1]]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 October 2023 16:04 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/6 "2023-10-31T16:04:20Z")

</div>

Generally you cannot rely on the ordering of properties of an object, so possibly the keys should be sorted before taking the first one.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [31 October 2023 16:12 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/7 "2023-10-31T16:12:15Z")

</div>

I thought it might need sorting too, but the internet claims that more recent js (since 2015) preserves the insertion order as long as the keys are not integers?

Certainly can't hurt to sort.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [31 October 2023 16:19 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/8 "2023-10-31T16:19:38Z")

</div>

If I'm not mistaken, that object holds prices for some period, where every item is price which lasts 30 minutes.

You'r intention is to get the last one but that is not fail safe at all.  
To get good advise it takes to know what that object actually holds. Is it price list of historical data where youngest item is most recent (maybe even current) or is it look-ahead list.  
Then specify your target in terms of time what is your intention. Should it be price which is valid for current moment or for next half of hour or something else.  
And then it also depends on timing, when you call the function to get the price of your interest.

So if you can make your intention more clear in all terms above, it'll be fine to figure out best solution.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 October 2023 16:27 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/9 "2023-10-31T16:27:35Z")

</div>

> [@jbudd](#):
>
> the internet claims that more recent js (since 2015) preserves the insertion order as long as the keys are not integers?

I think ES6 became standard in nodejs with nodejs v14, which is why node red 3 requires at least v14. There are still many users with earlier versions of nodejs though.

However, in this example, how does one know that the insertion order will always be the same?

---

<div class="post-metadata">

**Author:** ![XcOM987](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xcom987/32/58677_2.png) [@XcOM987](https://discourse.nodered.org/u/XcOM987)\
**Post date:** [31 October 2023 16:38 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/10 "2023-10-31T16:38:15Z")

</div>

Apologies, should have stated my intentions.

Basically Octopus Agile is the pricing structure for an energy plan where the prices change every 30 minutes, the prices are set during the day, the sensor updates every 30 minutes and the attributes are updated.

Generally I've always seen them ordered in that way, top to bottom furthest in the future to most recent at the bottom (Last) where the last entry is the 30 minute window that you are currently in.

They hold the price per Kw in pence, the repo for the service has been dead now for a few years, and people suggest using the newer one, but that only shows you the rates for the half hour you are in if you are on the Agile plan.

What I want to use is get the data myself and use it to workout if it would be cheaper to move over to it.

The rates sensor seems reliable but the current rate sensor doesn't update, its a known bug and I can't see a fix for it, figured I'd be able to use Node Red to parse the data in to an input helper for me to use as I see fit.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 October 2023 16:41 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/11 "2023-10-31T16:41:15Z")

</div>

In that case, provided you are using nodejs at least version 14, something like @jbudd's solution should work. If you want to be absolutely certain that you get the oldest timestamp then sort the keys and take the first one.

---

<div class="post-metadata">

**Author:** ![XcOM987](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xcom987/32/58677_2.png) [@XcOM987](https://discourse.nodered.org/u/XcOM987)\
**Post date:** [31 October 2023 16:43 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/12 "2023-10-31T16:43:39Z")

</div>

Just tried, this, amended it to the object in question and seems to be working a treat for now.

Many thanks all for the input and help, I will test it for 24 hours and see how it behaves.

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [31 October 2023 18:07 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/13 "2023-10-31T18:07:47Z")

</div>

Octopus have an API where you can query Agile prices!

---

<div class="post-metadata">

**Author:** ![XcOM987](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xcom987/32/58677_2.png) [@XcOM987](https://discourse.nodered.org/u/XcOM987)\
**Post date:** [31 October 2023 18:35 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/14 "2023-10-31T18:35:48Z")

</div>

Yea I did have a play, if this doesn't work I will use the API, I used it in the past and it can be a bit of a mess

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [31 October 2023 20:15 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/15 "2023-10-31T20:15:46Z")

</div>

> [@XcOM987](#):
>
> I used it in the past and it can be a bit of a mess

The API provides accurate data. What do you find "messy"?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [30 December 2023 20:15 UTC](https://discourse.nodered.org/t/array-selection-help-selecting-last-item-in-array/82496/16 "2023-12-30T20:15:54Z")

</div>

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