I think it might be me who re-invented the wheel! I’ll show you what I’ve done, but be warned: I’m not a programmer, nor do I work in IT: everything I’ve done is hacked together from other examples.
I am on openHAB 2.5.10.
I use the Jython Helper Libraries, installed as per this post.
I have installed the datapoint-python
project.
In $OPENHAB_CONF/lib/python/configuration.py
I have the following, defining my key:
datapoint_key = "xxxxxxxx-yyyy-zzzz-xxxx-yyyy"
In $OPENHAB_CONF/lib/python/personal/personal_functions.py
I have the following. This function returns a boolean True
or False
depending on whether it is likely to rain at the specified site_id
, time
and date
:
from datetime import datetime, timedelta
from core.actions import LogAction
import datapoint
import configuration
reload(configuration)
from configuration import datapoint_key
def will_it_rain(site_id, time=None, date=0):
rain_flag = False
# Create connection to DataPoint with your API key
conn = datapoint.connection(api_key=datapoint_key)
# Get a forecast for site_id with 3 hourly timesteps
forecast = conn.get_forecast_for_site(site_id, "3hourly")
if time == None:
LogAction.logInfo("will_it_rain","Checking for rain all day")
# Loop through all the timesteps in day date
for timestep in forecast.days[date].timesteps:
# Check to see if the chance of rain is more than 30% at any point
if timestep.precipitation.value > 30:
LogAction.logInfo("will_it_rain","Rain chance {}% at {}".format(timestep.precipitation.value,timestep.date))
rain_flag = True
else:
LogAction.logInfo("will_it_rain","Checking for rain at a specific time")
# Get the difference in time between now and the requested forecast time
delta = time - datetime.now()
# Check that the difference in time is into the future. If not, assume the request is for now
if delta.total_seconds() > 0:
if forecast.future(in_seconds=delta.total_seconds()).precipitation.value > 30:
LogAction.logInfo("will_it_rain","Rain chance {}% at {}".format(forecast.future(in_seconds=delta.total_seconds()).precipitation.value,forecast.future(in_seconds=delta.total_seconds()).date))
rain_flag = True
else:
if forecast.now().precipitation.value > 30:
LogAction.logInfo("will_it_rain","Rain chance {}% at {}".format(forecast.future(in_seconds=delta.total_seconds()).precipitation.value,forecast.future(in_seconds=delta.total_seconds()).date))
rain_flag = True
return rain_flag
From my Jython rules within openHAB, I can then call:
will_it_rain(351492, date=0)
which will return whether it will rain at the site 351492
at any point today (date 0). Or
import datetime as dt
tomorrow_at_8 = dt.datetime.combine(dt.date.today() + dt.timedelta(days=1), dt.time(8,0))
will_it_rain(351492, time=tomorrow_at_8)
which will return whether it will rain at the site 351492
at 8am tomorrow morning. Or
from datetime import datetime
will_it_rain(351492, time=datetime.now())
which will return whether it will rain at the site 351492
now.
That’s all I’m doing with the DataPoint data at the moment. Not pretty whatsoever, and I am certainly considering moving to using the new HTTP binding when I migrate to OH3 - the fewer rules the better!