Newbi needs help with a string

Hi,
Please bear with me, I'm a total stranger with this. I cannot seem to find my answer with good old google.

I'm trying to change the string from my fronius inverter to something that Domoticz is willing to understand, so a json string.

Here is what I start with:

{"DAY_ENERGY":{"Unit":"Wh","Value":40110},"DeviceStatus":{"ErrorCode":0,"LEDColor":2,"LEDState":0,"MgmtTimerRemainingTime":-1,"StateToReset":false,"StatusCode":7},"FAC":{"Unit":"Hz","Value":49.99},"IAC":{"Unit":"A","Value":0.22},"IDC":{"Unit":"A","Value":0.24},"PAC":{"Unit":"W","Value":41},"TOTAL_ENERGY":{"Unit":"Wh","Value":6892709.5},"UAC":{"Unit":"V","Value":229.9},"UDC":{"Unit":"V","Value":297.8},"YEAR_ENERGY":{"Unit":"Wh","Value":1605788.38}}

That is what comes out of the fronius-node. Now I am interested in a few values.

ENERGY_DAY, PAC, YEAR_ENERGY and TOTAL_ENERGY. So I started filtering out values with msg.payload[0] (for the ENERGY_DAY), msg.payload[6] (PAC), msg.payload[7] (TOTAL) and msg.payload[10] (YEAR) energy

But I found that msg.payload[0] isn't ENERGY_DAY and all the values (like msg.payload[0].Value for today's total) but '{' .

I know I'm missing something, starting with knowledge to all this. Who is willing to help me? I'm clueless in this one. I'm willing to share my project if I can get it to work. Which I think someone has already done and I'm inventing the wheel again. But I can't find it. And yes there is a plugin for domoticz to work with the fronius inverter, but it is horrible unstable in my setup. So I thought let's go with node red...

Have a look in the debug window on the right hand side - it provides an object browser so you can see exactly what the full "path" is to each node in the string

So bring in your value, and output to a debug node - then drill down in the debug window and see what you get

Post up some screenshots of this and your results

Craig

to expand on what @craigcurtin said. You are receiving a JSON string it looks like. This is a string representation of a Javascript object. First thing in question is did you already parse it to javascript object?
If it comes out of the first node in exactly this format you posted above you will have to do this to be able to work with it.
You can simply use the JSON node that you can find in the Palette on the left to do this.
Secondly you have a javascript object of type object but msg.payload[0] is a way to access a property of an object of type array.
So have a look at https://nodered.org/docs/user-guide/messages
as this will give you all the information needed on what object types their are and understanding what you see in the debug window and how to work with it.

Johannes

Thank you guys for the info.

I knew I had something wrong with the msg.payload[0] and [1]. But I get it , I have to get the JSON into an array to get that working. Makes sense now, feeling a bit stupid :slight_smile: , but everybody has strong points and weak points.

These are the nodes and debug window:

The string as I posted it is from the debug window attached to fronius-control-in. Hope you can help! Like to learn this (a bit).

I installed the fronius-control-in node in the settings.

can you show us a picture of what comes straight out of the fronius node when you connect a debug to it? Maybe you dont need the json node.
And think again, its not an array. There is a difference between an array and an object. You will have to work with object keys and not array indexes. Its all in the link I posted to the documentation.

Edit: just to clarify is the output we see in the debug window from the debug attached to the json node or is this from when the debug was connected directly to the fronius node?

This is the output then in the debug window.
debug-2

and expanded:

I'm sorry, my thoughts on the msg.payload[0] were in general. Not for this string, this one first needs to be json to work with it.

To the right is where I click to see the debug messages. Do correct me if I'm wrong.

No this is already an object you can work with no json node needed. Please read the section on understanding the structure of a message here https://nodered.org/docs/user-guide/messages
When you have done this you should have all the tools to figure it out.
But to repeat it:
You have a object and not an array your working with. So msg.payload[0] cant work as its an array reference. You will have to use something like msg.payload.DAY_ENERGY. Objects have keys and arrays have indexes.
If you hover over one of those in the debug tab like DAY_ENERGY it will give you little icons on the side and one of them will give you this path.
This is all explained in the documentation i posted the link to including a link to further information about javascript object types https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures.

Thanx for pointing me in the right direction! I will do that.

Thank you so much! Got it working with a change node. Now I can add the needed info into the original object! So simple....

1 Like

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