I am getting "Humidity: 64.40%, Temperature: 32.00 Celsius" which is in string from my serial port. I wish to change it to key value data. This is so that I can do gauge for humidity and temperature separately. Do advice. Do I have to use function node or some other node. I am not familiar with function node.
This should work in a function node:
const input = msg.payload.split(",")
msg.payload = {}
msg.payload.humidity = parseFloat(input[0].split(":")[1])
msg.payload.temperature = parseFloat(input[1].split(":")[1])
return msg
output:
{"payload":{"humidity":64.4,"temperature":32}}
In the gauge node you can now use {{payload.humidity}}
The gauge node requires a number input, so the % and Celsius was removed.
Thank you sir. It really works. Can I know what does the second and third line means? what is msg.payload = {}? I understand the first third line but how do you know the input is 0 or 1 then you split into [1]. I do not understand this particular part. If I can understand, I can try to do on my own in near future for other device also.
The input is "Humidity: 64.40%, Temperature: 32.00 Celsius"
const input = msg.payload.split(",")
will create an array of 2 elements, each with their own string:
0: "Humidity: 64.40%"
1: "Temperature: 32.00 Celsius"
These strings can in turn be split by the ":" symbol
So input[0]
= the humidity string, split the string by ":" and you get another array:
0:"Humidity"
1: "64.40%"
We create a new msg.payload = {}
object
We assign a new property "humidity": msg.payload.humidity
and assign the value:
input[0].split(":")[1]
= "64.40%"
I hope this somewhat clarifies.
Thank you sir. I understand now. If I wish to insert chart node, must I use switch node after the function to separate the humidity data and temperature data? Because like Gauge node, we can just use payload.temperature but in chart node, I do not think we can right? Btw thanks for guiding me for the previous question.
A Change node, not a Switch node. You are correct in that the Chart must have the value in the payload and can have a line label in the topic. This is so that multiple lines can be plotted on one chart.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.