Counter in If-statement (Javascript) not working consistently

OK, that confirms the error is the getStateAs. This might be one of the many little changes that has been made between OH 2 and OH 3. Looking at the JavaDoc it looks like the function has changed named to as.

Try replacing getStateAs with as in both places.

Like this?

logger.info("Collectors imported, about to filter the Group members");
var numOnList = ir.getItem("Leuchten_Alle")
              .members
              .stream()
              .filter(function(i){ 
                  logger.info("Processing Item " + i.name + " whose current state is " + i.state);
                  var currState = i.as(OnOffType);
                  logger.info(i.name +"'s state is " + currState);
                  return currState == ON; 
              })
              .collect(Collectors.toList());
logger.info("The filter returned the following Items: " + numOnList.toString());
logger.info("There are " + numOnList.size() + " lights that are ON");

I only found one expression to exchange, not two: var currState = i.as(OnOffType);

and this is generated:

2021-04-16 17:02:29.449 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - Collectors imported, about to filter the Group members
2021-04-16 17:02:29.461 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - Processing Item EsszimmerTisch_Farbe whose current state is 29,83,0
2021-04-16 17:02:29.464 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Leuchten_An_Anzahl' failed: TypeError: i.as is not a function in <eval> at line number 10

Darn, I was looking at the wrong class.

OK, first try replacing the as with state.as(OnOffType).

If that doesn’t work try replacing that with i.getStateAs(OnOffType.class)

When I get to c a computer I’ll do some experiments too.

1 Like

Gratulations (and thank you again!), looks like you/we finally made it :slight_smile: !
For documentation’s sake:

working code:

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var Collectors = Java.type("java.util.stream.Collectors");

logger.info("Collectors imported, about to filter the Group members");
var numOnList = ir.getItem("Leuchten_Alle")
              .members
              .stream()
              .filter(function(i){ 
                  logger.info("Processing Item " + i.name + " whose current state is " + i.state);
                  var currState = i.getStateAs(OnOffType.class);
                  logger.info(i.name +"'s state is " + currState);
                  return currState == ON; 
              })
              .collect(Collectors.toList());
logger.info("The filter returned the following Items: " + numOnList.toString());
logger.info("There are " + numOnList.size() + " lights that are ON");
events.sendCommand("AnzahlLampenAn", numOnList.size());

Example of a result:

2021-04-16 17:36:34.472 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - Collectors imported, about to filter the Group members
2021-04-16 17:36:34.473 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - Processing Item EsszimmerTisch_Farbe whose current state is 29,83,0
2021-04-16 17:36:34.474 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - EsszimmerTisch_Farbe's state is OFF
2021-04-16 17:36:34.475 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - Processing Item Frosch_Farbe whose current state is 46,56,0
2021-04-16 17:36:34.476 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - Frosch_Farbe's state is OFF
2021-04-16 17:36:34.477 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - Processing Item LeuchteHueklein_Farbe whose current state is 46,56,0
2021-04-16 17:36:34.477 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - LeuchteHueklein_Farbe's state is OFF
2021-04-16 17:36:34.478 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - Processing Item LeuchteHuegross_Farbe whose current state is 36,30,21
2021-04-16 17:36:34.479 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - LeuchteHuegross_Farbe's state is ON
2021-04-16 17:36:34.480 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - Processing Item LeuchteMobil_Farbe whose current state is 0,0,20
2021-04-16 17:36:34.481 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - LeuchteMobil_Farbe's state is ON
2021-04-16 17:36:34.492 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - Processing Item EsszimmerWein_Farbe whose current state is 29,83,0
2021-04-16 17:36:34.493 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - EsszimmerWein_Farbe's state is OFF
2021-04-16 17:36:34.494 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - Processing Item EsszimmerKlavier_Farbe whose current state is 29,83,0
2021-04-16 17:36:34.495 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - EsszimmerKlavier_Farbe's state is OFF
2021-04-16 17:36:34.496 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - Processing Item LeuchteWohnzimmer_Farbe whose current state is 29,83,0
2021-04-16 17:36:34.497 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - LeuchteWohnzimmer_Farbe's state is OFF
2021-04-16 17:36:34.498 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - The filter returned the following Items: [LeuchteHuegross_Farbe (Type=ColorItem, State=36,30,21, Label=Farbe, Category=ColorLight, Tags=[Lighting, Point], Groups=[LeuchteHuegross, Leuchten_Alle]), LeuchteMobil_Farbe (Type=ColorItem, State=0,0,20, Label=Farbe, Category=ColorLight, Tags=[Lighting, Point], Groups=[LeuchteMobil, Leuchten_Alle])]
2021-04-16 17:36:34.499 [INFO ] [org.openhab.rule.Leuchten_An_Anzahl ] - There are 2 lights that are ON
==> /var/log/openhab/events.log <==
2021-04-16 17:36:34.462 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'LeuchteMobil_Farbe' received command 0,0,20
2021-04-16 17:36:34.466 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'LeuchteMobil_Farbe' predicted to become 0,0,20
2021-04-16 17:36:34.469 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'LeuchteMobil_Farbe' changed from 0,0,0 to 0,0,20
2021-04-16 17:36:34.501 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'AnzahlLampenAn' received command 2
2021-04-16 17:36:34.504 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AnzahlLampenAn' changed from 1 to 2

Thanks for sticking it out. I’ll be sure to make a note of this as well.