Request State of all members of a group with a javascript loop

Hey,
i would like to know if it’s possible to create a Blockly skript with a loop that gets the same value, for example a temperature, from a group of thermostats and react when one of them is below or above a defined value?
Another usecase would be to observe all batteriestates by creating a group batteries and request them all in one simple loop.

something like

for(all items of group "Batteries"){
    if(itemRegistry.getItem('Batteries').getState() <= "10"){
        telegramAction.sendTelegram("Batterie xy is getting low");
    }
}

i played a little bit with lists and variables but i wasnt able to figure out how this could work.

This cannot be done yet. For now you’ll have to do this with text code.

In JavaScript it would be something like:

ir.getItem("Batteries").members.
                       .stream()
                       .filter(function(batt) { return batt <= 10; } )
                       .forEach(function(batt) { telegramAction... } );

Note that you can’t call binding Actions using Blockly yet either. You’ll have to access the telegram action in the usual way.

actions.get("telegram", "uid of the telegram Thing").sendTelegram("message");

Thanks Rich,

you are definitly the awesome answer dude!
But sadly i don’t get it to work…

my first trie is to log the states.

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);

ir.getItem("Batteries").members.
                       .stream()
                       .filter(function(batt) { return batt <= 100; } )
                       .forEach(function(batt) { logger.info(batt); } );

i get the following error

Script execution of rule with UID '7128728f30' failed: <eval>:4:23 Expected ident but found .

i tried to read something about javascript but i don’t get it how this is suposed to work. if someone has a simular sollution maybe he could post it and explain how it works.

There is an extra . in the code after members, also the variable batt will contain an Item, not it’s state.

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);

ir.getItem("Batteries").members
                       .stream()
                       .filter(function(batt) { return batt.state.intValue() <= 100; } )
                       .forEach(function(batt) { logger.info(batt.state); } );
1 Like

for thos who care, my full solution.

var whatitis = "";
ir.getItem("Batteries").members
                   .stream()
                   .filter(function(batt) {return batt.state.intValue() <= 10; } )
                   .forEach(function(batt) { whatitis = whatitis + batt.label +": "+ batt.state +"%" + "\r\n"; } );
if(whatitis != ""){
    events.sendCommand('TelegramText', "Batteriewarnung für: \r\n" + whatitis);
}

I have set this script to be triggered every saturday at 12:00.

TelegramText is a string item wich will send this string to telegram when this item changes.
this way i can use blockly for shorter rules including telegram.

1 Like