Can somebody help me extract this object which start with a number?

Hi, I've just discovered that unfortunately Openweather returns on hourly data an object instead of the simple value (as for daily), please look here:

I try to exttract this number in this way

var precipitazioni = [];
for (let i = 0; i < 25; i++) { 
	precipitazioni[i]=0;
	if (typeof msg.payload.hourly[i].snow.1h!== 'undefined' {precipitazioni[i]=msg.payload.hourly[i].snow.1h;}
	if (typeof msg.payload.hourly[i].rain.1h!== 'undefined') {precipitazioni[i]=msg.payload.hourly[i].rain.1h;}}

unfortunately seams the number 1 creates problems on node red syntax checker, how can I overcome that? Thanks!

To avoid typos, use the tools provided. Use the "Copy Path" button in the debug sidebar.

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


PS, for reference, for accessing non standard object properties you use bracket notation

As well as any hints that Steve has given you, your first if() statement is missing it's )

Thanks!
I've follower your guide, it' looks like I've to type msg.payload.hourly[i].snow["1h"], but using this code:

var precipitazioni = [];
var massimo=0;
for (let i = 0; i < 25; i++) { 
	precipitazioni[i]=0;
	if (typeof msg.payload.hourly[i].snow["1h"]!== 'undefined') {precipitazioni[i]=msg.payload.hourly[i].snow["1h"];}
	if (typeof msg.payload.hourly[i].rain["1h"]!== 'undefined') {precipitazioni[i]=msg.payload.hourly[i].rain["1h"];}
	if (precipitazioni[i]>massimo) {massimo=precipitazioni[i];}
	massimo=Math.round(massimo*10) /10;
	}

I have the error:
"TypeError: Cannot read property '1h' of undefined"
that's strange, looks a syntax error, if this property doesn't exist it simply has to ignore it, but not. Hw can I fix it?

I overcomed the error with a double check, but I still have all zeros as output, so this ["1h"] doesn't catch the value

var precipitazioni = [];
var massimo=0;
for (let i = 0; i < 25; i++) { 
	precipitazioni[i]=0;
	if (typeof msg.payload.hourly[i].snow!== 'undefined') {
		if (typeof msg.payload.hourly[i].snow["1h"]!== 'undefined'){precipitazioni[i]=msg.payload.hourly[i].snow["1h"];}}
	if (typeof msg.payload.hourly[i].rain!== 'undefined') {
	if (typeof msg.payload.hourly[i].rain["1h"]!== 'undefined') {precipitazioni[i]=msg.payload.hourly[i].rain["1h"];}}
	if (precipitazioni[i]>massimo) {massimo=precipitazioni[i];}
	massimo=Math.round(massimo*10) /10;
	}

This is my code processing the precipitation data from OWM, it's a different test for the existence of rain and snow quantities. I'm only looking for values greater than 1mm.

msg.payload.hourly.forEach(element => {
if (element.hasOwnProperty('rain')&&element.rain["1h"]>1) {element.rainmm = Math.round(element.rain["1h"]) + "mm";}
if (element.hasOwnProperty('snow')&&element.snow["1h"]>1) {element.snowmm = Math.round(element.snow["1h"]) + "mm";}
}
1 Like

Thanks, it works, also my code, I've just missed a the only value available (I?m checking every 2 hours, it happened at 3rd hour so I didn't see it.
Good!

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