Simple python script: OpenWeather Map Temperature to Emoncms.org

I’ve been doing some testing using data from OpenWeather map and posting to Emoncms.org, here’s a simple script to get current ambient temperature for a particular location and post to emoncms.org.

OpenWeather map a free tier for 1,000,000 API calls per month: Weather API - OpenWeatherMap

# /usr/bin/python3

import requests
import json

# Setup OpenWeather Map 
openweather_api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
lat = "xx.xxx"
lon = "-x.xxxx"
openweather_url = "https://api.openweathermap.org/data/2.5/onecall?lat=%s&lon=%s&appid=%s&units=metric&exclude=minutely,hourly,daily,alerts" % (lat, lon, openweather_api_key)

# Get Temperature
response = requests.get(openweather_url)
data = json.loads(response.text)
current = data.get('current') 
temp = current.get('temp')

# Setup Emoncms
emoncms_apikey='xxxxxxx'
payload = {'csv':temp, 'apikey':emoncms_apikey}
url_emoncms = 'https://emoncms.org/input/post?node=ambient_temp'

if type(temp) == float:
    print(temp)
    r = requests.post(url_emoncms, params=(payload))
    print (r.text)

Note: this script is a quick and dirty test. It’s not written in a particular robust way, or extensively tested. It’s designed for prototyping and development rather than use in production.

4 Likes

I’ve have written something similar, and have found it to be very useful.

Some other handy things you can get from this API:

  • current.get('dt') - the timestamp of the weather reading, if you care about super accuracy.
  • current.get('feels_like') - the effective temperature, which takes into account wind and humidity. This can be helpful for fine tuning the operation of an air source heat pump, for example.
  • current.get('clouds') - the percentage cloud cover, potentially useful for solar installations.

There’s many other useful weather fields, such as pressure, humidity, wind, etc. {see API Doc}

4 Likes

Nice! That’s very cool.

I pull data from a weather service with Home Assistant then pass the data on to Emoncms via Node-RED.

1 Like

Indeed, there are lots of good options to do this, at home I use NodeRED to post to weather to my home emonPi Emoncms. However, this script above was a solution to help a user who wanted a simple script without having to install NodeRED or Home Assistant. It would be cool to have an emonHub config that could do this.

3 Likes

It would but creating an interfacer to pull the API data is the easy part, decoding the result less so as that would have to be specific to the API.

You could do a ‘common’ REST API interfacer, then specific interfacers to convert the result for sending to emoncms.

Hello Glyn,

Can you share the nodeRed config and how to go about integrating the weather to local emonPi. Thank you

2 Likes