# Can't work out how to pull array from attrib's

**URL:** https://discourse.nodered.org/t/cant-work-out-how-to-pull-array-from-attribs/87057
**Category:** General
**Tags:** home-assistant
**Created:** [7 April 2024 13:01 UTC](https://discourse.nodered.org/t/cant-work-out-how-to-pull-array-from-attribs/87057 "2024-04-07T13:01:12Z")
**Posts on this page:** 5
**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: [7 April 2024 13:01 UTC](https://discourse.nodered.org/t/cant-work-out-how-to-pull-array-from-attribs/87057/1 "2024-04-07T13:01:12Z")

</div>

Hi all,

So I am stuck and can't seem to work out where to start on something, I have Octopus Agile, and I have the rates available to me in 30 minute blocks, I can get the data which are recorded as attributes as seen here:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/5/752b46cc06c89adbfa50646dae63801b96761812.png)

These are stored in msg.data.attributes, it looks like each price segment is it's own data entry, my goal is to parse them all and find the 3 lowest value entries and pass them on down the line, the issue I've got at the moment is parsing the dataset, I can't see to figure out a way to parse the entire attribute object in to an array to work it out, but I fear doing that it will have the date/time stamp there which will confuse the calculations in working out what is the cheapest time.

Anyone got any idea's please?

N/B the timestamps change so aren't constant so can't write a huge expression to calculate it them all.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [7 April 2024 13:10 UTC](https://discourse.nodered.org/t/cant-work-out-how-to-pull-array-from-attribs/87057/2 "2024-04-07T13:10:26Z")

</div>

There are a number of ways to approach this is one way: [Object.entries() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries)

Pass the `msg.whatever.attributes` object into `Object.entries` and you'll get an array of arrays (`[key, value]`)

---

<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: [7 April 2024 14:23 UTC](https://discourse.nodered.org/t/cant-work-out-how-to-pull-array-from-attribs/87057/3 "2024-04-07T14:23:48Z")

</div>

Cheers, I've read though that and I've managed to get it in to an array and find the lowest price, but it seems to kill the key so I can't see what time the price came from...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/278754c537bb99b8fca95c5f717f0920fbd5adb0.png)

```auto
[{"id":"f3dc28c4406d5034","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":140,"y":1420,"wires":[["a0ab059cf17c3de7"]]},{"id":"a0ab059cf17c3de7","type":"api-current-state","z":"4fbb0f02.0e5f6","name":"","server":"b3a3253e.d12568","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"octopusagile.all_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":370,"y":1420,"wires":[["81c05b0bba92579c","ca97de9e3d173bb1"]]},{"id":"81c05b0bba92579c","type":"debug","z":"4fbb0f02.0e5f6","name":"debug 11","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"data.attributes","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":1420,"wires":[]},{"id":"ca97de9e3d173bb1","type":"function","z":"4fbb0f02.0e5f6","name":"function 8","func":"\nconst obj = msg.data.attributes;\n//let arr = Object.values(obj);\nvar arr = Object.keys( obj ).map(function ( key ) { return obj[key]; });\nlet min = Math.min(...arr);\n//const map = new Map(Object.entries(obj));\n//console.log(map); // Map(2) {\"foo\" => \"bar\", \"baz\" => 42}\n\n\nmsg.prices=arr\nmsg.lowestprice=min\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":1460,"wires":[["77085d7a9acce152"]]},{"id":"77085d7a9acce152","type":"debug","z":"4fbb0f02.0e5f6","name":"debug Function out","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":1460,"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}]

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [7 April 2024 15:22 UTC](https://discourse.nodered.org/t/cant-work-out-how-to-pull-array-from-attribs/87057/4 "2024-04-07T15:22:14Z")

</div>

Hi, Steve mentioned Object.entries(), so you have an array of arrays containing timestamp and price, you can then just [sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) it. The first three indexes will be the lowest  
With `msg.payload[0][0]` holding timestamp of lowest price and `msg.payload[0][1]` holding the lowest price.  
e.g.

```auto
msg.data = {
    attributes: {
        one:3,
        two:4,
        three:1
    }
}
msg.payload = Object.entries(msg.data.attributes).sort((a,b) => a[1] -b[1]);

return msg;

```

Output

```auto
[
    [
        "three",
        1
    ],
    [
        "one",
        3
    ],
    [
        "two",
        4
    ]
]

```

---

<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: [6 July 2024 15:22 UTC](https://discourse.nodered.org/t/cant-work-out-how-to-pull-array-from-attribs/87057/5 "2024-07-06T15:22:48Z")

</div>

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