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

Nilan VP18 vs OpenHABRating: 
70.8%

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: 
73.8%

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.

Advanced disk cloningRating: 
0%

The classic disk cloning tool in linux is the dd command. This command creates a image of disk or extracts the contents from the image to disk. The dd tool needs to be handled carefully and you need to understand its use. We'll show a more advanced technique of creating a disc image that contains unallocated space. A classic example is when I have a Raspbian operating system installed on a 250 GB disc, but there's only a tiny boot partition (e.g. sda1) and my own operating system partition (sda2) which together have 15 GB. Instead of saving a 250 GB image, I would like to save only two partitions with 15 GB, so that the image is smaller and can be expanded to 16 or 32 GB flash.

iOS OpenVPN Connection SettingsRating: 
60%

In this article you will find detailed instructions on how to connect to OpenVPN from iOS devices.

Android OpenVPN Connection SettingsRating: 
73.3%

In this article you will find detailed instructions on how to connect to OpenVPN on Android.

OpenVPN on MikroTik routersRating: 
100%

If you have a smart-home, NAS, etc., it's a good idea to think about safe access to your network when you are not at home. Currently I recommend commercial router like Turris or a cheaper option of MikroTik routers. In the other hand, Raspberry Pi can be used as OpenVPN server too. In this article, we will show how to set up secure access to your home network from the internet using OpenVPN on MikroTik routers.

Video intercom Doorbird in OpenHABRating: 
0%

Native support for OpenHAB is provided by video intercom by Doorbird company. The solution is powered and connected by a UTP cable and has its own IP address to make it easier for you to install at the gate. OpenHAB is then able to import images from the intercom, visit history, manage gate opening on the button and by the phone, do not disturb feature during night, alarm feature etc. In the instructions we guide you how to use OpenHAB with Doorbird D101.