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.

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

Nejlepší články z blogu

Tailscale - remote access without public IP
Tailscale - remote access without public IPRating: 
90%

The Tailscale service solves access between individual devices if you don't have a public IP address. It's free for one user with support for up to 100 devices. We'll show you how to install Tailscale on a Raspberry Pi that you'll have at home and on a client (Android, iPhone) from which you'll want to access the home Raspberry Pi. Our example will be remote access to OpenHAB running on a Raspberry Pi. However, the same setup applies to many other applications - home NAS, PiHole, Home Assistant, Domoticz, NextCloud, and others.

Shelly OpenHAB MQTT
Shelly vs OpenHABRating: 
90%

The Shelly brand is known for its products that primarily communicate over WiFi, including smart plugs, relay switches, blinds control relays, and many other devices. One of the advantages for deployment is the ability to both read and control these devices using the universal MQTT protocol. Across existing add-ons for both OpenHAB and Home Assistant, we will demonstrate how to use Shelly devices without installing any additional extensions.

Victron & OpenHAB
Victron vs Smart HomeRating: 
0%

In this post, we will show you how to retrieve information from a photovoltaic power plant by Victron. We will connect to the Cerbo unit via MQTT. Based on these values, we can control various appliances (heating, boiler, etc.) and prevent the battery from being drained when they don't need to be.

Smart Home GoodWe inverter
Smart Home vs GoodWeRating: 
50%

In the post, we will demonstrate step by step how to communicate directly with the GoodWe inverter in a smart home setup and obtain real-time information (unlike the SEMS portal). This information is essential if we want to react to current parameters in a smart home, such as activating additional cooling or controlling a socket with a various load.

Voice control smart home
Voice control of the houseRating: 
60%

In this article, we will connect the Amazon Echo Dot voice assistant with open source home automation. We won't be using OpenHAB Cloud, so everything runs locally. In this case, a few additional settings are necessary, but the result is worth it!

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

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

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

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