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.
# /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.
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}
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.