Changing values of all members of a group using a Javascript-rule

I’d like to change a value of all members of a group (specificially: set the brightness of all lamps to 0) with a Javascript rule.
Basically these lines do the job:

leuchte= "LeuchteHuegross_Farbe";
x = items[leuchte].toString().split(",")[0] + "," + items[leuchte].toString().split(",")[1]+"," + "0"; 
events.sendCommand(leuchte,x);

Now I could copy that for each lightbulb and change the name, which is obviously not very elegant.

I’ve read that there’s a way to apply an action to all members of a group but can’t figure out how to do it in Javascript.
Here (But How Do I…? — openHAB Helper Libraries documentation) is indicated that it might not work in that language, but maybe that’s outdated or is there a different way to achieve this?

What are you really trying to do here?
We can interpret that as “turn all lamps off” (brightness=0), which is easy enough.
events.sendCommand(yourGroup,OFF);

When your devices report their new state as off, it’s likely the HS part of HSB gets zeroed as well, depending on the binding and device technology.
Does that happen already with your individual commands?

1 Like

Thank you, this indeed works fine for the current task.
However, I’d be interested in a general solution. Let’s say I wanted to set the brightness to 30 (or the color to a specific value), then this wouldn’t work. And there might be many rules where changing entire groups instead of individual items would be handy.
Is there such a solution or is it just not possible with Javascript?

In truth you can send Dimmer style commands to Color Items, just like ON/OFF.
events.sendCommand(yourGroup,30);
but that’s not your point.
Let’s say, add 10 to existing H value, where they may not all be the same to begin with

No, there are no pre-written functions to “select all members of a Group and add 10 to H value”.
It is not even possible to just update all members of a Group in one hit. Only send a command, which gets propagated out to all members (and might have eventual consequences on the states).
This what rules are for.

I think what you are looking for is to iterate through all members of a Group and do whatever you do to each one in turn.
I have no idea about javascript but searching with the magic word gives -

where forEach is the key method to use.

1 Like

Thank you, Rossko,

this looks like what I’m interested in. When the need arrives (rather sooner than later), I now know where to look.

You solved a FUTURE problem! You are way ahead of us!

1 Like