Hi all,
I’ve created small python script for uploading weather data to the PWS WU station, As many of us do have weather station, this comes handy…in my usecase for Hydrawise watering solution to use my own PWS as data source… and as it sometimes happends I was not able to find anything useful.out there
all you need to do is adjust
api_key
station_id
openhab_base_url
and make sure that WU items are populated with data from OH.
I’ve created separated WU items as Numbers/Strings to have it out of my regular items with dimensions in OH and for some unknown reason WU accepts data in inches and fahrenheits so … calculations inside the script from metrics.
script
import requests
import pytemperature
def get_openhab_item_value(openhab_base_url, item_name):
url = f"{openhab_base_url}/rest/items/{item_name}/state"
try:
response = requests.get(url)
if response.status_code == 200:
return response.text
else:
print(f"Failed to fetch item '{item_name}' value. Status code: {response.status_code}")
return None
except requests.exceptions.RequestException as e:
print(f"An error occurred while fetching the item '{item_name}' value: {e}")
return None
def upload_weather_data(api_key, station_id, temperature_item, dewpoint_item, humidity_item, wind_speed_item, wind_gust_item, wind_direction_item, rainfall_item, rainfallday_item, uv_item, pressure_item, clouds_item):
openhab_base_url = 'ADJUST'
temperature = float(get_openhab_item_value(openhab_base_url, temperature_item))
dewpoint = float(get_openhab_item_value(openhab_base_url, dewpoint_item))
humidity = float(get_openhab_item_value(openhab_base_url, humidity_item))
wind_speed = float(get_openhab_item_value(openhab_base_url, wind_speed_item))
wind_gust = float(get_openhab_item_value(openhab_base_url, wind_gust_item))
wind_direction = float(get_openhab_item_value(openhab_base_url, wind_direction_item))
rainfall = float(get_openhab_item_value(openhab_base_url, rainfall_item))
rainfallday = float(get_openhab_item_value(openhab_base_url, rainfallday_item))
uvlevel = float(get_openhab_item_value(openhab_base_url, uv_item))
pressure = float(get_openhab_item_value(openhab_base_url, pressure_item))
clouds = get_openhab_item_value(openhab_base_url, clouds_item)
url = f"https://weatherstation.wunderground.com/weatherstation/updateweatherstation.php"
data = {
"ID": station_id,
"PASSWORD": api_key,
"action": "updateraw",
"dateutc": "now",
"tempf": pytemperature.c2f(temperature),
"dewptf" : pytemperature.c2f(dewpoint),
"humidity": humidity,
"windspeedmph": wind_speed * 0.621371192,
"windgustmph" : wind_gust * 0.621371192,
"winddir": wind_direction,
"rainin": rainfall * 0.039,
"dailyrainin": rainfallday * 0.039,
"UV" : uvlevel,
"baromin" : pressure * 0.0295301,
"clouds" : clouds
}
try:
response = requests.post(url, data=data)
if response.status_code == 200:
print("Weather data uploaded successfully.")
else:
print(f"Failed to upload weather data. Status code: {response.status_code}")
print(response.text)
except requests.exceptions.RequestException as e:
print(f"An error occurred while making the request: {e}")
# OH items && Station ID/key
if __name__ == "__main__":
api_key = 'ADJUST'
station_id = 'ADJUST'
temperature = 'WU_Temperature'
dewpoint = 'WU_DewPoint'
humidity = 'WU_Humidity'
wind_speedms = 'WU_WindSpeedMS'
wind_speed = 'WU_WindSpeed'
wind_gust = 'WU_WindGust'
wind_direction = 'WU_WindAngle'
rainfall = 'WU_RainfallHour'
rainfallday = 'WU_RainfallDay'
uv = 'WU_UVlevel'
pressure = 'WU_Pressure'
clouds = "WU_Clouds"
upload_weather_data(api_key, station_id, temperature, dewpoint, humidity, wind_speed, wind_gust, wind_direction, rainfall, rainfallday, uv, pressure, clouds)
running it from OH direcly via rule:
rule "Update WU Weather PWS"
when
Item WS_Temperature received update or
Item WS_WindSpeed received update or
Item WS_Humidity received update or
Item Weather_Temperature received update or
System started
then
executeCommandLine("python3", "/etc/openhab/scripts/weatherundeground.py")
end
items: (populate them accordingly)
Number WU_Temperature
Number WU_Humidity
Number WU_WindSpeed
Number WU_WindSpeedMS
Number WU_WindGust
Number WU_WindAngle
Number WU_RainfallHour
Number WU_RainfallDay
Number WU_DewPoint
Number WU_UVlevel
Number WU_Pressure # sealevel
String WU_Clouds # OVC, BKN, SCT, FEW, SKC
enjoy, cheers