Retrieve a object comming from MQTT

Hi
I'm dead sure it's because I'm newbie, I hope someone will help me out here - here is my problem:

I'm trying to calculate where if have power to start other things in the house, this is done by looking at the value: (payload.data.PO - payload.data.P) comming on MQTT, this part works.

My problem is in the node: "Calculate power diff" here I keep getting the error: TypeError: Cannot read properties of undefined (reading 'data')

My code is:

var value = msg.payload["power"] 
msg.payload = value.data.PO - value.data.P 
return msg;

You can see the values from the picture below:

Thank you for your help

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


in short var value = msg.payload["power"] means value is now pointing to msg.payload.power and value.data.PO is pointing to msg.payload.power.data.PO

image

But again - use the tools provided to avoid simple mistakes (use the "Copy Path" button)

Thank you, but that is what I have done:/
Hence I need help!!

Clearly you have not (or clicked something else further off screen) - where did msg.payload["power"] come from - its not in that image.

Try again - hover over the item of interest (e.g. PO) and click the "copy path" item. When you paste it in a function you need to suffix it with msg. e.g. msg.payload.x.y.x

Hi
Thank you, you are absolutly correct,- I've owe you a beer!

/Henrik

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