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);
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
You then just have to drop in an If / Do block in Logic and you should be thereā¦
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.
I am not totally sure what you meant to achieve 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 !