[SOLVED] PowerOFF only powered ON lights in a group

I have a group where I linked all my lights and I can use it to power OFF all the lights. BUT when I use it, a lot of non necessary logs and command are sent to the bus. So I’d like to design a pattern where only the powered ON lights will receive the power OFF command.
my idea is a group of lights on .items file:

Group:Switch:OR(ON, OFF)                gLight                 "all the light"
Switch                                  Luce_Bagnetto          "Light1"           <light>         (gLight)
Switch                                  Luce_Bagnetto          "Light2"           <light>         (gLight)

rules:

//rule that switch OFF only powered on lights
when
      Item away received command ON
than
   gLight.members.filter[ i | i.state == ON]
                                             //here I miss an easy way to sendCommand OFF
end

maybe someone know a more intelligent way to address this problem.
Thanks Domenico

Here is a topic you may find helpful. :wink:

2 Likes

Of course I read It. but I miss some step because I don’t have experience on programming.

I’m trying following rule:

rule "switch OFF"
when
        Item away received command ON
then
        if (gLight.state == ON)     {
                gLight.members.filter[ i | i.state == ON].forEach[i | i.sendCommand(OFF) ]
                logInfo("A letto", "spengo le luci accese")
              }
end

What do you see in the logs when you give the item away an ON command?

You can also simply give an off command to all lights, even if their OFF, with no need to check the state first. :wink:

that rule work but only for simple light, not for dimmers.

I had a problem with this approach because I made a rule that first poweroff all the lights and than powerON one light. But power off sequence need more time and it shutdown the light powered by the rule… :neutral_face:

I should have said a rule to turn all lights in a group off and not every light in home. :smile:
Doing this you will need a rule for each group that you want to control.

I should make a group with all the lights minus that light I want to power on.
But I thought it’s more professional a specific pattern :laughing:

That’s what I like about OH, you can start simple, add complex rules as you go and without losing control of your devices. FYI make sure you have a backup and verify it works as you don’t wanna lose all your hard work. :wink:

It doesn’t work for Dimmers because Dimmers don’t carry ON or OFF as a state. They carry a PercentType. So i.state == ON will always be false for a Dimmer.

However, Dimmers understand ON and OFF so you can use this instead in your filter

i.getStateAs(OnOffType) == ON

That will cause the Dimmer to convert it’s PercentType to ON/OFF and your filter will work.

Great tips! this worked :star_struck:
Edit:
I post the complete rule for other people on the community:

rule "A letto routine"
when
        Item A_Letto received command ON
then
        if (gLight.state == ON)     {
                gLight.members.filter[ i | i.getStateAs(OnOffType) == ON].forEach[i | i.sendCommand(OFF) ]
                logInfo("A letto", "spengo le luci accese")
              }
end