SMHI hourly forecast

I see.
Index is not an index.

Ok let’s use a flag instead.

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

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 (flag) {
                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 (flag) {
                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 (flag) {
                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 (flag) {
                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 (flag) {
                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 (flag) {
                 msg.mcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
            }
        }
        flag = false;
    }
}

/*  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;

Because index wasn’t an index the code failed hard.
But now we set a flag to true and on the first time series it sets the msg.tcc mcc and so on then makes the flag false so that it does not overwrite the values again.

Yes, now there is no errors.
But I can’t see the msg.tcc output
image

It seems there was an issue with variable scope.
But not all values are in the original response from SMHI so you can’t get all of it.

What this returns is all you can possibly get from SMHI in your location it seems.


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

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 (flag) {
                var 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 (flag) {
                var 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 (flag) {
                var 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 (flag) {
                var 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 (flag) {
                var 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 (flag) {
                var mcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
            }
        }
        msg.flag = false;
    }
}


msg.tcc = tcc;
msg.tcc_mean = tcc_mean;
msg.lcc = lcc;
msg.lcc_mean = lcc_mean;
msg.mcc = mcc;
msg.mcc_mean = mcc_mean;


msg.arr = array;
return msg;

Okay, I see.
Now it’s good with the outputs but they get wrong values :thinking:
Can see the values in the array but not in the msg output

The reason you see values is because you created them.
Look at row 16.

If you instead look at the msg.payload and expand one time series you will not see these values

At tow 16 all values get value “0” that I understand but why will not
tcc = 0
tcc_mean = 4
lcc = 0
lcc_mean = 4
mcc = 0
mcc_mean = 0

As in arr_2022-12-23 10:00

It becomes undefined because the if statement is never true

        } 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 (flag) {
                var tcc = msg.payload.timeSeries[index].parameters[val].values[0];
            }
        }

This never gets true and therefore

When this line here comes:

msg.tcc = tcc;

tcc is undefined

@Hellis81 or @dubbfredda any good tips on how to get this smhi hourly sensor in a lovelace weather card ?

Like for example this

I assume you need a template entity

1 Like

Thanks! :slight_smile:
Do you use it ?. Can you share an example for youre sensor config in that case ?.

No. I don’t use this data for that purpose.
I only use the hourly data to know what kind of clothes to bring home from school to our 6 year old.
By some reason they are forced to be outside for 15 minutes every morning before allowed to get inside, and if we don’t have appropriate clothes for those 15 minutes then the teachers don’t care.
So every time we arrive to pick him up at school we get a notification with the weather at that time next day ± 2 hours.

1 Like

Haha nice teacher and school :slight_smile:

I can kind of understand the reasoning that it’s easier to control the kids if everyone enters at the same time and the kids don’t loose focus and do something they shouldn’t.
But if it’s raining from the side then 15 minutes is a very long time. And even worse if we forgot to bring home the rain clothes the day before.

I don’t understand should not the “flag” be true the first run and that should give “tcc” one value

It is. But I see now that the flag = false is placed one row to high.
It should be after the for loop.

        } 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 (flag) {
                var mcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
            }
        }
        msg.flag = false;. // Move this
    }
    // Down here
}

I move the code

           }
        } 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 (flag) {
                var mcc_mean = msg.payload.timeSeries[index].parameters[val].values[0];
            }
        }
        //msg.flag = false;
    }
    msg.flag = false;
}

But the output is still the same :thinking:
And the mcc_mean is not correct

Ahh!!

It’s not msg.flag, its just flag = false.

Great, now it’s working.
Thanks’ a lot :grinning: