[SOLVED] Group name function question

  • Platform information:
    • Hardware: raspberry pi3b+
    • OS: rasbian stretch
    • Java Runtime Environment: openjdk
    • openHAB version: 2

Right now I determine how many lights I have in a group with

Group:Switch:OR(ON,OFF) group_house_switch_light

it it possible to display as a groupname something like

“[Grouepname] 3 switched on out of 4”

Thanks

You should be able to use

Group:Number:COUNT("ON") group_house_switch_light "[%d] of 4 lights on"

to get the number of items that is currently on. Note that you have to hard-code the total number of items, so if you add another light to the group you have to change that manually.

Not sure if I got it completely correct, but see here for more info.

Okay so it is not possible to count the total number of lights by function???

In fact, the dynamic part of the label is not that flexible.

Group:Switch:OR(ON,OFF) group_house_switch_light "Number of Lights on [%d]"

would suffice to get the correct answer, but the total number of members can’t be displayed this way.
You could build a rule like this:

rule "count members"
when
    Member of gGroup changed
then
    var Integer ctOff = gGroup.members.filter[m|m.state == OFF].size
    var Integer ctOn = gGroup.members.filter[m|m.state == ON].size // maybe var Int instead of var Integer?
    var Integer ctSum = gGroup.members.size
    var lOn = ctOn.toString + " out of " + ctSum.toString + " are switched ON"
    var lOff = ctOff.toString + " out of " + ctSum.toString + " are switched OFF"
end

Afaik there are commands to change the label through a rule, but I never used them, so I don’t know if this will work as ecpected. Of course you could use a String Item to store the label and to display it.

rule "count members"
when
    member of group_house_switch_light changed
then
    var Integer ctOff = group_house_switch_light.members.filter[m|m.state == OFF].size
    var Integer ctOn = group_house_switch_light.members.filter[m|m.state == ON].size // maybe var Int instead of var Integer?
    var Integer ctSum = group_house_switch_light.members.filter[m|true].size
    var lOn = ctOn.toString + " out of " + ctSum.toString + " are switched ON"
    var lOff = ctOff.toString + " out of " + ctSum.toString + " are switched OFF"
end

this my rule…

but the error says

no viable alternative at input 'member'

small typo: Member of...

by the way: you need OH2.3.0 for this to work
see: Features for Rules that work with Groups

Ooops, sorry…

Changed to upper case :wink:

Udo: do you remember if 2.3.0 Stable has this included?
the post that I linked is from Feb/18 and OH2.3.0 Stable was released on May/18 so it should include this

As far as I remember Member of was introduced before 2.3.0 stable.

It is possible to use

when
    Item gGroup received update
then

instead, but this will always trigger gGroup.members.size + 1 times each time one member is updated.

1 Like

next error ist

2018-11-30 08:14:36.990 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'default.rul$
The value of the local variable lOn is not used
The value of the local variable lOff is not used

because it is not set to the group how do I do this???

it only displays me a number not the whole text right now

You will need to display that text that is built in the two variables.
This is not an error. Just info from the rule parsing engine that the variables are not used.
Create 2 new items:

String NumberOfLightsON
String NumberOfLightsOFF

Change your rule:

rule "count members"
when
    Member of group_house_switch_light changed
then
    var Integer ctOff = group_house_switch_light.members.filter[m|m.state == OFF].size
    var Integer ctOn = group_house_switch_light.members.filter[m|m.state == ON].size // maybe var Int instead of var Integer?
    var Integer ctSum = group_house_switch_light.members.filter[m|true].size
    var lOn = ctOn.toString + " out of " + ctSum.toString + " are switched ON"
    var lOff = ctOff.toString + " out of " + ctSum.toString + " are switched OFF"
    NumberOfLightsON.postUpdate(lOn)
    NumberOfLightsOFF.postUpdate(lOff)
end

Sitemap:

Text item=NumberOfLightsON
Text item=NumberOfLightsOFF

but how can i add this directly to a group??

What do you mean?
Why would you want to add that to a group?
These two items contain the String that you wanted displayed in your first question.

My group is now this

Group:Switch:OR(ON,OFF) group_house_switch_light "Lichtschalter [%d]" <lightbulb> (group_house)

But I want to see a text with the group like “3 Lichtschalter von 4 sind an” instead only the number of light switches

That’s exactly what the 2 items above do. Put them in a sitemap and have a look.

label is not updated and no error in the log

Which label?
Add some logInfo in the rule to see if it runs at all:

rule "count members"
when
    Member of group_house_switch_light changed
then
    var Integer ctOff = group_house_switch_light.members.filter[m|m.state == OFF].size
    var Integer ctOn = group_house_switch_light.members.filter[m|m.state == ON].size // maybe var Int instead of var Integer?
    var Integer ctSum = group_house_switch_light.members.filter[m|true].size
    var lOn = ctOn.toString + " out of " + ctSum.toString + " are switched ON"
    var lOff = ctOff.toString + " out of " + ctSum.toString + " are switched OFF"
    logInfo("lOn", lOn)
    logInfo("lOff", lOff)
    NumberOfLightsON.postUpdate(lOn)
    NumberOfLightsOFF.postUpdate(lOff)
end

in the log i see it but the label is stil displayed as “NumberOfLightsON” no text

Changed the two items:

String NumberOfLightsON "[%s]"
String NumberOfLightsOFF "[%s]"