Openhab Rules - Grouping of functions/procedures in one rule

Hello,

I’m trying to automate my garden irrigation, containing different zones/plants with different water requirements.

And all the zones/plants need different water requirements.

To fulfil this, I need to create Lambda procedures/functions to be used as utility functions, which can be invoked from different rule files.

Is there a way to use one single (.rules) file as a utility file? if possible, how do we invoke the functions from other rules file?

Thanks for your help.

Regards,
Bas

You cannot invoke lambdas outside of the file where they are defined.

However you could use a script file with proxy items

Thats awesome. Thanks…

Is there a link which explains script file with proxy items?

I’ll give it a try. And will update how it goes.

Scripts cannot receive arguments nor can they return anything so they will probably be of limited use in this case.

http://docs.openhab.org/configuration/rules-dsl.html#scripts

Proxy Items are described here:

Upon thinking about it I’m not so sure that I would implement this using lambdas. I think it can be implemented with just a couple of rules. You would:

  • have an item that you send a command to that triggered the main rule
  • a cancel flag as described here
  • the main rule has an if clause to see if the controller switch is ON or OFF. If OFF set the cancel flag. If ON execute the irrigation logic
  • The irrigation logic
    • starts the first zone
    • has a while loop that periodically checks for the cancel, if cancelled the irrigation stops
    • inside the while loop check to see if it is time to move to the next zone, when it is the off the current zone and start the next. Keep the amount of time to run each zone in a group and use Item names so it is ready to construct the name on one from the other. Examples are [here](Design Pattern: Working with Groups in Rules
  • set up persistence on the zones so we can call lastUpdate

So, for example:

int zone =0
while(!cancelled && zone < gZones.members.size){
    var currZone = gZones.members.filter[z|z.name == "IrrigationZone"+zone].head as SwitchItem
    var Number timeMins = gZoneTimes.members.filter[t|t.name == currZone.name+"Time"].head.state as Number
    if(now.minusMinutes(timeMins).isBefore(currZone.lastUpdate.millis) {
        zone = zone +1
        currZone.sendCommand(OFF)
    }

    else {
        if(currZone.state != ON) currZone.sendCommand(ON)
        Thread::sleep(5000)
    }
}

gZones.members.forEach(z|z.sendCommand(OFF)

I just tried in the above on my phone. There are probably errors.