"Var"-Problems when creating custom scripts

Tags: #<Tag:0x00007f174f2b0c10>

I’m trying to recreate some of my old rules in OH3, but i’m running into some confusing problems

Sinply said, i can’t get my own script for a rule working with variables.


A variable is nessecary here, because, later, I want to get the current state of every “item” and modify it.

I am aware, that i can integrate old rules, by putting them in the “rules” directory on my Rasppi, but when I do so, the “when” gets commented out.
2021-02-16 11_36_31-openHAB

As of now I have looked up other topics and deducted, that “var” is working for them.
I have to admitt, i’m still new to OH3 (even though I have a bit of experience with OH2) so you can think of me as a total beginner here.

2021-02-16 11_58_49-openHAB
Well, NOW the imported rule works.
But to be honest, I dont have the slightest idea, why/how…

This is the second case where someone has claimed that code like

TaskNum = blah blah blah

worked in OH 2.5 but doesn’t in OH 3 and adding the var to it is what fixed it. All I can say is that it never should have worked in OH 2.5 either. You have to declare all your variables using var or val before their first use.

As for your first example, again, it has always been the case, even in OH 2.5, that you cannot reference a var inside a forEach. But you can reference a val.

Finally, please don’t post screen shots of code. Paste the actual code and use code fences.

```
code goes here
```

@Vetri what language are you using for the rule you are trying to copy into OH3, ECMA Script or Rules DSL?

The syntax is quite similar and maybe 85% of the DSL code will work in ECMA, but ECMA requires var x where I’m not sure that DSL does.

It does too, though I’ve seen some people who were apparently not using var in 2.5 with rules that supposedly worked and are now breaking. I’m skeptical that these rules actually did work (I can’t count how many times I seen “this worked in 2.5” but there is no way the code could have ever worked).

The code shown above has to be Rules DSL. You can’t just start using an Item like that without pulling it out of the item registry first (unless there is something I don’t know).

Putztruppe.members.forEach[item |

would work in Rules DSL just fine. In JavaScript though it’d have to be something like (if using Java stream API)

ir.getItem("Putztruppe")
    .members
    .stream()
    .forEach(function(item) { blah blah blah; })

True enough, but I get the impression he’s getting kickback from the editor on syntax errors. The fact that Putztruppe has no value will only be seen at runtime, so the JS syntax checker wouldn’t complain.