String manipulation in rules

strings
rules
Tags: #<Tag:0x00007fe0531801d8> #<Tag:0x00007fe053180098>

(Michael) #11

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


(Michael) #12

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


(Guido Van Haasteren) #13

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!


(Dominik) #14

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


(Dominik) #15

No ideas?


(Vincent Regaud) #16
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:


(Dominik) #18

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


(hr2) #19
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


(Dominik) #20

Thanks, but what do you mean with:

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

Thanks


(Vincent Regaud) #21

What is the error message now?


(Vincent Regaud) #22

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

(Dominik) #23

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.
``

(Vincent Regaud) #24

Any more on the log about that error?


(Vincent Regaud) #25
postUpdate(rs.name.toString,"DOWN")

(Dominik) #26

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…


(Dominik) #27

Still the same. Sorry…


(Vincent Regaud) #28

You are using a Lambda inside a Lambda…
let’s add another var at the begining

var String windowName = ""
var String rsString = ""

and the rule:

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("_")
    rsString = rs.name.toString
    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(rsString, "DOWN")
           }]
end

(Dominik) #29

Hey Vincent,

I would be happy to say, that your code solved the problem, but still -

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

Any other idea - I am fully lost here…


(Vincent Regaud) #30

Does the rule run?
Are there any lines after that error message in the log?


(Vincent Regaud) #31

What are the items in the group gH_Contact_global?