[SOLVED] Group name function question

(M) #1
  • 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

0 Likes

(Anders Alfredsson) #2

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.

0 Likes

(M) #3

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

0 Likes

(Udo Hartmann) #4

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.

0 Likes

(M) #5
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'

0 Likes

(Angelos) #6

small typo: Member of...

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

0 Likes

(Udo Hartmann) #7

Ooops, sorry…

Changed to upper case :wink:

0 Likes

(Angelos) #8

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

0 Likes

(Udo Hartmann) #9

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

(M) #10

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

0 Likes

(Vincent Regaud) #11

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
0 Likes

(M) #12

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

0 Likes

(Vincent Regaud) #13

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.

0 Likes

(M) #14

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

0 Likes

(Vincent Regaud) #15

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

0 Likes

(M) #16

label is not updated and no error in the log

0 Likes

(Vincent Regaud) #17

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
0 Likes

(M) #18

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

0 Likes

(Vincent Regaud) #19

Changed the two items:

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