SMHI hourly forecast

Don’t know.
Read the API documentation.

Is it possible to fetch estimated temperature data for the hole day and present it in apexchart?

Don’t know about the chart but the temperature is in the API

image

Temperature is t in the API response

Need some help here. I have never used node-red so can’t get it to work. I only get value for 2015 in my sensor. Do I need a automation to fetch value from the node-red automation?

I don’t understand what you are saying.
What do you mean by 2015?

I can upload my current smhi sequence later

This is what I use currently:

[{"id":"8584052db0255346","type":"http request","z":"ad594fb84fe6d294","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/xxxxxxx/lat/xxxxxxx/data.json","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"credentials":{},"x":310,"y":120,"wires":[["baaeb67025c3b042"]]},{"id":"994a4699a32ef4ec","type":"inject","z":"ad594fb84fe6d294","name":"","props":[{"p":"payload"}],"repeat":"7200","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":150,"y":120,"wires":[["8584052db0255346"]]},{"id":"baaeb67025c3b042","type":"function","z":"ad594fb84fe6d294","name":"","func":"var array = {};\nvar time = \"\";\n\ntime = new Date(msg.payload.approvedTime);\nvar offset = time.getTimezoneOffset(); \ntime = new Date(time.getTime() - (offset*60*1000)); \nmsg.state = time.toISOString().replace(\"T\", \" \").substring(0,16)\n\n\nfor (var index in msg.payload.timeSeries.slice(0,67)) {\n    time = new Date(msg.payload.timeSeries[index].validTime);\n    offset = time.getTimezoneOffset();\n    time = new Date(time.getTime() - (offset * 60 * 1000)); \n\n    array[time.toISOString().replace(\"T\", \" \").substring(0, 16)] = { \"temp\": 0, \"wind\": 0, \"gust\": 0, \"nederbord\": 0, \"symbol\" : 0 };\n    for (var val in msg.payload.timeSeries[index].parameters) {\n        if (msg.payload.timeSeries[index].parameters[val][\"name\"] == \"t\") {\n            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]) }\n        }else if (msg.payload.timeSeries[index].parameters[val][\"name\"] == \"ws\"){\n            array[time.toISOString().replace(\"T\", \" \").substring(0,16)].wind =  msg.payload.timeSeries[index].parameters[val].values[0];  \n        }else if (msg.payload.timeSeries[index].parameters[val][\"name\"] == \"gust\"){\n            array[time.toISOString().replace(\"T\", \" \").substring(0,16)].gust =  msg.payload.timeSeries[index].parameters[val].values[0];  \n        }else if (msg.payload.timeSeries[index].parameters[val][\"name\"] == \"pmax\") {\n            array[time.toISOString().replace(\"T\", \" \").substring(0, 16)].nederbord = msg.payload.timeSeries[index].parameters[val].values[0];  \n        } else if (msg.payload.timeSeries[index].parameters[val][\"name\"] == \"Wsymb2\") {\n            array[time.toISOString().replace(\"T\", \" \").substring(0, 16)].symbol = msg.payload.timeSeries[index].parameters[val].values[0];\n        }\n    }\n}\n\nmsg.arr = array;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":160,"wires":[["deb8fd1153e98ceb","aeb70bcf74c928d9"]]},{"id":"deb8fd1153e98ceb","type":"debug","z":"ad594fb84fe6d294","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":690,"y":80,"wires":[]},{"id":"aeb70bcf74c928d9","type":"ha-entity","z":"ad594fb84fe6d294","name":"smhi hourly","server":"4bbca37b.1700ec","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"smhi hourly"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"state","stateType":"msg","attributes":[{"property":"hourly","value":"arr","valueType":"msg"}],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":670,"y":180,"wires":[[]]},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]
2 Likes

Thanks!
Never used node-red and not sure if i did it right (sorry).
I have put in my long ang lat, should change something else.
I get unknown in my sensor.

I answer myself. The problem was that i use coordinates XX.XXXXX and XX.XXXXX. I should only use XX and XX.

Thanks for the code.

Big thank you for this example code!

Fungerar perfekt :smile:

Hi,
I try to read out the cloudiness values from SMHI.
Can see from doc. that there is four different parameters

image

Have added the SMHI to the Integrations
But I don’t manage to read out this values to sensor
Have following code

      smhi_cloudiness_mean:
        friendly_name: SMHI mean cloudiness
        value_template: "{{ state_attr( 'weather.smhi_vasteras', 'tcc_mean') }}"

When I try to show this value in the UI, I get “Unknown”

image

What am I doing wrong ?

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