-
Notifications
You must be signed in to change notification settings - Fork 0
Drools
How to use Drools in openHAB
Since openHAB 0.9.0, JBoss Drools support is available as a separate download (link?). This can be used as a replacement for the integrated rule engine, if a more powerful rule engine is needed for the definition of automation rules.
To install this package, simply extract it to ${openhab.home
} and you are done.
If you want to switch off the integrated rule engine, you have to add the parameter -DnoRules=true
to the start.sh/bat
script.
Note that in contrast to the integrated rule engine, there is no IDE support in the openHAB Designer for Drools. You may want to download and install the JBoss Tooling for it.
Most of the rule code can be written in plain Java, but you should make yourself familiar with the specifics of the Drools rule syntax.
Rule files can be created in the folder ${openhab.home}/configurations/drools
. They should have the file extension drl for normal Java-based rule files. The runtime distribution already comes with an example rule file.
Most of the time you will be dealing with item states in your rules. You will have to refer to the sources to see what type the states of your item are and how these can be transformed into Java primitive types.
You can find the item definitions here and the type definitions here.
To get a Java float of a temperature value defined as an NumberItem
, you could use the following code:
((DecimalType)$item.getState()).toBigDecimal().floatValue()
The working memory of the Drools engine contains the following objects:
-
The
Item
instances for all defined items of openHAB. These have the following attributes (their type is given in brackets): -
name (String)
-
state (State)
-
A
StateEvent
instance for every status update that was received on the event bus since the last rule evaluation. AStateEvent
has the following attributes: -
itemName (String)
-
item (Item)
-
timestamp (Calendar)
-
changed (boolean)
-
oldState (State)
-
newState (State)
-
A
CommandEvent
instance for every command that was received on the event bus since the last rule evaluation. ACommandEvent
has the following attributes: -
itemName (String)
-
item (Item)
-
timestamp (Calendar)
-
command (Command)
StateEvent
and CommandEvent
objects are removed after a single rule evaluation again, so that rules are not triggered multiple times on such events. The Item
objects clearly stay in the working memory, but are always directly updated when their status changes.
The rule evaluation is triggered continuously with a 200ms pause between two evaluations.
To have all functions and types available, you should at least use the imports as they exist in the demo.drl file. Add additional import statements for any additional Java classes you might want to use (e.g. java.math.*).
The when clause (LHS) of a rule should contain conditions based on the objects in the working memory, i.e. items and events.
Here is an example of a when clause:
$event : StateEvent(itemName=="Rain", changed==true)
$window : Item(name=="Window", state==OpenClosedType.OPEN)
The rule logic can use the variables defined in the when clause and contain any kind of Java code. To work with item states, see the notes on the type system above - unfortunately, there is quite some ugly type casting involved in this. Besides normal Java operations, there are some statically imported methods available to your rule code, called "actions" in openHAB - see the next section on what features are provided through these actions.
Here is an example of a rule logic for the LHS from above:
boolean $isRaining = ((OnOffType)$event.getNewState()).equals(OnOffType.ON);
if($isRaining) {
say("It has started raining and your window is still open!");
} else {
playSound("ICanSeeClearlyNow.mp3");
}
In general, Drools can call any kind of static Java methods that are available on the classpath. openHAB comes with a set of useful actions for different use cases.
###Windows
###FreeBSD
- Classic UI
- iOS Client
- Android Client
- Windows Phone Client
- GreenT UI
- CometVisu
- Kodi
- Slack
- Chrome Extension
- Cosm Persistence
- db4o Persistence
- Amazon DynamoDB Persistence
- Exec Persistence
- InfluxDB Persistence
- JDBC Persistence
- JPA Persistence
- Logging Persistence
- mapdb Persistence
- MongoDB Persistence
- MQTT Persistence
- my.openHAB Persistence
- MySQL Persistence
- rrd4j Persistence
- Sen.Se Persistence
- SiteWhere Persistence
- AKM868 Binding
- AlarmDecoder Binding
- Anel Binding
- Arduino SmartHome Souliss Binding
- Asterisk Binding
- Astro Binding
- Autelis Pool Control Binding
- BenQ Projector Binding
- Bluetooth Binding
- Bticino Binding
- CalDAV Binding
- Chamberlain MyQ Binding
- Comfo Air Binding
- Config Admin Binding
- CUL Transport
- CUL Intertechno Binding
- CUPS Binding
- DAIKIN Binding
- Davis Binding
- Denon Binding
- digitalSTROM Binding
- DMX512 Binding
- DSC Alarm Binding
- DSMR Binding
- eBUS Binding
- Ecobee Binding
- EDS OWSever Binding
- eKey Binding
- Energenie Binding
- EnOcean Binding
- Enphase Energy Binding
- Epson Projector Binding
- Exec Binding
- Freebox Binding
- Freeswitch Binding
- Frontier Silicon Radio Binding
- Fritz AHA Binding
- Fritz!Box Binding
- FritzBox-TR064-Binding
- FS20 Binding
- Garadget Binding
- Global Caché IR Binding
- GPIO Binding
- HAI/Leviton OmniLink Binding
- HDAnywhere Binding
- Heatmiser Binding
- Homematic / Homegear Binding
- HTTP Binding
- IEC 62056-21 Binding
- IHC / ELKO Binding
- ImperiHome Binding
- Insteon Hub Binding
- Insteon PLM Binding
- IPX800 Binding
- IRtrans Binding
- jointSPACE-Binding
- KNX Binding
- Koubachi Binding
- LCN Binding
- LightwaveRF Binding
- Leviton/HAI Omnilink Binding
- Lg TV Binding
- Logitech Harmony Hub
- MailControl Binding
- MAX!Cube-Binding
- MAX! CUL Binding
- MiLight Binding
- MiOS Binding
- Mochad X10 Binding
- Modbus Binding
- MPD Binding
- MQTT Binding
- MQTTitude binding
- MystromEcoPower Binding
- Neohub Binding
- Nest Binding
- Netatmo Binding
- Network Health Binding
- Network UPS Tools Binding
- Nibe Heatpump Binding
- Nikobus Binding
- Novelan/Luxtronic Heatpump Binding
- NTP Binding
- One-Wire Binding
- Onkyo AV Receiver Binding
- Open Energy Monitor Binding
- OpenPaths presence detection binding
- OpenSprinkler Binding
- OSGi Configuration Admin Binding
- Panasonic TV Binding
- panStamp Binding
- Philips Hue Binding
- Picnet Binding
- Piface Binding
- PiXtend Binding
- pilight Binding
- Pioneer-AVR-Binding
- Plex Binding
- Plugwise Binding
- PLCBus Binding
- Powermax alarm Binding
- Primare Binding
- Pulseaudio Binding
- Raspberry Pi RC Switch Binding
- RFXCOM Binding
- RWE Smarthome Binding
- Sager WeatherCaster Binding
- Samsung AC Binding
- Samsung TV Binding
- Serial Binding
- Sallegra Binding
- Satel Alarm Binding
- SimpleBinary Binding
- Sinthesi Sapp Binding
- Smarthomatic Binding
- Snmp Binding
- Somfy URTSI II Binding
- Sonance Binding
- Sonos Binding
- Souliss Binding
- Squeezebox Binding
- Stiebel Eltron Heatpump
- Swegon ventilation Binding
- System Info Binding
- TA CMI Binding
- TCP/UDP Binding
- Tellstick Binding
- TinkerForge Binding
- Tivo Binding
- UCProjects.eu Relay Board Binding
- VDR Binding
- Velleman-K8055-Binding
- Wago Binding
- Wake-on-LAN Binding
- Waterkotte EcoTouch Heatpump Binding
- Weather Binding
- Wemo Binding
- Withings Binding
- XBMC Binding
- xPL Binding
- Yamahareceiver Binding
- Zibase Binding
- Z-Wave Binding
- Asterisk
- DoorBird
- FIND
- Foscam IP Cameras
- LG Hombot
- Worx Landroid
- Heatmiser PRT Thermostat
- Google Calendar
- Linux Media Players
- Rainforest EAGLE Energy Access Gateway
- Roku Integration
- ROS Robot Operating System
- Telldus Tellstick
- Zoneminder
- Wink Hub (rooted)
- Wink Monitoring
- Transformations
- XSLT
- JSON
- REST-API
- Security
- Service Discovery
- Voice Control
- BritishGasHive-Using-Ruby
- Dropbox Bundle
A good source of inspiration and tips from users gathered over the years. Be aware that things may have changed since they were written and some examples might not work correctly.
Please update the wiki if you do come across any out of date information.
- Rollershutter Bindings
- Squeezebox
- WAC Binding
- WebSolarLog
- Alarm Clock
- Convert Fahrenheit to Celsius
- The mother of all lighting rules
- Reusable Rules via Functions
- Combining different Items
- Items, Rules and more Examples of a SmartHome
- Google Map
- Controlling openHAB with Android
- Usecase examples
- B-Control Manager
- Spell checking for foreign languages
- Flic via Tasker
- Chromecast via castnow
- Speedtest.net integration