Translate from javascript to rules DSL

You could recreate your Items to follow a set naming pattern.

If you know it will always be “_Lamp”, for example you can use replace.

i.name.replace("_Lamp", "_ColorTemperature")

They are all parts of the Java Classes you are working with.

Method Where it’s maintained Link to JavaDocs
.name openHAB, part of Item GenericItem (openHAB Core 4.2.0-SNAPSHOT API)
.split Core Java, part of String String (Java SE 11 & JDK 11 )
.get(0) Core Java, Rules DSL converts arrays to an ArrayList. ArrayList (Java SE 11 & JDK 11 )
.head Part of Xtend’s way to process Lists. Their docs are all over the place though. It’s a layer on top of Java’s Stream API. I’ve consolidated this at the following DP. Design Pattern: Working with Groups in Rules

Doh, I’ve been away from Rules DSL for too long.

Like I said, save each step to it’s own variable instead of trying to do everything all on one line.

val members = gAll_Lights_Brightness.members
logInfo("test", "All members: " + members)
val filtered = members.filter[i | i.state as Number > 0]
logInfo("test", "Filtered: " + filtered)
filtered.forEach[item |
    logInfo("test", "Processing Item " + item.name)
    // here is where it's better to do String manipulation to convert item.name to the ColorTemperature
    // Item's name, even if it means you have to rename those Items to fit the pattern
    // sendCommand(item.name.replace("Lamp", "ColorTemperature"), newState.toString)
    // or maybe it's as simple as
    // sendCommand(item.name + "_ColorTemperature", newState.toString)

    // As explained in the working with groups DP linked above, use findFirst instead of filter and head
    val commandItem = gAll_Lights_ColorTemperature.members.findFirst[ j | j.name.contains(item.name) ]
    logInfo("test", "Color Item found is " + commandItem) // if this is null the Item was not found
    commandItem.sendCommand(newState)
]

Break it up and log each step of the way. If you do half a dozen operations all on the same line and it doesn’t work it’s impossible to figure out why.

1 Like