Build String and output from Loop

  • Platform information:
    • openHAB version: 4.0.0.M3

Hey, can anyone help me please to understand how i can return a output of a assembled string?
For each group member whose status is not “Keine”, it should expand the string and output it after the last group member.

  1. Cannot refer to the non-final variable BuildPollenString inside a lambda expression; line 4, column 120, length 17
  2. Cannot refer to the non-final variable BuildPollenString inside a lambda expression; line 8, column 379, length 17
var String BuildPollenString
Grp_DWD_PollenIndex.members.filter[ i | i.state != 0 ].forEach[ i |
var String PollenIndex
BuildPollenString = "Meldung: \n"
PollenIndex = transform("MAP", "DWDPollen.map", i.state.toString)                                                             
  if (PollenIndex != "Keine") {    
    //logInfo("Pollen", ""+ i.label +" = " + PollenIndex)
    BuildPollenString += ""+ i.label +" = " + PollenIndex +"\n"  
  }                                                             
]
logInfo("Pollen",""+BuildPollenString)

Hello, maybe someone still has suggested solution? :wink:

Well, the non-final var is because it’s a local var.
You have to use a global var for this purpose.

As I’m also using the dwdpollenflug addon: Are you aware of the fact, that each Index Item has to be of type String, therefor the filter has to be [ i | i.state.toString != "0" ]

regarding the correct use of the variable I’m a bit stumped, can you help me there please?

The filter works with and without string, with both I get the same values in the log.

var String BuildPollenString
Grp_DWD_PollenIndex.members.filter[ i | i.state != 0 ].forEach[ i |
var String PollenIndex
//BuildPollenString = "Meldung: \n"
PollenIndex = transform("MAP", "DWDPollen.map", i.state.toString)                                                             
  if (PollenIndex != "Keine") {    
    logInfo("Pollen", ""+ i.label +" = " + PollenIndex)
    //BuildPollenString += ""+ i.label +" = " + PollenIndex +"\n"  
  }                                                             
]
//logInfo("Pollen",""+BuildPollenString)

Well, this is, because you’re filtering twice :slight_smile:
I did not test this, but my guess is, that StringBuilder may work better:

val sbPollen = new StringBuilder 
sbPollen.append("Meldung: \n")
Grp_DWD_PollenIndex.members.filter[ f | f.state != NULL && f.state.toString != "0" ].forEach[ i |
    val strIndex = transform("MAP", "DWDPollen.map", i.state.toString)
    sbPollen.append(i.label + " = ")
    sbPollen.append(strIndex + "\n")
]
logInfo("pollen","{}",sbPollen.toString)

Please be aware that there is no if(PollenIndex != "Keine") which is the reason that there is no Message for 0…

EDIT: Now tested and eliminatid some typos…

thank you, with the StringBuilder it works now :smiley: