Remove “{"power":00000}” from payload

I need to change the following MQTT output from

{"power":3463}

to

3463

Where the 3463 is a changing number after reading a power sensor. So it could be 0 up to 72000.

Reading the forum read this can be done in several ways so I started with the change node and

$substring(payload,6,11)

Changed to

$substring(payload,1,2,3,4,5,6,7,8,9,14)

But I seem to have this wrong? Any advice please?

That payload is a valid JSON string. That means you can pass it to a JSON node which will convert it to the JavaScript object equivalent. That allows you to address the individual properties of the object directly.

Then you will be able to use a Change node to set msg.payload to the value of msg.payload.power.

There's a page in the docs that explains some of these ideas: https://nodered.org/docs/user-guide/messages

In fact if, in the mqtt node settings, you select the Output as Parsed JSON, then the node will automatically convert it from JSON to a javascript object, which you can feed straight into the Change node. However you may not even need to do that. What are you going to do with it after you have passed on the message with it in the payload.

Thanks ok have got working with the change node. I have built a ESP32 Smart Power Meter from GitHub. Issue is I cant figure how to slow down the MQTT state changes as they are very sec and this is loading my PI running Home Assistant. I use Node Red on this PI and want to delay (added a delay node/limiting) the Power reading to 1 every 30s. So I created a NR MQTT flow and now want to end up with just the numeric numbers as they change but 1 reading every 30s.

You can use the Delay node to do this. Configure it to be in "rate limiting" mode and pick the rate for the messages. Make sure you click the "drop intermediate messages" box.

https://cookbook.nodered.org/basic/rate-limit-messages

Thanks sorry I have completed that its working. I need now to only have the returned message as a numeric number example 3463 from {"power":3463} with the understanding that the number can range from 0 to 7200.

I'm gonna guess you didn't read & absorb the link posted twice by 2 people.

{"power":3463} is JSON == JavaScript object notation.

Pass the data to a json node then you WILL get the value as a number. Because it is a number.

This video cleanly explains what we were saying

Dropping 29 out of 30 samples is not that nice.
You can miss vital information. What if sample 18-23 is a current peak?

Better feed the 30 samples in the calculator node and release the mean value after 30 samples. You get then 1 output in 30 sec with the average of the last 30sec.

node-red-contrib-calculate

Sorry I'm on my tablet and can't give you a example now.

Nice added the palette and this is averaging reading every 30s so thank you.

1 Like

Passing it to a JSON node gives me the following 4002 but in the debug is power:4002 and the flow debug reports Topic removed, non numerical value found. if I copy the path I see payload.power and the value is 4002 but the flow reports non numerical value found. Watched the video and I still dont see what I am doing wrong?

Without a screenshot or your flow it is difficult to tell but I suspect where ever you're sending it to wants the value to be in payload. So add a change node after the JSON node, set msg.payload to msg.palyload.power then send it to where it needs to go.

Ps, that video is part of a series. They are nice short 2m easily digestible & hugely beneficial to watch. Honestly, invest 20mins watching the series. All of what I said above just clicks into place.

Top flow works with change node
Bottom flow with delay / json node path payload and value is 3671 which is correct. So I want to replace the change node with the json which throws the error of non numerical value found which I suspect is power: portion. Will also watch the videos...

So pretty much exactly as I said...

Thanks its now working. Whats the best way to change payload 66.0909090909091 limit to four numbers so its 66.09?

Well if it's a number you can use toFixed or round

Some good info here

You can use a change node with a jsonata round function. This gives you the option to round it to any precision you would like just with a change node and no function or additional node needed.

If you want to round it for display in the dashboard that is better done in the ui widget itself. You can use
{{value | number:2}}

1 Like

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