Understanding the syntax for calculations inside forEach loops

Hello

I’m still new to the openHAB system and syntax, and have some difficulty understanding how to solve the below challenge. - I have a group of dimmers, which I want to dim up or down, based on a calculation. However, the code inside the forEach parenthesis does not work, because I cannot figure out the right way of writing this.

Can anyone help?

val GroupItem dimmers = room_group.members.filter( i | i.name == room+"_Light_Dimmers" ).head
logInfo("RULE.lightTimerChanged", "dimmers: "+dimmers )


if ( receivedCommand == ON )
{
    dimmers.members.forEach( i | 
        var Number current_dimmer_level = i.state
        var Number new_dimmer_level = current_dimmer_level / 2
        i.sendCommand(new_dimmer_level)
    )
}
else if ( receivedCommand == OFF )
{
    dimmers.members.forEach( i | 
        var Number current_dimmer_level = i.state
        var Number new_dimmer_level = current_dimmer_level * 2
        i.sendCommand(new_dimmer_level)
    )
}

please try this:

if ( receivedCommand == ON )
    dimmers.members.forEach( i | i.sendCommand((i.state as Number) * 0.5) )
else if ( receivedCommand == OFF )
    dimmers.members.forEach( i | i.sendCommand((i.state as Number) * 2) )

Just to make it clear, the key difference between Udo’s version and yours is the i.state as Number. You have to cast the state. Sometimes the Rules DSL is not smart enough to do it for you. :frowning:

1 Like