Javascript Rules in Openhab 3

Hi there,

can somebody give me a hint how to use javascript rules (file based) in openhab 3.
It’s native supported via ui but how to use file based?


I think is the best docs for file based.

I know this page, but is this working for oh3 as well.
Is this installed in oh3 per default:

A quick scan of that page and except for the PaperUI stuff and the need to install it separately I think all that information is still correct for OH 3.

I am currently testing it, but get a lot of class cast exceptions, because packages moved from oh2 to oh3. Is there an updaded version for oh3.
Or what is the preferred alternative of dsl rules in oh3 for file based rules? I just find ui based examples, but the worst thing with is that i cannot search for usages of something.

Its working now, but the last question is still open, whats the preferred rule language?

There is no preferred language over all. You won’t find too many examples of text based javascript examples because in OH 2.5 Python was the most popular alternative. The preferred language will be your preferred language.

If you will be writing .js rules files, I strongly recommend the helper libraries already linked to above.

Also understand that the OH 3 docs are no where near complete. Most of the common stuff you’d do in a JavaScript or Rules DSL or Blockly rule created through MainUI will be in the docs. And pretty much any JavaScript example you see on the forum now for OH 2.5 will also be applicable to OH 3.

About the biggest difference between text based .js rules and creating them through the UI is that all that rule stuff (creation of the rule, setting of the triggers, etc.) you have to write in the code yourself. In the UI rules you don’t have to mess with the actual code unless/until you get to creating a Script Action or Script Condition. The actual code you put in there though will exactly match the code you’d put into the execute function that you’d write when using .js rules files.

What else should i do in rules when not script-action.
for example for renaming i cannot find any usages of things or items via ui. With file based stuff i can run a full text search.
Additionally i don’t have syntax highlighting or code completion in the ui.

I do not think that ui based rules are working for large setups (currently more then 2000 items) or should it be possible.

You should have both. I know I do.

Renaming what? You cannot rename Items but Things. All you can do is remove and recreate.

And since that’s code that works go in a script action, it’s the same code whether you do it in a.js file or through the UI.

It should be apparent how to do that stuff in the triggers and such. There’s a list to pick from and a form to fill out. And in script actions, it’s the same as you would do in. j’s files.

I think that works in the UI too. But if not, b you can certainly do a full text search in the JSONDB. I do so periodically to check which items have what metadata given the REST API for metadata is so poor.

Frankly, it should work better. JSONDB loads and parses faster than text based configs. They don’t experience the problem where rules start to run before all the items are loaded. Management of items thorough the model works really well. The developer sidebar makes the code and test loop really fast.

Is is somehow possible to define js functions in Scripts and use them in rules?

1 Like

Yes, of course. See OH 3 Examples: Writing and using JavaScript Libraries in MainUI created Rules

Hello to all
I’m new to work with OH 3 all my devicese are working fine and now i want to use my Sonoff basic(with temp. sensor) to switch an heater.
I’ve tried a simple rule
If temp is lower 3.0°C switch on the powerplug but I can’t get it working.

  • id: “1”
    itemName: ESPURNA4561CCSwitch_ESPURNA_4561CC_temperature
    state: “3.0”
    type: core.ItemStateUpdateTrigger
  • inputs: {}
    id: “3”
    itemName: ESPURNA4561CCSwitch_ESPURNA_4561CC_temperature
    state: “3.0”
    operator: <=
    type: core.ItemStateCondition
  • inputs: {}
    id: “2”
    itemName: fernsehlicht_fernsehlicht
    command: ON
    type: core.ItemCommandAction

At the moment I’m using a device “fernsehlicht” just for easier controlling
Can somebody give me a hint how to setup the rule .

For specific problems like this it’s better to open a new thread. This is off topic for this thread as there is no JavaScript in this rule.

I don’t see anything obviously wrong with the rule but it’s hard to tell. Please use code fences to perserve indentation and white space which is important.

code goes here