Remove "{ }" from payload

Probaly very easy but I have put several housr trying to get his working.
I am using Weather-Display to publish weather data to my Mosquitto.
I got this now when doing debug.

WeatherDisplay/temp2 : msg.payload : string[6]

"{12.8}"

I want the payload to be just 12.8 so I can put that value in my MYSQL database.

Please help !

Have you thought about using a function node to set msg.payload to a substring starting at position 1 (it’s 0-indexed), and ending at the length of the payload - 1?

Thanks for a fast answer !
I am new to Node red so I need a little more help
Adding Funtion to flow is no problem but how to write the function is worse
image

The function node uses JavaScript to work around the problem programmatically. For this you need basic knowledge of javascript, and a small amount of knowledge on how Node-RED works. Check the following links from the node-red docs: working with messages and using the function node.

Beyond that, take a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

If you follow the concepts described in the working with messages and function node documentation, you should be able to work out the rest with the Mozilla Developer Network link. I'm not going to spoil the answer for you, but you should get it done with 5 lines of code at most. If you work this part out yourself, you can do most basics related to it without (much) help in the future :slight_smile:

2 Likes

Thanks, will read those pages and see if I understand more.
But isnt there anything in Node-Red that can do that witout any programming skills ?

You can use JSONata syntax in a change node to do the same, setting a substring of that outer string that way. But that requires learning and programming in the JSONata syntax. Here's an example posted to another topic: Function String Extract - #5 by zenofmud

Or you could use node-red-contrib-string I think, but it is horrendous overkill. If you want to get the best out of node-red you will need some basic javascript.

You can use the Change node to remove { and }

The quotes indicate it's a string rather than a number (as you can see it only has 6 characters)

Set the change node to "change" and then search and replace...

2 Likes

So there's three options on how to do this.... I just put together a sample flow to show they all give exactly the same result.

[{"id":"30adbf39.74344","type":"inject","z":"6d1914f5.165c5c","name":"","topic":"","payload":"{12.8}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":126.5,"y":177,"wires":[["605798e1.c64338","cbff28e3.cce978","f941b127.ec0f6"]]},{"id":"99637a8d.d5d28","type":"debug","z":"6d1914f5.165c5c","name":"JSONata output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":580.5,"y":178,"wires":[]},{"id":"605798e1.c64338","type":"function","z":"6d1914f5.165c5c","name":"With a function node","func":"msg.payload = msg.payload.substring(1, msg.payload.length - 1)\nreturn msg;","outputs":1,"noerr":0,"x":326.5,"y":239,"wires":[["55e0f225.0010e4"]]},{"id":"55e0f225.0010e4","type":"debug","z":"6d1914f5.165c5c","name":"Function node output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":597.5,"y":239,"wires":[]},{"id":"cbff28e3.cce978","type":"change","z":"6d1914f5.165c5c","name":"With JSONata","rules":[{"t":"set","p":"payload","pt":"msg","to":"$substring(payload, 1, $length(payload) - 2)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":308.5,"y":177,"wires":[["99637a8d.d5d28"]]},{"id":"f941b127.ec0f6","type":"change","z":"6d1914f5.165c5c","name":"With change node/search & replace","rules":[{"t":"change","p":"payload","pt":"msg","from":"{","fromt":"str","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"}","fromt":"str","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":310.5,"y":335,"wires":[["39e2d6eb.e6e86a"]]},{"id":"39e2d6eb.e6e86a","type":"debug","z":"6d1914f5.165c5c","name":"Change node search/replace output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":661.5,"y":336,"wires":[]}]
1 Like

Maybe not the best way to do but with my minimal programming skills it solved the problem

I think I understand the solutions presented here, but I'm wondering why my solution in a function block does not work:

var payLoad = msg.payload;
payLoad.replace("}}", "");
payLoad.replace('{', '');
var timeLeft = payLoad.split(':');
msg.payload = timeLeft[3];
return msg;

What does your payload contain & where does it come from (MQTT? HTTP request? Etc?)

Can you screenshot the debug message?

(You can paste a screen snip directly into a reply)

I have an mqtt input node, with the topic: stat/tasmota/irrigation/RESULT
The upper portion of this debug window is the unmodified data (before my function node), the lower portion is after my function node.
image

As I suspected, the payload is JSON & therefore can be parsed into a proper object.

I.e, there is zero need to do any string splitting or manipulation.

Set your MQTT in node to return a parsed object.

Then...

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

Oh man, that is so simple! Thank you.