Help with writing and understanding function for setting payload from an json

Hi

I got an json ( https://pollenkontroll.no/api/middleware/pollen/b5bb4856-2117-433d-bf18-53504ef2f101?t=1623043832177 ) that i want to sort and set status on sensors in Home assistant. Still struggling with function nodes and how to grab data and post it as payload.

I don't know if it's easier to putt all data to one sensor, and then use a template sensor in HA to set state, or to write it directly in NR to sensors with parameter and set state for today an an attribute for the tomorrow value.

It's the "name" and the level_description for today date and tomorrow that i need

data from my http request node:

[{"date":"2021-06-07","allergens":[{"name":"Salix","latin_name":"Salix","level_number":2,"level_description":"Low"},{"name":"Bjørk","latin_name":"Betula","level_number":2,"level_description":"Low"},{"name":"Or","latin_name":"","level_number":0,"level_description":"None"},{"name":"Hassel","latin_name":"Corylus","level_number":0,"level_description":"None"},{"name":"Gress","latin_name":"Gramineae","level_number":2,"level_description":"Low"},{"name":"Burot","latin_name":"Artemisia","level_number":0,"level_description":"None"}]},

{"date":"2021-06-08","allergens":[{"name":"Salix","latin_name":"Salix","level_number":2,"level_description":"Low"},{"name":"Bjørk","latin_name":"Betula","level_number":0,"level_description":"None"},{"name":"Or","latin_name":"","level_number":0,"level_description":"None"},{"name":"Hassel","latin_name":"Corylus","level_number":0,"level_description":"None"},{"name":"Gress","latin_name":"Gramineae","level_number":2,"level_description":"Low"},{"name":"Burot","latin_name":"Artemisia","level_number":0,"level_description":"None"}]}]

Andrew, as it was a HTTP Request, those values are actually inside payload (the op has copied the payload not the msg i.e. its not msg.date but actually msg.payload.date)

To use the values in a function node, there’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Just remember to prefix the path with msg. in a function. e.g. if you "copy path" of the date item & paste into a function it will paste payload.date - in a function you would use msg.payload.date

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

https://nodered.org/docs/user-guide/messages

if you want the date in msg.payload then use a change node to set msg.payload to msg.payload.date.
in a function that would be

msg.payload = msg.payload.date;
return msg;
1 Like

Thanks. i understand this part, but struggling on sorting the data in an non complex way

I want to re-sort it, under forecast i got 2 array (for today and tomorrow). and under them again i got 5.

So thats why im wondering about an function for setting new payload sorted on the name
Date today:
payload.forecast[0].allergens[0].name
payload.forecast[0].allergens[0].level_description
and from equal name in array 1
payload.forecast[1].allergens[0].level_description, so that i then can set the values to sensors in Home Assistant

I dont follow. The data you posted is already sorted by date. The first entry is "2021-06-07" the second entry is "2021-06-08"

Try a different approach - what would you like to see in the outgoing payload - an object with all values? a specific value? How many msgs do you want - 1 msg with the 2 days or 2 messages with 1 days values?

Its sorted by dates, i want payload sorted by name. 1 message pr. name with the 2 values of level_description for today and tomorrow

was this more understanding?

not really. level_description is present 6 times in both array elements.

lets try again...

Using this data...

[
  {
    "date": "2021-06-07",
    "allergens": [
      {
        "name": "Salix",
        "latin_name": "Salix",
        "level_number": 2,
        "level_description": "Low"
      },
      {
        "name": "Bjørk",
        "latin_name": "Betula",
        "level_number": 2,
        "level_description": "Low"
      },
      {
        "name": "Or",
        "latin_name": "",
        "level_number": 0,
        "level_description": "None"
      },
      {
        "name": "Hassel",
        "latin_name": "Corylus",
        "level_number": 0,
        "level_description": "None"
      },
      {
        "name": "Gress",
        "latin_name": "Gramineae",
        "level_number": 2,
        "level_description": "Low"
      },
      {
        "name": "Burot",
        "latin_name": "Artemisia",
        "level_number": 0,
        "level_description": "None"
      }
    ]
  },
  {
    "date": "2021-06-08",
    "allergens": [
      {
        "name": "Salix",
        "latin_name": "Salix",
        "level_number": 2,
        "level_description": "Low"
      },
      {
        "name": "Bjørk",
        "latin_name": "Betula",
        "level_number": 0,
        "level_description": "None"
      },
      {
        "name": "Or",
        "latin_name": "",
        "level_number": 0,
        "level_description": "None"
      },
      {
        "name": "Hassel",
        "latin_name": "Corylus",
        "level_number": 0,
        "level_description": "None"
      },
      {
        "name": "Gress",
        "latin_name": "Gramineae",
        "level_number": 2,
        "level_description": "Low"
      },
      {
        "name": "Burot",
        "latin_name": "Artemisia",
        "level_number": 0,
        "level_description": "None"
      }
    ]
  }
]
  1. how many messages will be generated?
  2. What exact part of the above data will be inside the msg payload?
  3. should the topic be set?

Ok. let me try to describe it as an userstorry then

For my Home assistant system, i want to read the json data in " https://pollenkontroll.no/api/middleware/pollen/b5bb4856-2117-433d-bf18-53504ef2f101?t=1623043832177 " and set the state (for today)and the attribute next day for my 6 pollen sensors (with same name) in Home assistant.

I was reading before i wrote my post about function nodes (and used it for sorting first dates in an json ones before) and though that it maybe was an go solution for creating an payload that i can use for setting the state and attribute.

But perhaps there is an easier way?

Maybe, but until I understand your requirement it is hard to tell. Not many of us here know HA and none of us know what you NEED to send to it - so unless you can provide clear information about how you want the data from the API converted to 1 or more msg & what should be in the payload/topic, its impossible to say.

Try to answer the questions I posted.

Ok
1: 1 message pr name (so 6 messages)
2: the payload from "level_description" on date today and tomorrow
3: topic = name

Example:
Topic: "Bjørk" (from name)
state: "Low" (form level_description date today)
next_state: "None" (from level_description date tomorrow)

Then i can sort on topic and handel the values to home assistant

Thats better.

This demo should get you close to a solution...

[{"id":"530e9a02b0a3e43a","type":"inject","z":"553814a2.1248ec","name":"your http data (simulated)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"date\":\"2021-06-07\",\"allergens\":[{\"name\":\"Salix\",\"latin_name\":\"Salix\",\"level_number\":2,\"level_description\":\"Low\"},{\"name\":\"Bjørk\",\"latin_name\":\"Betula\",\"level_number\":2,\"level_description\":\"Low\"},{\"name\":\"Or\",\"latin_name\":\"\",\"level_number\":0,\"level_description\":\"None\"},{\"name\":\"Hassel\",\"latin_name\":\"Corylus\",\"level_number\":0,\"level_description\":\"None\"},{\"name\":\"Gress\",\"latin_name\":\"Gramineae\",\"level_number\":2,\"level_description\":\"Low\"},{\"name\":\"Burot\",\"latin_name\":\"Artemisia\",\"level_number\":0,\"level_description\":\"None\"}]},  {\"date\":\"2021-06-08\",\"allergens\":[{\"name\":\"Salix\",\"latin_name\":\"Salix\",\"level_number\":2,\"level_description\":\"Low\"},{\"name\":\"Bjørk\",\"latin_name\":\"Betula\",\"level_number\":0,\"level_description\":\"None\"},{\"name\":\"Or\",\"latin_name\":\"\",\"level_number\":0,\"level_description\":\"None\"},{\"name\":\"Hassel\",\"latin_name\":\"Corylus\",\"level_number\":0,\"level_description\":\"None\"},{\"name\":\"Gress\",\"latin_name\":\"Gramineae\",\"level_number\":2,\"level_description\":\"Low\"},{\"name\":\"Burot\",\"latin_name\":\"Artemisia\",\"level_number\":0,\"level_description\":\"None\"}]}]","payloadType":"json","x":260,"y":1500,"wires":[["4cc6f6619479498d","64eff4cec1935cce"]]},{"id":"4cc6f6619479498d","type":"function","z":"553814a2.1248ec","name":"","func":"/*\ntopic: \"Bjørk\"(from name)\nstate: \"Low\"(form level_description date today)\nnext_state: \"None\"(from level_description date tomorrow)\n*/\nvar data = msg.payload;\nvar allergens = data[0].allergens;\nvar allergensNext = data[1].allergens;\nfor (let index = 0; index < allergens.length; index++) {\n    const current = allergens[index];\n    const next = allergensNext.find(al=>al.name==current.name)\n    const m = {\n        topic: current.name,\n        payload: {\n            state_date: data[0].date,\n            state: current.level_description,\n            next_state: next ? next.level_description : \"\",\n            next_state_date: data[1].date || \"\"\n        }\n    }\n    node.send(m);\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":1560,"wires":[["84a73a79a45ded69"]]},{"id":"84a73a79a45ded69","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":474.8958435058594,"y":1567.111083984375,"wires":[]},{"id":"64eff4cec1935cce","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":505.8958435058594,"y":1503.111083984375,"wires":[]}]
1 Like

Thanks for helping :slight_smile:

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