Complex Rule optimization

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:

val String ruleIdentifier = "AlarmOff"

rule "Alarmanlage abschalten"


    Item itmTuerschloss changed from CLOSED to OPEN

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

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

        MichaelHome = MichaelHomeFB
        logInfo(ruleIdentifier, "\nMichael: Only using WiFi")
        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!')
        logInfo("AlarmOFF", "Alarmsystem was disabled, someone is at home.")


kind regards

  • 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

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 and for a more specific example for using this approach on time see Design Pattern: Time Of Day.

Generic Presence Detection may also be of interest.