# Complex Rule optimization

**URL:** <https://community.openhab.org/t/complex-rule-optimization/82091>\
**Category:** Scripts & Rules\
**Created:** [September 17, 2019, 9:22pm UTC](https://community.openhab.org/t/complex-rule-optimization/82091 "2019-09-17T21:22:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bredmich](https://community.openhab.org/user_avatar/community.openhab.org/bredmich/32/30457_2.png) [@Bredmich](https://community.openhab.org/u/Bredmich)\
**Post date:** [September 17, 2019, 9:22pm UTC](https://community.openhab.org/t/complex-rule-optimization/82091/1 "2019-09-17T21:22:28Z")

</div>

Hi there,

i´m currently struggling to optimize some of my rules with complex if checks.  
The checks are pretty much the same over some of my rules and always bother me.

What i want to achieve:  
I´m currently using two methods to check for presence: iCloud location and WiFi connection  
As the iCloud binding sometimes has problems to keep the location updated, i tried to add a check before using the information.  
The outcome is used to shutdown motion detection for cameras or let an Echo Dot welcome someone who´s coming home.  
I already thought about a more global way to check the iCloud timestamp but didn´t had a good solution in mind.

I know that using values inside of if statements isn´t a good idea, but i didn´t had any better solution as i´m no developer.

I´m happy for every idea that makes this rule simplier or even change it completly, e.g. more globally to make it easier to use in multiple rules.

Current rule:

```php
val String ruleIdentifier = "AlarmOff"

rule "Alarmanlage abschalten"

when

    Item itmTuerschloss changed from CLOSED to OPEN

then
    // Checking iCloud timestamps
    val timeCheck = new DateTime(now().minusHours(1).toString)
    val lastUpdateM = new DateTime(iPMichael_LastUpdate.state.toString)
    val lastUpdateJ = new DateTime(iPJohanna_LastUpdate.state.toString)
    val updateM = (lastUpdateM.isBefore(timeCheck))
    val updateJ = (lastUpdateJ.isBefore(timeCheck))

    // Checking iCloud and 
    val JohannaHomeiP = (iPJohanna_Home.state == ON)
    val JohannaHomeFB = (fbJohanna.state == OPEN)
    var JohannaHome = (NULL)
    val MichaelHomeiP = (iPMichael_Home.state == ON)
    val MichaelHomeFB = (fbMichael.state == OPEN)
    var MichaelHome = (NULL)

    if(updateJ)
    {
        JohannaHome = JohannaHomeFB
        logInfo(ruleIdentifier, "\nJohanna: Only using WiFi")
    }
    else
    {
        JohannaHome = (JohannaHomeiP || JohannaHomeFB)
        logInfo(ruleIdentifier, "\nJohanna: Using iCloud and WiFi")
    }

    if(updateM)
    {
        MichaelHome = MichaelHomeFB
        logInfo(ruleIdentifier, "\nMichael: Only using WiFi")
    }
    else
    {
        MichaelHome = (MichaelHomeiP || MichaelHomeFB)
        logInfo(ruleIdentifier, "\nMichael: Using iCloud and WiFi")
    }

    val AlarmsystemON = (itmAlarmsystem.state == ON)
    val AlarmsystemOFF = (itmAlarmsystem.state == OFF)

    if(((JohannaHome == true) || (MichaelHome == true)) && AlarmsystemOFF)
    {
        echoFlur_TTS.sendCommand('Cameras turned off')
        logInfo("AlarmInfo", "Alarmsystem wasn´t armed, just turning off cameras.")
    }

    if(((JohannaHome == true) || (MichaelHome == true)) && AlarmsystemON)
    {
        echoFlur_TTS.sendCommand('Alarmsystem disabled!')
        itmAlarmsystem.postUpdate(OFF)
        logInfo("AlarmOFF", "Alarmsystem was disabled, someone is at home.")
    }

end

```

kind regards  
Michael

* * *

- Platform information:
  - Hardware: Raspberry Pi 4 Model B Rev 1.1 - 4GB
  - OS: Raspbian GNU/Linux 10 (buster) _(latest raspbian image)_
  - Java Runtime Environment:
    - openjdk version “1.8.0\_222”
    - OpenJDK Runtime Environment (Zulu8.40.0.178-CA-linux\_aarch32hf) (build 1.8.0\_222-b178)
    - OpenJDK Client VM (Zulu8.40.0.178-CA-linux\_aarch32hf) (build 25.222-b178, mixed mode, Evaluation)

  - openHAB version: 2.4.0-1

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [September 17, 2019, 11:04pm UTC](https://community.openhab.org/t/complex-rule-optimization/82091/2 "2019-09-17T23:04:42Z")

</div>

I don’t have time to do a full analysis but the general approach is to put the test in a separate rule that there when ever one of the values changes, and put the result into an item. See [Design Pattern: Separation of Behaviors](https://community.openhab.org/t/design-pattern-separation-of-behaviors/15886) and for a more specific example for using this approach on time see [Design Pattern: Simple State Machine (e.g. Time Of Day)](https://community.openhab.org/t/design-pattern-time-of-day/15407).

[Generic Presence Detection](https://community.openhab.org/t/generic-presence-detection/17878) may also be of interest.
