SMHI hourly forecast

If you are using the SMHI integration then it will not work.
Read the thread and use the Node red sequence to use the API.

@Hellis81 when I import your code in Node-red and run it I receive following error

image

Did you change the lat and long values in the url?

Now I didn’t!
But now when I have changed the lat and long in the url it still gives me the same fault.
My url looks like this.

https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/16.5914/lat/59.6075/dat

You’re missing a few characters at the end.

... /data.json

I see.
I add to this

https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/16.5914/lat/59.6075/dat.json

But it still not work!
If I pasted the url in the webbrower it also give me an error. But maybe it should do that!

Again you missed characters…

https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/16.5914/lat/59.6075/data.json

Thank’s now it’s working, no faults.
Now I just need to find out how to modify your code to get this parameters
image
Something you also can help me with?

On row 15 in the function node, add another element to the array with what you want it to be named as.

In the loop below, copy the last else if and paste it below.
Replace the Wsymb2 with the parameter you want to grab from the API (tcc_mean?).
Replace symbol that is just below with the name you gave in row 15.
That should be enough

Yes, that’s work :grinning:
I can see the output in the “arr”

But the array have many different time series.
Is it possible to get current value as outputs in the payload.
Would like to have current tcc and tcc_mean as outputs

1 Like

This part here:

says it should look at the first 67 timeseries.
If you want only one value then make it ...slice(0,1).
The problem is that you can’t add it to msg.payload at this point because the loop is looping the values of msg.payload and if you overwrite the content of payload then I guess it will fail.

But you could in your newly created else if add something like msg.tcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
and then in a change node after the function node pass the value(s) to msg.payload if you must have them there

msg.tcc_mean will do fine. Not need to be the payload.
If just want to assign tcc_mean to the first/current value from your code.
How do I do that?

As I described above.

The above code give me wrong value

msg.tcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];

It seems to give the symbol value!

If I place the above code outside the for-loop, should I replace the [index] with [0] for the first timeserie?

I can see if I have time to fix it today.

I have now tried some and partly it works :grinning:
tcc & tcc_mean seems to be correct but lcc, lcc_mean, mcc & mcc_mean don’t get the correct values

var array = {};
var time = "";

time = new Date(msg.payload.approvedTime);
var offset = time.getTimezoneOffset(); 
time = new Date(time.getTime() - (offset*60*1000)); 
msg.state = time.toISOString().replace("T", " ").substring(0,16)


for (var index in msg.payload.timeSeries.slice(0,67)) {
    time = new Date(msg.payload.timeSeries[index].validTime);
    offset = time.getTimezoneOffset();
    time = new Date(time.getTime() - (offset * 60 * 1000)); 

    array[time.toISOString().replace("T", " ").substring(0, 16)] = { "temp": 0, "wind": 0, "gust": 0, "nederbord": 0, "symbol": 0, "tcc": 0, "tcc_mean": 0, "lcc": 0, "lcc_mean": 0, "mcc": 0, "mcc_mean": 0 };
    for (var val in msg.payload.timeSeries[index].parameters) {
        if (msg.payload.timeSeries[index].parameters[val]["name"] == "t") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].temp = msg.payload.timeSeries[index].parameters[val].values[0];   //  { "temp": Math.round(msg.payload.timeSeries[index].parameters[val].values[0]) }
        }else if (msg.payload.timeSeries[index].parameters[val]["name"] == "ws"){
            array[time.toISOString().replace("T", " ").substring(0,16)].wind =  msg.payload.timeSeries[index].parameters[val].values[0];  
        }else if (msg.payload.timeSeries[index].parameters[val]["name"] == "gust"){
            array[time.toISOString().replace("T", " ").substring(0,16)].gust =  msg.payload.timeSeries[index].parameters[val].values[0];  
        }else if (msg.payload.timeSeries[index].parameters[val]["name"] == "pmax") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].nederbord = msg.payload.timeSeries[index].parameters[val].values[0];  
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "Wsymb2") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].symbol = msg.payload.timeSeries[index].parameters[val].values[0];
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "tcc") {
        array[time.toISOString().replace("T", " ").substring(0, 16)].tcc = msg.payload.timeSeries[index].parameters[val].values[0];
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "tcc_mean") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].tcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "lcc") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].lcc = msg.payload.timeSeries[index].parameters[val].values[0];
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "lcc_mean") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].lcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "mcc") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].mcc = msg.payload.timeSeries[index].parameters[val].values[0];
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "mcc_mean") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].mcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
        }
    }
}

msg.tcc = msg.payload.timeSeries[0].parameters[5].values[0];
msg.tcc_mean = msg.payload.timeSeries[0].parameters[6].values[0];
msg.lcc = msg.payload.timeSeries[0].parameters[7].values[0];
msg.lcc_mean = msg.payload.timeSeries[0].parameters[8].values[0];
msg.mcc = msg.payload.timeSeries[0].parameters[9].values[0];
msg.mcc_mean = msg.payload.timeSeries[0].parameters[10].values[0];

msg.arr = array;
return msg;

Result:

What I was thinking was something like this:

(untested)

var array = {};
var time = "";

time = new Date(msg.payload.approvedTime);
var offset = time.getTimezoneOffset(); 
time = new Date(time.getTime() - (offset*60*1000)); 
msg.state = time.toISOString().replace("T", " ").substring(0,16)


for (var index in msg.payload.timeSeries.slice(0,1)) {
    time = new Date(msg.payload.timeSeries[index].validTime);
    offset = time.getTimezoneOffset();
    time = new Date(time.getTime() - (offset * 60 * 1000)); 

    array[time.toISOString().replace("T", " ").substring(0, 16)] = { "temp": 0, "wind": 0, "gust": 0, "nederbord": 0, "symbol": 0, "tcc": 0, "tcc_mean": 0, "lcc": 0, "lcc_mean": 0, "mcc": 0, "mcc_mean": 0 };
    for (var val in msg.payload.timeSeries[index].parameters) {
        if (msg.payload.timeSeries[index].parameters[val]["name"] == "t") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].temp = msg.payload.timeSeries[index].parameters[val].values[0];   //  { "temp": Math.round(msg.payload.timeSeries[index].parameters[val].values[0]) }
        }else if (msg.payload.timeSeries[index].parameters[val]["name"] == "ws"){
            array[time.toISOString().replace("T", " ").substring(0,16)].wind =  msg.payload.timeSeries[index].parameters[val].values[0];  
        }else if (msg.payload.timeSeries[index].parameters[val]["name"] == "gust"){
            array[time.toISOString().replace("T", " ").substring(0,16)].gust =  msg.payload.timeSeries[index].parameters[val].values[0];  
        }else if (msg.payload.timeSeries[index].parameters[val]["name"] == "pmax") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].nederbord = msg.payload.timeSeries[index].parameters[val].values[0];  
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "Wsymb2") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].symbol = msg.payload.timeSeries[index].parameters[val].values[0];
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "tcc") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].tcc = msg.payload.timeSeries[index].parameters[val].values[0];
            msg.tcc = msg.payload.timeSeries[index].parameters[val].values[0];
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "tcc_mean") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].tcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
            msg.tcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "lcc") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].lcc = msg.payload.timeSeries[index].parameters[val].values[0];
            msg.lcc = msg.payload.timeSeries[index].parameters[val].values[0];
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "lcc_mean") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].lcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
            msg.lcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "mcc") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].mcc = msg.payload.timeSeries[index].parameters[val].values[0];
            msg.mcc = msg.payload.timeSeries[index].parameters[val].values[0];
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "mcc_mean") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].mcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
            msg.mcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
        }
    }
}

/*  I just left these here for no good reason.
msg.tcc = msg.payload.timeSeries[0].parameters[5].values[0];
msg.tcc_mean = msg.payload.timeSeries[0].parameters[6].values[0];
msg.lcc = msg.payload.timeSeries[0].parameters[7].values[0];
msg.lcc_mean = msg.payload.timeSeries[0].parameters[8].values[0];
msg.mcc = msg.payload.timeSeries[0].parameters[9].values[0];
msg.mcc_mean = msg.payload.timeSeries[0].parameters[10].values[0];
*/

msg.arr = array;
return msg;

But this only loops the first item.

You could use a if (index == 0) { msg.mcc = … } and loop all 67 items (or more) if you want.

Like this:

var array = {};
var time = "";

time = new Date(msg.payload.approvedTime);
var offset = time.getTimezoneOffset(); 
time = new Date(time.getTime() - (offset*60*1000)); 
msg.state = time.toISOString().replace("T", " ").substring(0,16)


for (var index in msg.payload.timeSeries.slice(0,67)) {
    time = new Date(msg.payload.timeSeries[index].validTime);
    offset = time.getTimezoneOffset();
    time = new Date(time.getTime() - (offset * 60 * 1000)); 

    array[time.toISOString().replace("T", " ").substring(0, 16)] = { "temp": 0, "wind": 0, "gust": 0, "nederbord": 0, "symbol": 0, "tcc": 0, "tcc_mean": 0, "lcc": 0, "lcc_mean": 0, "mcc": 0, "mcc_mean": 0 };
    for (var val in msg.payload.timeSeries[index].parameters) {
        if (msg.payload.timeSeries[index].parameters[val]["name"] == "t") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].temp = msg.payload.timeSeries[index].parameters[val].values[0];   //  { "temp": Math.round(msg.payload.timeSeries[index].parameters[val].values[0]) }
        }else if (msg.payload.timeSeries[index].parameters[val]["name"] == "ws"){
            array[time.toISOString().replace("T", " ").substring(0,16)].wind =  msg.payload.timeSeries[index].parameters[val].values[0];  
        }else if (msg.payload.timeSeries[index].parameters[val]["name"] == "gust"){
            array[time.toISOString().replace("T", " ").substring(0,16)].gust =  msg.payload.timeSeries[index].parameters[val].values[0];  
        }else if (msg.payload.timeSeries[index].parameters[val]["name"] == "pmax") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].nederbord = msg.payload.timeSeries[index].parameters[val].values[0];  
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "Wsymb2") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].symbol = msg.payload.timeSeries[index].parameters[val].values[0];
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "tcc") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].tcc = msg.payload.timeSeries[index].parameters[val].values[0];
            if (index == 0) {
                msg.tcc = msg.payload.timeSeries[index].parameters[val].values[0];
            }
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "tcc_mean") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].tcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
            if (index == 0) {
                msg.tcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
            }
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "lcc") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].lcc = msg.payload.timeSeries[index].parameters[val].values[0];
            if (index == 0) {
                msg.lcc = msg.payload.timeSeries[index].parameters[val].values[0];
            }
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "lcc_mean") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].lcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
            if (index == 0) {
                msg.lcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
            }
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "mcc") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].mcc = msg.payload.timeSeries[index].parameters[val].values[0];
            if (index == 0) {
                msg.mcc = msg.payload.timeSeries[index].parameters[val].values[0];
           }
        } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "mcc_mean") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].mcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
            if (index == 0) {
                 msg.mcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
            }
        }
    }
}

/*  I just left these here for no good reason.
msg.tcc = msg.payload.timeSeries[0].parameters[5].values[0];
msg.tcc_mean = msg.payload.timeSeries[0].parameters[6].values[0];
msg.lcc = msg.payload.timeSeries[0].parameters[7].values[0];
msg.lcc_mean = msg.payload.timeSeries[0].parameters[8].values[0];
msg.mcc = msg.payload.timeSeries[0].parameters[9].values[0];
msg.mcc_mean = msg.payload.timeSeries[0].parameters[10].values[0];
*/

msg.arr = array;
return msg;

Thanks @Hellis81
But unfortunately is something wrong with this code, index==0 is not correct

I don’t really see how this code is suppose to work.
Is the tcc… written to the arry or is it only written to the msg.tcc output?

I think that the code need to be like this ?

 } else if (msg.payload.timeSeries[index].parameters[val]["name"] == "tcc") {
            array[time.toISOString().replace("T", " ").substring(0, 16)].tcc = msg.payload.timeSeries[index].parameters[val].values[0];
            if (msg.payload.timeSeries[index] == 0) {
                msg.tcc = msg.payload.timeSeries[index].parameters[val].values[0];
            }

But where is the msg.tcc in the output message ?
image

If my old code in the end is used it looks like this
image