forked from openhab/openhab1-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Reusable Rules via Functions
paphko edited this page Feb 4, 2015
·
1 revision
Quite often, there are rules with all the same logic, but operating on different items and with different parameters. Using lambda expressions, it is in fact possible to specify reusable and parameterizable functions. The example below can be used to control rollershutters:
- S_[name]_up is a switch (input) item to move it up
- S_[name]_down is a switch (input) item to move it down
- R_[name]_up is the switch item that actually moves it up
- R_[name]_down is the switch item that actually moves it down
Some constants and functions are defined at the top:
// total runtime of the rollershutters
val int ROLLO_TIMEOUT = 20
// timers to switch it off after the timeout
val java.util.Map<String, org.openhab.model.script.actions.Timer> rolloTimers = newHashMap
// lambda expression that can be used as a function (here: with 5 parameters)
val org.eclipse.xtext.xbase.lib.Functions$Function5 rolloLogic = [
org.openhab.core.library.items.SwitchItem relayItem,
org.openhab.core.library.items.SwitchItem relayItemOpposite,
java.util.Map<String, org.openhab.model.script.actions.Timer> timers,
String timerKey, int timeout |
// if current state is off
if (relayItem.state == OFF) {
// if opposite state is also off
if (relayItemOpposite.state == OFF) {
// switch it on!
relayItem.sendCommand(ON)
// if there is already a timer, cancel it
timers.get(timerKey)?.cancel
// now create a new timer for switching off
timers.put(timerKey, createTimer(now.plusSeconds(timeout)) [|
// switch it off afterwards, if it is not already off
if (relayItem.state == ON)
relayItem.sendCommand(OFF)
// remove timer from map because nothing is ON anymore
timers.remove(timerKey)
])
} else { // opposite is on
// switch opposite off!
relayItemOpposite.sendCommand(OFF)
}
} else { // it is already on
// switch it off
relayItem.sendCommand(OFF)
}
]
And the actual rules simply call the function rolloLogic
with their specific parameters:
rule "rollo kitchen up"
when Item S_Kitchen_up changed to ON then
rolloLogic.apply(R_Kitchen_up, R_Kitchen_down, rolloTimers, "Kitchen", ROLLO_TIMEOUT)
end
rule "rollo kitchen down"
when Item S_Kitchen_down changed to ON then
rolloLogic.apply(R_Kitchen_down, R_Kitchen_up, rolloTimers, "Kitchen", ROLLO_TIMEOUT)
end
rule "rollo bathroom up"
when Item S_Bathroom_up changed to ON then
rolloLogic.apply(R_Bathroom_up, R_Bathroom_down, rolloTimers, "Bathroom", ROLLO_TIMEOUT)
end
rule "rollo bathroom down"
when Item S_Bathroom_down changed to ON then
rolloLogic.apply(R_Bathroom_down, R_Bathroom_up, rolloTimers, "Bathroom", ROLLO_TIMEOUT)
end
This should simplify many rule definitions :-)
Installation
Community
- Support
- News Archive
- Presentations
- How to Contribute
- IDE Setup
- How to Implement a Binding
- How to Implement an Action
- Projects using openHAB
- User Interfaces
- Classic UI
- iOS Client
- Android Client
- GreenT UI
- CometVisu
- Automation
- Scripts
- Rules
- Actions
- Bindings
- AlarmDecoder Binding
- Anel Binding
- Asterisk Binding
- Astro Binding
- BenQ Projector Binding
- Bluetooth Binding
- Comfo Air Binding
- Config Admin Binding
- CUL Binding
- CUPS Binding
- DAIKIN Binding
- Davis Binding
- digitalSTROM Binding
- DMX512 Binding
- DSC Alarm Binding
- DSMR Binding
- Ecobee Binding
- EDS OWSever Binding
- eKey Binding
- Energenie Binding
- EnOcean Binding
- Epson Projector Binding
- Exec Binding
- Freebox Binding
- Freeswitch Binding
- Fritz AHA Binding
- Fritz!Box Binding
- FS20 Binding
- Global Cache 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
- IRtrans Binding
- jointSPACE-Binding
- KNX Binding
- Koubachi Binding
- Leviton/HAI Omnilink Binding
- Lg TV Binding
- MAX!Cube-Binding
- MAX! CUL Binding
- MiLight Binding
- MiOS Binding
- Modbus TCP Binding
- MPD Binding
- MQTT Binding
- MQTTitude 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
- Philips Hue Binding
- Piface Binding
- pilight Binding
- Pioneer-AVR-Binding
- Plugwise Binding
- PLCBus Binding
- Primare Binding
- Pulseaudio Binding
- RFXCOM Binding
- Samsung AC Binding
- Samsung TV Binding
- Serial Binding
- Satel Alarm Binding
- Snmp Binding
- Somfy URTSI II Binding
- Sonos Binding
- Squeezebox Binding
- Swegon ventilation Binding
- System Info Binding
- TCP/UDP Binding
- Tellstick Binding
- TinkerForge Binding
- Tivo Binding
- VDR Binding
- Velleman-K8055-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
- Application Integration
- Asterisk
- Google Calendar
- Linux Media Players
- ROS Robot Operating System
- Telldus Tellstick
- Zoneminder
- Rooted Wink Hub
- Persistence
- db4o Persistence
- rrd4j Persistence
- MySQL Persistence
- MongoDB Persistence
- Sen.Se Persistence
- Cosm Persistence
- Logging Persistence
- Exec Persistence
- MQTT Persistence
- InfluxDB Persistence
- JPA Persistence
- Misc
- Transformations
- REST-API
- Security
- Service Discovery
- Dropbox Bundle
- Voice Control
Samples
- Item definitions
- Sitemap definitions
- Binding configurations
- Comfo Air Binding
- Nest Binding (new)
- Nest Thermostat
- Rollershutter Groups
- Squeezebox
- WAC Binding
- WebSolarLog
- Rules
- Misc. rule examples
- Alarm Clock
- Convert Farenheit to Celcius
- Taking Rules to New Heights
- Reusable Rules via Functions
- Items, Rules and more Examples of a SmartHome
- REST Examples
- Tips and Tricks
- Scripts
- Syntax highlighting for external editors
- Update-Scripts
- Controlling openHAB with Android
- Usecase examples
Release Notes