Extracting a Number from a specific line from a string

Hello, i have a "mqtt in" node that receives messages in the from a string from my smart meter. I only need one line from the message or more specifically the last numbers in that line. I have looked around on the forum and other sites and i have found people with similar problems but those solutions have not worked for me.

This is an example of the string:

{
	"Battery" : 255,
	"RSSI" : 12,
	"description" : "",
	"dtype" : "P1 Smart Meter",
	"hwid" : "12",
	"id" : "1",
	"idx" : 190,
	"name" : "Energy",
	"nvalue" : 0,
	"stype" : "Energy",
	"svalue1" : "34637147",
	"svalue2" : "38439648",
	"svalue3" : "72253",
	"svalue4" : "325054",
	"svalue5" : "1582",
	"svalue6" : "0",
	"unit" : 1
}

And this is the line i'm interested in.

	"svalue3" : "72253",

The number can be smaller or larger so this solution would not work for me. I hope there is someone around that knows of a solution, if i need to provide more info please ask.

The data are you have posted looks like JSON.

You can grab any value you want.

Thereā€™s a great page in the docs (Working with messages : Node-RED) 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

So in your case msg.payload.svalue3 will hold the value of interest.

If you are new to node-red, I recommend watching this 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.

1 Like

If you are using a MQTT-In node (in Node-RED) you could set the output to be a parsed JSON object then just extract the element you want with a function node or a change node.
parsed

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

Thank you so much! For some reason i put the output of the "mqtt in" node to string instead json after changing that i got the correct output. and together with the suggestion of dynamicdave i got it working just exactly as i was hoping.

1 Like

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