I have a DSL rule (from oh2.5) with the following expression:
gKNX_zyk.members.forEach[ i | i.sendCommand(i.state.toString) ]
to resend the state of every item in the group gKNX_zyk every 10 minutes, but actually I fail to build it as a ECMA rule. I have tried some syntax like:
There are a fixed number of ways to interact with Lists of things in all the programming languages:
filter: reduce the list down to a smaller list based on some criteria (e.g. a list of all the Items that are ON)
forEach: loop through the list and do something to each item in the list (e.g. send a command to each Item)
map: convert the list into another list after transforming each item in the list (e.g. get a list of just the Item’s states)
reduce: take a list and built a single result (e.g. create a sum of all the Item’s states)
findFirst: look through the list and return the first item that matches some criteria
No matter the language, these operations exist and they go by the same names.
I can’t even tell what you are thinking with your JavaScript version. It really doesn’t make any sense what-so-ever no matter what way you look at it beyond just trying some random stuff found on the forum and hoping something works without any understanding of how either the Rules DSL or the JavaScript lines work.
Because the operations are the same and use the same names, all it takes is a little bit of translation between the languages to make it work.
Rules DSL
JavaScript
Notes
gKNX_zyk.members
itemRegistry.getItem("gKNX_zyk").members
Get a list of all the members of the Group Item
not applicable
stream()
Convert the List of members to a stream, in Rules DSL this happens automatically for you
Loop through each member of the list and execute a lambda function on each member.
JavaScript has some quirks because the stream stuff isn’t done for you which mainly comes into play when your last operation on the list is a filter, map, reduce, etc. But that’s not going on here, you are just looping through the members. So you will see the JavaScript version looks very similar to the Rules DSL version.
For most users out there, please don’t move your rules to JavaScript (or any other language) unless you really have a need to. Rules DSL still works and will likely continue to work in OH for ever. Unless there is a compelling reason to rewrite your rules in a different language, don’t do it. Especially if your level of programming is at the cargo cult programming level (i.e. copying stuff from the internet without understanding how it works and just hoping that it does). You will get no benefit from moving to another language. It’s just going to cause you extra work and make it so you understand your own rules even less.
That is why I converted to JavaScript as well. I read the DSL had performance issues.
I have converted all my old 2.5 DSL and OH3 runs well. Any new rules I will still use Javascript.
I didn’t really understand DSL as well.