- Platform information:
- Hardware: Dockerized on a Terramaster NAS
- OS: Linux
- Java Runtime Environment: 11
- openHAB version:3.0.2
I have been developing a Jython rule for the irrigation of the garden. I have set up an automation item which is responsible of turning the irrigation on, the jython script engine (jsr223, jython 2.7.2). and a netatmo weather station with a rain module attached. I also setup netatmo lib (python 2.7 compliant) via pip in the proprer directory(i.e. automation/python/lib/personal).
Meanwhille I faced the issue of importing the python standard library in the jsr223 openhab environment. My solution was to to set in in the EXTRA_JAVA_OPTS variable to make it accessible but i’m not sure it is the right way:
EXTRA JAVA OPTS=Dpython.path=/usr/lib/python2.7
I also added the directory as a docker bind volume
The rule should work like this:
Once a day the system checks rain 24h data(it gives the amount of rain fallen in a day) and add it to a python list that is saved to a json file. Then it loads data, computes the rain24h-mean of the list then it should start irrigating for ten minutes if the mean is zero.
So i developed such files using Pycharm IDE:
discover.py which is responsible of getting rain data from the station (located in automation/python/lib/personal);
persistence.py (located in automation/python/lib/personal). which is responsible for populating data of the list and save it to a json file;
irrigation_rule.py which is a cron rule that starts the rule (located in automation/python/jsr223/personal).
I see from openhab log that the rule starts but there is no response from it and nothing happens. Moreover i don’t know even how to debug!!! I am despered!
persistence.py
import json
class Persistence:
@staticmethod
def _load_data():
filename = "rain.json"
try:
with open(filename) as f:
rain_list = json.load(f)
except IOError:
return None
else:
return rain_list
@staticmethod
def _save_data(rain_list):
filename = "rain.json"
with open(filename,'w') as f:
json.dump(rain_list, f)
@staticmethod
def add_to_rain_list(value, dimension):
rain_list = Persistence._load_data()
if rain_list is None:
rain_list = [value]
else:
rain_list.append(value)
while(len(rain_list)) > dimension:
rain_list.pop(0)
Persistence._save_data(rain_list)
@staticmethod
def get_rain_list():
rain_list = Persistence._load_data()
return rain_list
discover.py
import threading
import time
from persistence import Persistence
import netatmo_client.client
temperature_result = None
rain_result = None
results_available = threading.Event()
class Discover:
def __init__(self, client_id, client_secret, username, password):
self.client_id = client_id
self.client_secret = client_secret
self.username = username
self.password = password
def _background_thread(self):
global temperature_result
global rain_result
temperature_result = self.get_temperature()
rain_result = self.get_rain_24_h()
results_available.set()
time.sleep(1)
def get_data_dict(self):
client = netatmo_client.client.NetatmoClient(self.client_id, self.client_secret)
scope = "read_station"
client.request_token_with_client_credentials(self.username, self.password, scope)
data_dict = client.station.get_station_data()
return data_dict
def get_rain_24_h(self):
rain_dict = self.get_data_dict()['devices'][0]['modules'][1]['dashboard_data']
return rain_dict['sum_rain_24']
def get_temperature(self):
temperature = self.get_data_dict()['devices'][0]['dashboard_data']['Temperature']
return temperature
def get_rain_list(self):
return Persistence.get_rain_list()
def get_rain_average(self):
average=sum(self.get_rain_list())/len(self.get_rain_list())
return average
def rain_monitoring(self, list_dimension=3):
thread = threading.Thread(target=self._background_thread())
thread.start()
results_available.wait()
Persistence.add_to_rain_list(rain_result, list_dimension)
irrigation_rule.py
import sys
sys.path.append('/openhab/conf/automation/lib/python/personal')
from discover import Discover
sys.path.append('/openhab/conf/automation/lib/python/core')
from core.rules import rule
from core.utils import sendCommand
from core.triggers import when
from threading import Timer
@rule("irrigation rule")
@when("Time cron 0 50 18 * * ?")
def func(event):
d = Discover('my_client_id', 'my_client_secret',
'my_username', 'my_password')
d.rain_monitoring()
if d.get_rain_average() == 0:
sendCommand("iI3_switch", "ON")
t = Timer(600.0, sendCommand("iI3_switch","OFF"))
t.start()
openhab.log
...
021-06-19 19:38:29.888 [WARN ] [jython.startup ] -
*******************************************************************************
Jython version: 2.7.2.final
Operating system: Linux
OS Version: 4.4.18-g8bcbd8a-dirty
Java vendor: Azul Systems, Inc.
Java VM name: OpenJDK 64-Bit Server VM
Java runtime name: OpenJDK Runtime Environment
Java runtime version: 11.0.10+9-LTS
configuration.py installed: True
sys.path: /openhab/conf/automation/lib/python
/openhab/userdata/cache/org.eclipse.osgi/232/0/bundleFile/Lib
__classpath__
__pyclasspath__/
*******************************************************************************
2021-06-19 19:38:29.892 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/core/components/100_DirectoryTrigger.py'
2021-06-19 19:38:34.481 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/core/components/100_StartupTrigger.py'
2021-06-19 19:38:38.366 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/personal/irrigation_rule.py'
netatmo lib link: