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)

SSD disk (or better quality microSD card but this is not as reliable as SSD disc in production

Rules

You can find Rules under Settings tab in menu.

OpenHAB 3 - Rules

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).

OpenHAB - Alarm rule

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.

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



Add comment

Nejlepší články z blogu

MikroTik - Winbox, DHCP, Ranges
Basics - Winbox, DHCPRating: 
72%

In this series, we will look at the step-by-step setup of MikroTik devices for home users or a small business (up to 25 people). In the first article, we will focus on the initial setup - we will download Winbox and set up DHCP for the primary network and guest network. Similarly, we will also adjust the WiFi settings.

Alarm Smart Home PIR
Alarm from existing PIR sensors in a smart home.Rating: 
0%

In a your smart home, PIR sensors may not only be used to switch lights on and off based on motion, it is possible to utilize these sensors to detect the presence of motion in a particular room. This information can be used to create a relatively reliable uncertified home security system. In this guide, you will find the logic for how this can work in the OpenHAB software in our model smart home.

NFC Tag Example in Smart Home
NFC tags in smart homeRating: 
80%

NFC (Near Field Communication) tags are small plastic or paper stickers that can be used to automate various functions in the smart home. In this article, we will show you examples of use and a guide on how to write an action on an NFC tag using a mobile phone.

WireGuard iOS
WireGuard on iOS devicesRating: 
85%

In this article, you will find a detailed guide on how to connect to WireGuard VPN from iOS.

WireGuard on Android device
WireGuard on Android devicesRating: 
0%

In this article, you will find a detailed guide on how to connect to WireGuard VPN from Android.

WireGuard on MikroTikRating: 
80%

This article describes the self setup of the WireGuard VPN protocol on MikroTik devices with RouterOS version 7 and higher. This phenomenal VPN is very fast, secure, and easily configurable in a home environment. It can be said that it is currently the best VPN for home use available.

Nilan VP18 vs OpenHABRating: 
67.2%

We provide tutorial for the connection of the active Nilan recuperation unit, in our case the VP18 K WT type with OpenHAB 3. By communicating via Modbus it is possible to read the whole range of values, but mainly also to control the unit through OpenHAB. This allows us to remotely or programmatically change the performance and ventilation settings depending on the mode (night, cooking, etc.)

PiHome - Chytry dum mereni spotreby elektriny
Energy meter in smart homeRating: 
70.4%

We are providing a simple step-by-step tutorial how to install home energy meter. We did not find any MQTT device, so we buy Aeotec Home Energy Meter as we are already have a Z-Wave controlller in our smart home.

Centrální tablet pro chytrý dům
Wall tablet to controll your smart homeRating: 
100%

Wall tablet or older smartphone is perfectly suited as a central indicator of the state of the home, as well as controlling it. It usually replaces the video doorbell and as example, you can play your favorite music or read recipes on it during cooking in your kitchen. In the article, we describe a functional example of such a "wall" tablet and its settings in so-called kiosk mode so, display doesn't run at night and only reacts when someone is nearby.

Jablotron and smarthome synchronization
Connecting Jablotron with OpenHABRating: 
80%

In this article we will show you how to install Jablotron binding in OpenHAB. This binding helps us to synchronize states from Jablotron alarm to OpenHAB. Typically, you can force to switch off lights, close water pipe and circulation, switch off sockets or whatever you want to do when you leave home and set alarm.