Search Array and return corresponding data?

Hello, relative newbie to Node-Red, but having followed the various tutorials and searched what I can on here I have a few pages up and running.

My latest issue, that I am hoping the collective can assit with, is the code for looking up an id number in an array and returning various corresponding values.

The msg object/array I receive from an API request is as follows:

{
  "label": "MiHome",
  "data": [
    {
      "id": 260882,
      "name": "UPS",
      "on": 1,
      "power": 116
    },
    {
      "id": 253105,
      "name": "PC",
      "on": 1,
      "power": null
    },
    {
      "id": 253110,
      "name": "TV",
      "on": 0,
      "power": null
    },
    {
      "id": 253593,
      "name": "Desk",
      "on": 1,
      "power": null
    },
    {
      "id": 257639,
      "name": "Printer",
      "on": 0,
      "power": 0
    },
    {
      "id": 260431,
      "name": "Fan",
      "on": 0,
      "power": null
    },
    {
      "id": 260448,
      "name": "Socket",
      "on": 0,
      "power": 0
    },
  ]
}

I want to then be able to lookup the ID number and return the on/off state (0 or 1) and the power reading (where applicable). I've tried searching the forum, and lokoing at the JSONata pages but can't seem to get anything to work?

It would be great if someone can point me in the right direction of the way to go about this?

Many thanks
Olly

An example using change node and JSONata expression

[{"id":"187967a.a95e618","type":"inject","z":"4484ee28.caa3c","name":"","props":[{"p":"payload"},{"p":"search","v":" 253105","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"label\":\"MiHome\",\"data\":[{\"id\":260882,\"name\":\"UPS\",\"on\":1,\"power\":116},{\"id\":253105,\"name\":\"PC\",\"on\":1,\"power\":null},{\"id\":253110,\"name\":\"TV\",\"on\":0,\"power\":null},{\"id\":253593,\"name\":\"Desk\",\"on\":1,\"power\":null},{\"id\":257639,\"name\":\"Printer\",\"on\":0,\"power\":0},{\"id\":260431,\"name\":\"Fan\",\"on\":0,\"power\":null},{\"id\":260448,\"name\":\"Socket\",\"on\":0,\"power\":0}]}","payloadType":"json","x":130,"y":500,"wires":[["4119a2a6.a337dc"]]},{"id":"4119a2a6.a337dc","type":"change","z":"4484ee28.caa3c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$filter(\t   payload.data,\t   function($v) {$v.id =  search }\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":500,"wires":[["269a25ff.79887a"]]},{"id":"269a25ff.79887a","type":"debug","z":"4484ee28.caa3c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":590,"y":360,"wires":[]}]

msg.payload holds the data.
msg.search holds the search id number.

1 Like

Thanks, I'll give that a go, much apprecaited

That seems to be working perfectly, many thanks - much appareciated.
I can then use funcitons to get the raw data out to plot/display.

Cheers

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