Trouble with working through the Node Red tutorials

I have been working through the Node Red tutorials and got to the point of building arrays with the Star Wars data from the site, “people”: “https://swapi.info/api/people”. I have then followed the tutorial to convert a text label “height” value from “172” to a numerical value of 172.

The problem comes from referencing the correct part of the array. I seem only to be able to reference on record [0] of the array which then only changes the height value of one record. When I try to move up a level it gives me the error message - “Invalid JSONata expression: Argument 1 of function “number” does not match function signature”.

Hi & welcome to the forum.

I dont know which tutorial you are refering to but for arrays of data, you need to look at the split and join nodes.

You pass the msg (the one with the payload array) to a split node - that then splits the payload into x number of sequential messages - each containing the 1 element of the payload array. You can then operate on msg.payload.height as required. Then you pass them back into a join node to re-create the original array.

http-request → split change → join → debug

See the build in exampes (CTRL-I --> Examples)


My advice - avoid JSONata expressions until you get up-to-speed with Node-RED (J expressions are much less commonly used and only a handful of crazy folk know more than the basics :wink: )

Hi Steve, Thanks for your help. The tutorial is actually on Loops as shown on the screen shot below:-

As you can see below I have been able to write the code to change the array value [0] for the records "height" and "mass" to numerical values but my code only works on record (or line) 0 rather than filing through the whole array. When I take the [0] reference out of the code it gives me the error message i stated above.

Example of applying a function to all height properties in an array, using JSONata transform operator. Other Operators · JSONata
e.g

[{"id":"960e96e5343522c2","type":"inject","z":"613df62afc8a16bf","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"height\":\"123\",\"other\":\"any\"},{\"height\":\"456\",\"other\":\"more\"}]","payloadType":"json","x":155,"y":720,"wires":[["3318ae36c068cdd2"]]},{"id":"3318ae36c068cdd2","type":"change","z":"613df62afc8a16bf","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload ~> |$|{\"height\": $number($.height)}|","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":720,"wires":[["8abc35ac1c8fcbdc"]]},{"id":"8abc35ac1c8fcbdc","type":"debug","z":"613df62afc8a16bf","name":"debug 18","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":525,"y":720,"wires":[]}]

But if you are splitting the array you can use your OG example on each object after the split node also.

If i remember correctly, in a jsonata expression you refer to payload rather than msg.payload

That said, I agree with @Steve-Mcl regarding Jsonata (ie the J: expression in your change node)
The learning curve for javascript in a function node is gentler than that for Jsonata.

You can use msg as JSONata will go into compatibility mode. But it is best to avoid using msg and use $$ instead, or omit $$ but this may bite you later in map loops and more complicate JSONata expressions.

Hello Steve, once again thanks for your input. I've had a couple of other suggestion but sadly don't understand what they're talking about so I'm going to skip that section and move on! My ultimate goal is to be able to learn enough Node Red in order to write a series of flows to allow me to manage my Victron solar and battery system.
I want to be able to pull in the forecast solar hours for tomorrow and use this to alter the target limit of charge on my batteries.
This so so that when I have a sunny day my batteries have some spare capacity to store the sun's free energy. I don't have authority to send to the grid because I designed and installed the system myself and didn't MCS it.
If you have any experience of this I'd be delighted to hear it.
Please find enclosed a screen shot of my system today. I manually limited the charging level to 80% last night so that today's sun's energy could be stored and used later.

As you can see its been a sunny day here in Tynemouth so I've stored that energy and won't need to refill the batteries with as much grid electricity tonight when they schedule charge at the cheap rate.

Which tutorials are you working through? Can you post a link to them please?

Have you watched the official node-red beginners playlist? Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Hi Colin,

This is a link to the Node Red academy:

Kind regards,

Richard.

Note that that is nothing to do with the node red core developers, as far as I know.

It is Colin, it is an initiative from FlowFuse.