Blockly count

Hi all.
As i created all my rules in blockly, and i searched all blocks without success, i wonder if there is a way to do a count like the script below in blockly?

var group = itemRegistry.getItem("Battery");
var battson = group.members.stream().filter(function(i){ return i.state == "ON" }).count();

var label;

 if (battson == 0) {
   label = "OK";
 } else {
   label = battson + " " + "leer" ;
 }

events.postUpdate('Battery_Count', label);

Greetings Roli

Under ā€œListsā€ thereā€™s a ā€œlength ofā€ block.

Hi,

Yes, the group members part is in Items & Things and is ā€˜get members of groupā€™ which you can then run the rule from

The other part is in ā€˜Run & Processā€™ called contextual info and you can use this to grab lots of useful things, including the received command, or state of item etc

image

You then just have to drop in an If / Do block in Logic and you should be thereā€¦

Thanks for your reply`s
I tryed the ā€œlength ofā€ block without success.

This is my aproach;
Get the members of the group,
Filter the sate
and get the length of the list

Blockly15

but the result is always ā€œundefinedā€

2022-09-23 19:50:37.580 [INFO ] [org.openhab.rule.ABTest             ] - undefined

if i just log the variable ā€œiā€

i get the list with items state OPEN

2022-09-23 19:51:49.646 [INFO ] [org.openhab.rule.ABTest             ] - Z4F_State (Type=ContactItem, State=OPEN, Label=Z04, Category=Contact, Tags=[OpenState], Groups=[Z4F, Windows])

Is this the correct approach or do i miss something?

In the code above, i is an Item, not a list of Items. If you want the number of members of the group

length of | get members of group | Windows

In your original code you want a count of the members that are on. Iā€™m not sure how to do a filter on a List in Blockly but once you do that youā€™ll get back a smaller list. Then you plug that smaller list into the ā€œlength ofā€ block.

That is really tricky.
I guess the problem is to get a list with the states of group members, that`s what actually a filter would do.

As per the motto nothing is impossible i found a solution, it is working but it is fairly complicated.

Blockly16

If i come back to this block assembly after 2 month, iā€™m sure i donā€™t remember how it works

I think Iā€™ll create my own block.

I am not totally sure what you meant to achieve :thinking: but if the intent is to count how many of the groupā€™s members are OPEN then you could use this approach (I am here counting the number of lights that are on as it was either to test it out)

Hi Stefan
Thanks for your reply.
Yes, thats exactly what i was looking for and the longer i look at your block assembly, the more obvious it becomes.
Lesson learned !

Thanks a lot

1 Like