Kategorie
Part 4 - Configuration - Rules
Part 4 - Configuration - Rules
In this article, we look at the rules in OpenHAB 3. The rules are rensposibles for entire ecosystem of actions responding to different states. In OpenHAB 3, the rules can be created manually via the user interface or via the .rules files. For our model house, we provide examples in Main UI. Same can be done via .rules files or copy-paste in Main UI.
What we'll need
Raspberry Pi 4 B with case (optional DIN holder)
microSD card (SSD disc in production)
Rules
You can find Rules under Settings tab in menu.
You can start a new rule by clicking on the blue + icon at the bottom right. We demonstrate an example of a rule in the following example.
Plan: When leaving the house we want to turn off all lights, turn off socket circuits (e.g. kettle, various chargers, etc.) but of course leave the fridge, ventilation or heating in normal states.
In our case we create a rule Alarm Full - All off
When
Set when the rule should be executed. In our case, when the Alarm state changes to number 1. This state is translated as Full in the Alarm item view (Add metadata -> State description).
Then
What should happen. In our case, lights, sockets, heating, etc. should be switched off. So add individual records of which items you want to switch off.
Tip: You can click on Code if yu prefer to copy-paste a text. Simply copy the text of the items and change the pattern
But only if
Here you can add another condition that the actions should only be performed if the condition is met.
Tip: Of course, you can insert your own script in the Then section of Rules. Either use the block graphic interface or use the code of the script. For me, it's best to use a text code. In the following case, we'll show you how we'd handle turning off the lights 30 seconds after leaving.
In Then, choose Add action -> Run script -> Rule DSL and add something like this:var timer_Delay = createTimer(now.plusSeconds(30), [ |
sendCommand(L011, OFF)
sendCommand(L012, OFF)
sendCommand(L013, OFF)
] )
In our model house Pi-Home there are over 50 pre-prepared rules. Let's look for som examples we used. We are providing a "Code" you can directly copy paste to your Rules in OpenHAB 3 UI.
Example of Buttons -> Lights rule (B111)
configuration: {}
triggers:
- id: "2"
configuration:
itemName: B111
state: ON
previousState: OFF
type: core.ItemStateChangeTrigger
conditions: []
actions:
- inputs: {}
id: "1"
configuration:
type: application/vnd.openhab.dsl.rule
script: |2
if(L111.state == ON){
sendCommand(L111, OFF)
}else{
sendCommand(L111, ON)
}
type: script.ScriptAction
Example of PIR ->Light rule (SP111)
configuration: {}
triggers:
- id: "1"
configuration:
itemName: SP111
type: core.ItemStateChangeTrigger
- id: "5"
configuration:
itemName: SP111
type: core.ItemStateUpdateTrigger
conditions:
- inputs: {}
id: "2"
configuration:
itemName: SP111_trigger
state: ON
operator: =
type: core.ItemStateCondition
- inputs: {}
id: "4"
configuration:
itemName: SunPhase
state: DAYLIGHT
operator: "!="
type: core.ItemStateCondition
actions:
- inputs: {}
id: "3"
configuration:
type: application/vnd.openhab.dsl.rule
script: |2
if(SP111.state == ON){
sendCommand(L111, ON)
}else{
sendCommand(L111, OFF)
}
type: script.ScriptAction
Example of PIR ->Alarm rule (SP111ALARM)
configuration: {}
triggers:
- id: "1"
configuration:
itemName: SP111
state: ON
type: core.ItemStateChangeTrigger
conditions: []
actions:
- inputs: {}
id: "3"
configuration:
type: application/vnd.openhab.dsl.rule
script: |-2
if(Alarm.state == 1 || Alarm.state == 2){
sendCommand(Panic, 2)
}
type: script.ScriptAction
Example of H111 Thermostat Room Rule
configuration: {}
triggers:
- id: "1"
configuration:
itemName: ST111
type: core.ItemStateUpdateTrigger
conditions:
- inputs: {}
id: "2"
configuration:
itemName: H111MODE
state: "1"
operator: =
type: core.ItemStateCondition
actions:
- inputs: {}
id: "3"
configuration:
type: application/vnd.openhab.dsl.rule
script: |-
// Rule for thermostat
if (ST111.state instanceof Number) {
val currentTemperature = ST111.state as Number
val targetTemperature = H111SET.state as Number
if (currentTemperature < targetTemperature - 1) {
H111.sendCommand(ON) // turn on the heating
} else if (currentTemperature > targetTemperature + 1) {
H111.sendCommand(OFF) // turn off the heating
}
}
// Protect floor from overheating
if (SF111.state instanceof Number) {
val currentFloorTemperature = SF111.state as Number
if(currentFloorTemperature > 32) {
H111.sendCommand(OFF)
}
}
type: script.ScriptAction
Add comment