SMHI.py to be used with e.g. Automagically

http://automagucally.weebly.com

Moderators: davka003, Telldus

Post Reply
KHolm
Posts: 163
Joined: Thu Jan 18, 2018 11:08 am

SMHI.py to be used with e.g. Automagically

Post by KHolm » Thu Jan 18, 2018 11:08 am

Hi,

I made a small 'hack' to fetch weather from SMHI, in my case to be used with Automagically on the Raspberry Pi.

It's a python script that simply fetches weather forecast using SMHI API (JSON) and outputs a series of temperatures based on arguments given.

$ python smhi_parse.py 5 69.059954 20.548647
temperature,-28.3,-28.3,-28.4,-28.4,-28.5

$ python smhi_parse.py 3 55.361551 12.812748
temperature,0.4,0.3,0.3

First argument is the number of hours to fetch from now(hour), second and third argument is latitude and longitude; which you get from maps.google.se (right click, 'what is here').

This can be used in a data fetcher:
SMHI.py fetch every 15 min from /usr/bin/python /home/pi/source/automagically/smhi_parse.py 5 67.849707 20.595049
==> Use: Line, scanf
==> Use: $s as scanf argument

And a signalHandler (store global variable ) with the following pattern to match (No of %f depends on input arguments given to script):
Pattern to match==> datafetcher,2,SMHI.py,fetched:temperature,%f,%f,%f,%f,%f

Parsed variable to store ==> Put 1 for now(hour), 2 for now(hour+1), etc...

Now it's just to throw it up on your remote and voila.

Improvements needed:
Arguments should really be fetched from settings; possibly possible if David or someone can tell how to...

Put the script in /home/pi/source/automagically/.

Code: Select all

#! /usr/bin/python
import urllib2
import json
import argparse
from time import strftime, localtime
from datetime import datetime, timedelta

debug = False

parser = argparse.ArgumentParser(description='Read temperature from SMHI')
parser.add_argument('hours', type=int)
parser.add_argument('latitude', type=float)
parser.add_argument('longitude', type=float)
args = parser.parse_args()
if debug:
    print args.hours, args.longitude, args.latitude

url='http://opendata-download-metfcst.smhi.se/api/category/pmp1g/version/1/geopoint/lat/'+str(args.latitude)+'/lon/'+str(args.longitude)+'/data.json'
if debug:
    print url  
j = urllib2.urlopen(url)
j_obj = json.load(j)

output = 'temperature'
for predition_time in j_obj['timeseries']:
    for i in range(0, args.hours):
        if predition_time['validTime'] == (datetime.now() + timedelta(hours=i)).strftime("%Y-%m-%dT%H:00:00Z"):
            output += ","+ str(predition_time['t'])

print output
Have fun!
/Marcus

KHolm
Posts: 163
Joined: Thu Jan 18, 2018 11:08 am

Re: SMHI.py to be used with e.g. Automagically

Post by KHolm » Thu Jan 18, 2018 11:08 am

KHolm wrote: Improvements needed:
Arguments should really be fetched from settings; possibly possible if David or someone can tell how to...
Implemented and issued a pull request. https://bitbucket.org/davka003/automagi ... t-added-to
It is now possible to use both generic settings and global variables as arguments to datafetches of type Execute.

Code: Select all

/bin/echo 5,$Location Latitude$,$Location Longitude$,$Timezone$
# Executes:
/bin/echo 5,56.213524,15.559601,Europe/Stockholm
# Signals
datafetcher,3,Echo,fetched:5,56.213524,15.559601,Europe/Stockholm

/bin/echo Temp:$floatSMHITemp$
# Executes:
/bin/echo Temp:-3.1
# Signals
datafetcher,3,Echo,fetched:Temp:-3.1
BR,/Marcus

sjostrom
Posts: 2
Joined: Thu Jan 18, 2018 11:08 am

Re: SMHI.py to be used with e.g. Automagically

Post by sjostrom » Thu Jan 18, 2018 11:08 am

Worth mentioning is that the SMHI API has been upgraded so the url that refers to pmp1g is obsolete:
url='http://opendata-download-metfcst.smhi.s ... /data.json'

Replacing with pmp2g to
url='http://opendata-download-metfcst.smhi.s ... /data.json'
does the trick..

Post Reply