Noob Q: What's the code to access this data?

I'm a Java noob and I have spent an hour googling to try to figure out how I access the value 125.5 in a payload represented below. I tried payload.solar_power.value and payload.solar_power['value'] but no luck. I don't know what to search for to find the answer or I wouldn't be asking.

Note: this is part of the output from a Join node. msg.payload.solar_power gives me "{"value": 125.5}" but I don't know the proper way to access the number.

Any help is appreciated!

"solar_power : {"value": 125.5}"

Hi @bacontexas

I'm a bit confused by the quotes because "{"value": 125.5}" seems invalid.

Can you show us the part of your flow that produces this?

Also use the debug node "Copy value" button to show us the output from your join node. Use the </> button to paste the value here.

Untitled 1

ps it's javascript not java :slightly_smiling_face:

It's gotten dark so value is now 0.0 (no sun).
{"value": 0.0}

Here's the code that isn't working - commented out the attempts I made to access, but I don't know JS (or Java). I'm rusty with perl / python...

m = {}
m.sunny = msg.payload.solar_power
// m.sunny = msg.payload.solar_power.value -- throws exception
// m.sunny = msg.payload.solar_power["value"] -- throws exception
return m

And here's two screen shots that may give you everything you need, I hope... I'm sure this is a first-day-of-class thing to know and I'm embarrassed it's taking me so long to figure it out.

Can you put a debug node on the output of the delay node to show what the data is and show the code in the ā€˜StandardizeObjent node.

The issue is that all the items in msg.payload seem to be strings when the look like they should be objects which would make them easy to handle.

P.s. the code is JavaScript not Java :grin:

A picture of your data is better than nothing, but it's not the actual data copied from the debug node that I asked for.

My attempt to emulate what you have looks like this (I used the </> button to post it)

"consumption_power":"{\"value\": 94.17}",
"solar_power":"{\"value\": 12.34}",
"fridge_temp":"{\"value\": 42.2}"

(If you had posted the actual data I would not have had to emulate it, and I have no proof that mine is the same as yours)

As @zenofmud says, normally we expect to see this sort of data as javascript objects like this


If I pass my version through a json parser node set to create a javascript object from msg.payload.solar_power it gives me a valid msg.payload.solar_power.value.
This is a fudge though and it's much better to fix the data at it's origin.

How did your data get in this state in the first place?
Is it bad code inside the function or something odd coming from an API?
I suspect the function, only because I see it has multiple input wires.
Why all these "value" property names? Perhaps the original data also had "units".

If you show us your function code we may be able to fix it.

Turns out the data input node, from MQTT, was set to output as "a String" instead of auto-detect. It is now outputting a parsed JSON object and everything is working nicely so far.

Thanks for your response. Next time I'll post actual data as requested.

Data source is from MQTT (on a RaspPi running Victron Venus OS), the node was set to output as "a String" instead of JSON auto-detect. I'd set it that way while goofing around trying to get this, my first flow, working. Got wrapped up in my underwear and couldn't undo what were probably multiple problems.

Today I fixed the root problem and cleaned up the flow and now it will actually trip a Shelly 1+ relay to turn on and off a fan on my desk based on the real-time status of spare solar power available. Eventually I'll have it controlling my RV fridge so that when the sun goes down, the fridge auto-magically switches from AC-power-inverted-from-solar to running off of propane instead.

This means I won't wake up in the mountains to empty batteries and no power at all if I forget to manually switch from AC power to propane in the evenings.

1 Like

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