[SOLVED] JSR223 Javascript

I’m trying to move my rules from Rule DSL to JSR223/Javascript… I’ve got most things working but I"m struggling with how to implement the equivalent of the following…

fgEmptyBatteries.members.forEach[ item | fgEmptyBatteries.removeMember(item)]|
fgBatteries.allMembers.filter( s | s.state < 15).forEach[ item | fgEmptyBatteries.addMember(item)]|

Any ideas appreciated.

For iterating over group members…

ir.getItem("gTest").members.forEach(function (item) {
    // do stuff
});

Or in Jython

for item in ir.getItem("gTest").members:
    #do stuff

The JS helper libraries and docs will be getting setup like Jython very soon! For your specific needs (untested)…

//fgEmptyBatteries.members.forEach[ item | fgEmptyBatteries.removeMember(item)]|
var group1 = ir.getItem("fgEmptyBatteries")
group1.members.forEach(function (item) {
    group1.removeMember(item);
});

//fgBatteries.allMembers.filter( s | s.state < 15).forEach[ item | fgEmptyBatteries.addMember(item)]|
val group2 = ir.getItem("fgBatteries")
group2.allMembers.forEach(function (item) {
    if (item.state < 15) {
        group1.addMember(item);
    }
});

Someone might be able to provide a better way to do the filter.

In Jython…

#fgEmptyBatteries.members.forEach[ item | fgEmptyBatteries.removeMember(item)]|
group1 = ir.getItem("fgEmptyBatteries")
for item in group1.members:
    group1.removeMember(item)

#fgBatteries.allMembers.filter( s | s.state < 15).forEach[ item | fgEmptyBatteries.addMember(item)]|
group2 = ir.getItem("fgBatteries")
for item in filter(lambda item: item.state < 15, group2.allMembers):
    group1.addMember(item)

Many thanks… This worked perfectly… I even managed to turn the code into a function to be called from multiple rules…

Now, for the next problem… Is there an equivalent to

	when
		Member of xxx-group changed from ON to OFF
	then

None of the triggers I can see in lewies library seem to provide an equivalent function to this???

Currently, there’s nothing like this in JS… only for Jython. Once OH is compatible with JDK9, which has ES6, I plan to build the same functionality into the JS helper libraries. This could probably be done now, but will be easier with class support. But I’ll also be working on a new scripting API that will replace the need for using the helper libraries too. Not sure which will come first. The raw API is ugly!

If you’re not set on JS, try Jython! :wink:

I’m still working on a is enhancement. Maybe this is interesting for you.

Hey guys,

i also trying to move my rules from Rule DSL to JSR223/Javascript.
I want to move the window reminder Rule from here :

But i fail in de group members action.i
In Rule DSL
val timer = FHTfensterTimer.members.filter[t | t.name == fenster.name+"_Timer"].head as SwitchItem
To move it to javascript i tried the way from @rlkoshak here
https://community.openhab.org/t/experimental-next-gen-rules-engine-documentation-4-of-writing-scripts
/55963
var filtered = group.members.stream().filter(function(i){ return <boolean expression>; }).toArray(); where <boolean expression> is the filtering conditional.

Here my rule

load(Java.type("java.lang.System").getenv("OPENHAB_CONF")+'/automation/jsr223/jslib/JSRule.js');

JSRule({

    name: "Fensterüberwachung",

    description: "Diese Regel überwacht die Fenster und gibt Alarme in bestimtmen Zeitfenstern aus",

    triggers: [

        TimerTrigger("0/10 * * * * ?")//Enable/Disable Rule

    ],

    execute: function( module, input){

        

        var logname = "FENSTERÜBERWACHUNG";

        //Fenster Item aus TriggerEvent holen

        var fenster = getItem("TestFenster1"); //dieses Item als Test -> Muss durch TriggerItem ausgetauscht werden

        logInfo(fenster.getName() +" hat die Regel ausgelöst")

        //TimerItem zu Fensteritem

        var group = getItem("GroupFensterTimer");

        var filtered = group.members.stream().filter(function(i){ return i.getName() == fenster.getName() +"_Timer" ; }).toArray();

        logInfo(filtered +" ist das Timer-Item");
}

});

Items

//Testitem für Fensterüberwachung
Group:Switch                    GroupFensterTimer
Switch TestFenster1                 "Testfenster 1"
Switch TestFenster2                 "Testfenster 1"
Switch TestFenster1_Timer           "Testfenster 1 Timer"       (GroupFensterTimer)
Switch TestFenster2_Timer           "Testfenster 1 Timer"       (GroupFensterTimer)

Here the log

Now i don’t unterstand the second log item. How can i get the right timer item from the group to the var = filtered?
Can everyone help me?

It looks like you are just trying to set a variable to an Item. This is MUCH easier to do in scripted automation than in the rules DSL…

val timer = ir.getItem(fenster.name+"_Timer");

https://openhab-scripters.github.io/openhab-helper-libraries/Guides/But%20How%20Do%20I.html#get-an-item

1 Like

Thank you so much!
A very simple solution. I was thinking too complicated shame
Nevertheless, it would be interesting to solve the other way.

best
Nico

Is there now a equivalent according this trigger available?
Best
Nico

No, not yet.