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.
PS, for reference, for accessing non standard object properties you use bracket notation
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";}
}
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!