[SOLVED] Help with String+""

I’m trying to create a string that names out who is home, this was my attempt. Can anyone tell me a better way to do this or what I am doing wrong? The output state is blank “”

rule "Currently Home"
when
    Item MichellePhoneActual received update or Item RonaldPhoneActual received update or Item RyanPhoneActual received update or Item TylerPhoneActual received update
then
    if(MichellePhoneActual == ON){
        WhoHome = "Michelle"
    }
    if(RonaldPhoneActual == ON){
        WhoHome = WhoHome+"Ronald"
    }
    if(RyanPhoneActual == ON){
        WhoHome = WhoHome+"Ryan"
    }
    if(TylerPhoneActual == ON){
        WhoHome = WhoHome+"Tyler"
    }
    PeopleHome.sendCommand(WhoHome as String)
end

You can use StringBuilder:

There’s a nice utilisation of this approach also here:

1 Like

Another alternative is:

  • Put your Items into a Group, let’s call it PhonesActual
  • Trigger the rule using Member of PhonesActual
  • Use filtet and map/reduce as described in the link Paul provided
rule "Currently Home"
when
    Member of PhonesActual changed
then
    val whoHome = PhonesActual.members.filter[ p | p.state == ON ].map[ name.replace("Phone actual", "") ].reduce[ msg, person | msg = msg + ", " + person ]
    PeopleHome.sendCommand(whoHome)
end

There rule triggers when ever any member of PhonesActual changed state. We filter the members of PhonesActual to get a list of just those that are ON. Then we get a list of just the names of those that are on with the “Phone actual” removed to get a list of the names of the people. Finally we use reduce to create a String listing all the people who are home.

1 Like

This is brilliant, thanks for that. I did have to switch around the value msg and person, not sure if it was supposed to work but it didn’t validate and didn’t make sense in my head.

rule "Currently Home"
when
    Member of Phones changed or Item TestSwitch received update ON
then
    val WhoHome = Phones.members.filter[ p | p.state == ON ].map[ name.replace("PhoneActual", "") ].reduce[ person, msg | msg + ", " + person ]
    PeopleHome.sendCommand(WhoHome)
end