Convert string in array to value

Hello,

I am new to the forum. I'm Wesley, 38 years old and have been working on home automation for a while now. Recently I did an integration with node-red and the PLC. Everything is working well.

Now in Belgium the energy prices are rising enormously, there will be a capacity tariff. This means that you pay for the peaks that you cause.

I have extracted on a site the energy spot price per hour.
These are in a payload in an array

These values are "string" values separated by a comma. I have been searching for a week to convert these values to numbers. What have I already tried:

msg.payload = Number(msg.payload);
msg.payload = Number(msg.payload.vars["1"].value)
msg.payload = payload.replace(",", ".");

Nothing works :frowning:
(error NaN (not a number))

I have a value per hour, from this I want:
1: the average
2: the 5 highest values (hours are available in topic)

All thanks for the help

Hello Wesley,

your msg.payload is an array so in order to pick one element of that array and convert its nested payload to a number you have to do .. for example :

msg.payload = Number(msg.payload[1].payload)

the above is for converting the string of element in position 1

if you want to convert all your strings then :

msg.payload = msg.payload.map(el => Number(el.payload))

Your variable is in msg.payload[1]. payload

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.

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

1 Like

(Off the top of my head)...

I suspect the value will need to have the comma changed to a decimal point

msg.payload = msg.payload.map(el => Number(el.payload.replace(",",".")))
1 Like

opps .. missed that part .. you are right

Hello,

Thanks in advance for helping me think.

If I use
msg.payload = msg.payload.map(el => Number(el.payload))
then

do i use the "replace" then
TypeError: Cannot read property 'replace' of undefined

I collect the data as follows:

Thanks

after Steve's post, I adjusted the payload

one of your values will be invalid.

Proof

image

when the payload contains [ { payload: '1,2' }, { payload: '3,4' }, { payload: null } ] you get error Cannot read property 'replace' of null

You could filter the array first

const filtered = msg.payload.filter(e => (!!e && e.payload != null))
const converted = filtered.map(el => Number((el.payload + "").replace(",",".")))
msg.payload = converted;
return msg;

EDIT: updated ↑ function to handle situation when payload is already a number

If I retrieve everything without trying to convert I have the values below:


I may be wrong (don't have much node red experience yet) but I don't think I have any invalid values?

if i filter the array:

You have changed the data format. The code we offered won't work now.

Change code to...

const filtered = msg.payload.filter(e => e != null)
const converted = filtered.map(el => Number((el + "").replace(",",".")))
msg.payload = converted;
return msg;

No succes :frowning:

Ok copy and paste the payload (of good data) using the copy value button that appears under your mouse when you hover over the payload in the debug window.

surround your data with three backticks to prevent corruption...
```
like this
```

See this post for more details - How to share code or flow json

We are only seeing part of your Function node code with that screenshot ..
did you change msg.payload further up or is msg.payload arriving at this Function as you shared in your 1st post ?

can you share the Flow instead of images ?

Here is proof if you send an array of strings in msg.payload it does work...

image

and here is proof in node-red...

Demo flow...

[{"id":"3dc90c37ee504bc4","type":"function","z":"eb81e71f9edd709c","name":"filter and convert","func":"\nconst filtered = msg.payload.filter(e => e != null)\nconst converted = filtered.map(el => Number((el + \"\").replace(\",\", \".\")))\nmsg.payload = converted;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1700,"y":400,"wires":[["47f532c32e1e1cc0"]]},{"id":"f8a71f9cef66afb7","type":"inject","z":"eb81e71f9edd709c","name":"your data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"192,36\",\"225,00\",\"213,72\"]","payloadType":"json","x":1480,"y":400,"wires":[["3dc90c37ee504bc4","96e556112cd78945"]]},{"id":"96e556112cd78945","type":"debug","z":"eb81e71f9edd709c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1560,"y":440,"wires":[]},{"id":"47f532c32e1e1cc0","type":"debug","z":"eb81e71f9edd709c","name":"result","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1850,"y":440,"wires":[]}]

Hello,

Verry strange, now it's working :smiley:
thanks a lot!!!!!

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