String manipulation in rules

Try replacing the parens around the lambdas with [ ]. That means the parens after forEach and after filter.

Hello Rich,

Thanks a lot for your quick reply. Yes, that was it. And for all others who wondering about still existing errors:
namedParts[0]” etc. does not work. You have to use “nameParts.get(0)” instead.
I read this in another post from you Rich - thanks a lot for your valuable posts in this forum that helped me a lot already!

Unfortunately, it seems to me a lot of guesswork and trial and error to figure out the syntax and possibilities of DSL. I would love to have a good and up-to-date documentation for this (not only me…).
For example: How do I even know that the .split() command exists without this forum?

Best regards
Michael

1 Like

The easiest is to use VSCode with the openHAB Extension. When you type

val nameParts = rs.

VSCode will give you a list of all the possible valid ways to complete the statement. Use the up and down arrows to scroll throgh the list or narrow the list down by start typing the name of a method.

image

And you have https://docs.openhab.org/configuration/rules-dsl.html and Xtend - Expressions and Java Platform SE 8.

It does take a bit of work understanding how the language works and knowing what you are dealing with. The Rules Domain Specific Langauge (DSL) is its own language (see the first link) that is based on the Xtext language and closely resembles the Xtend language (see the second link) which makes available all the core classes from the Java language (see the third link).

Rich,you suggested me to use VS-Code too, which I do, and it is not showing me this list of possible statements! I installed the openhab-extension in VSC by clicking Ctr+Shift+X, typing ‘openhab’ and click install and reload.
VS-Code gives me suggestions for already used vars or Items, but it is giving me a list only of vars or Items that I have already used in the same file, and even the once that are miss-spelled. From what I read about VS-Code installation for openhab, all configuration is optional and the deaults are with all the trick built in.

Should the suggestion-lists work with the current version of the openhab-extension? (I’m using 0.3.5) and the current version of VSC? (Version 1.20.0, Shell 1.7.9, Renderer 58.0.3029.110, Node 7.9.0, Architecture x64)

And should I do more configuration?

My OH-configfiles are on RaspberryPi with samba-share. My VSC runs on windows laptop.

Thanks! It helps a lot - as long as VScode can communicate with the openHAB language server / REST API.

Other people also struggling with getting a connection:
https://community.openhab.org/t/visual-studio-code-howto-connect-to-rest-api/36900/23

Ok, i got it working now. I had ‘http’ included in the hostname. Now I see lots of errors and warnings, so back to work!

I am looking, exactly for the same - window open → rollershutter stays open.

I used the code above, but it does not really went well. If I trigger this rule, I always get the message in log:

[ERROR] [untime.internal.engine.RuleEngineImpl] - Rule ‘Rolladen Wohnzimmer schliessen’: 3

I have more or less the same setup, except for items:

Group:Rollershutter:OR(UP,DOWN) gH_Rolladen_wz_switch “Rolladen Wohnzimmer”

which includes all items which are rollershutter.

The code is modified as follow:

rule “Rolladen Wohnzimmer schliessen”
when
Item EG_wz_switch_3_long received update OFF
then
gH_Rolladen_wz_switch.members.forEach[rs|
val nameParts = rs.name.split(“")
val windowName = nameParts.get(0) + "
” + nameParts.get(1) + “fk” + nameParts.get(3)
logInfo (“Fensterkontakt der offen is:”, +windowName)
if(gH_Contact_global.members.filter [fk | fk.name == windowName].head.state != OPEN){
postUpdate(rs,DOWN)
}]
end

I think, it is somehow in relation, that items comes not as a string value therefore the full rules does not fly (i.e. logInfo also do not appear in the log).

Any suggestions, what I could do to improve as I am not a real programmer? I understand, what the code intend to do basically (more or less), but I get really fast to my limit.

Any help is appreciated.

Thanks in advance.
Dom

No ideas?

val nameParts = rs.name.split("")

What is the character splitting the string “_” or “-” or what? There nothing in your ""

Also can you use the code fences, please? See:

Hello Vincent,

Thanks for your reply - sorry for my late reply, but I don’t get a notice about your answer.

I use “_” as a string seperator - I have no clue, why it is not shown in the code I posted.

Again, here is my code in code fences:

rule "Rolladen Wohnzimmer schliessen"
when 
    Item EG_wz_switch_3_long received update OFF
then
  gH_Rolladen_wz_switch.members.forEach[rs|
    var nameParts = rs.name.split("_")
    var windowName = nameParts.get(0) + "_" + nameParts.get(1) + "_fk_" + nameParts.get(3)
    logInfo ("Fensterkontakt der offen is:", +windowName)
        if(gH_Contact_global.members.filter [fk | fk.name == windowName].head.state != OPEN){
           postUpdate(rs,DOWN)
           }]
end

Thanks
Dominik

logInfo ("Fensterkontakt der offen ist:", windowName)

remove the “+”

if(gH_Contact_global.members.filter [fk | fk.name == windowName].head.state != OPEN){

Cannot refer to the non-final variable windowName inside a lambda expression

Thanks, but what do you mean with:

What to change? Sorry, my programming skills are really basic.

Thanks

What is the error message now?

Try declaring windowName as a variable at the top of the file (Before all the rules)

var String windowName = ""

rule "Rolladen Wohnzimmer schliessen"
when 
    Item EG_wz_switch_3_long received update OFF
then
  gH_Rolladen_wz_switch.members.forEach[rs|
    var nameParts = rs.name.split("_")
    windowName = nameParts.get(0) + "_" + nameParts.get(1) + "_fk_" + nameParts.get(3)
    logInfo ("Fensterkontakt der offen is:", windowName)
        if(gH_Contact_global.members.filter [fk | fk.name == windowName].head.state != OPEN){
           postUpdate(rs,DOWN)
           }]
end

Thanks Vincent for your answer.

Still getting the same error:

20:53:41.324 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'Rolladen Wohnzimmer schliessen': 3

At the moment I save the rule, follwoing appears in the log:

20:52:51.958 [INFO ] [del.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'Rolladen.rules', using it anyway:
This expression is not allowed in this context, since it doesn't cause any side effects.
``

Any more on the log about that error?

postUpdate(rs.name.toString,"DOWN")

This one, when saving the rule:

20:52:51.958 [INFO ] [del.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'Rolladen.rules', using it anyway:
This expression is not allowed in this context, since it doesn't cause any side effects.

No idea, how to get more details…

Still the same. Sorry…