Programming question

As soon as you start heading down this path it is a strong sign you are outgrowing Rules DSL. All of the other options easily support this including Blockly. Rules DSL doesn’t support this simple use case very well.

You have two options to do this with Rules DSL.

  1. One rule to handle them all. Put the Items into a Group, use a member of rule trigger, and use the implicit variables to create one rule that handles all the members of the Group in the same way. See Design Pattern: Associated Items for steps you can take to make doing this easier through careful Item naming (for example).
//---------------------------------------------------------------------------------------------------------------------------------------------
rule "Keuken kookplaat lampen aan" 
//---------------------------------------------------------------------------------------------------------------------------------------------
when
    Member of Lampen changed // Lampen has all the Switch Items you want processed by this rule 
then
    Kooklampen.sendCommand(newState) // Sending a command to a Group forwards to all members of the group
end 

//---------------------------------------------------------------------------------------------------------------------------------------------
rule "Keuken lampkleur kookplaat"
//---------------------------------------------------------------------------------------------------------------------------------------------
when
     Member of Lampen received command // all Items that should trigger this rule 
then
    Kooklampen.sendCommand(receivedCommand) // there is no need to recreate the HSBType
end

Note that in the second case, you don’t really need the rule. Just send the command to the Group Item in the first place.

  1. Use a lambda. See Reusable Functions: A simple lambda example with copious notes. Note the limitations:

    • must be in the same file
    • cannot have more than 7 arguments
    • cannot reference other global variables from inside a global lambda; you must pass them as an argument

You also have access to Rules DSL Scripts but these are:

- defined in a separate file
- invoked using [`callScript`](https://www.openhab.org/docs/configuration/actions.html#openhab-subsystem-actions)
- cannot use import statements
- cannot return anything
- everything needs to be passed into them

Because of that their use is significantly more limited.

1 Like