Porting rules from textual .rules to UI Rules

Hi all, I was starting with OH 2.0 and I have a lot of rules in my .rules textual file.
One example for all

rule "OU_Outside_Recess"
    when
        Item OU_Outside_Recess received command
    then
        strVal = "none"
        switch(receivedCommand)
        {
            case ON: strVal = "1"
            case OFF:strVal = "0"
            default: logError("OU_Outside_Recess","*** INVALID CMD")              
        }

        if(strVal != "none")
        {
            executeCommandLine(scriptFullPath, RAMP_RECESS_SPOTLIGHT, strVal)
        }
end

where:

  • OU_Outside_Recess is a Text item
  • scriptFullPath is defined on top of .rules file
val String scriptFullPath                   = "/etc/openhab/scripts/myOwnBashScript.sh"
  • RAMP_RECESS_SPOTLIGHT is defined on top of .rules file
var String RAMP_RECESS_SPOTLIGHT 			= "20"
  • myOwnBashScript.sh is

#!/bin/bash
set -u
itemId=$1
itemVal=$2
…foo…

exit 0

I want to have the rule and the script in UI, removing the .sh and the .rules.
How can I do that?

I started a new rule


adding a “script” action
image

And now?

Now you decide what scripting language you would like to use.
Your old rule was written in ‘DSL’.
You’d want to review breaking changes for old DSL rules in OH3, if you decide to go that way.

I don’t know about the .sh script, but the .rule file you can migrate easiest to the UI with the help of the api.

General strategy is:

  • get the api output of the GET rest/rules endpoint. That is a complete list of definitions of all your actual rules
  • save that output to a file
    -delete the .rule files
  • send the definition of each rule to the PUT rest/rules endpoint, repeat for each rule
  • congratulations, your rules are migrated to the UI, you can now manage them from there. Check out they still work in the new version and make necessary changes

If you have a lot of rules, you can automate the one-by-one inserting using a program like postman

Thanks for answer. Can you please provide for more details about your quoted statement?

What is unclear to you?

You can find the api documentation here:

Well, some of you answered my “old rule”, that is

rule "OU_Outside_Recess"
    when
        Item OU_Outside_Recess received command
    then
        strVal = "none"
        switch(receivedCommand)
        {
            case ON: strVal = "1"
            case OFF:strVal = "0"
            default: logError("OU_Outside_Recess","*** INVALID CMD")              
        }

        if(strVal != "none")
        {
            executeCommandLine(scriptFullPath, RAMP_RECESS_SPOTLIGHT, strVal)
        }
end

was written in DSL:

Your old rule was written in ‘DSL’

Now, in UI I see I can add rules by “Rule DSL (v1)”
image
so at first glance, it seems I can almost copy-and-paste from textual to UI. :neutral_face:

Now, you are suggesting to use the REST API instead (I already use them since years to get/set items values from external application to OH).

But I need to achieve the opposite, that is OH sends command to the external application!!!
E.g.: I move a switch in OH app → I want to notify the external app

Up to today, I am achieving it by .rule textual file where, upon an item status change, I conveniently format a string which is in turn sent to the external app with a bash script using netcat.
Th external app parsed the received string and does it joy…and it works perfectly!

I read again the REST API doc but nothing releated to my needings

I “simply” want to move from textual .rules to UI rules, as I need to view/edit the rules remotely, and UI can be accessed from any device everywhere in the world. Textual .rule file not.

To summarize, I will ask you just a question:
what I have to write in UI Rule DSL to emulate/replicate the executeCommandLine function natively available in textual .rule

And for that I have provided you with a workflow to do it by API. I don’t think I have said something about using the API to trigger external events. What I was describing was to use the API to move from textual .rules to UI rules. What I described was a way to create rules by copy pasting the existing definition of the rule using the API.

Openhab loads the .rules files into it’s database but makes them read only because you manage the rules with the files. Well, you just need to tell openhab to use the same rule definition but without the files, then you can manage them from the UI.

So you have the .rules files now, the correct definition of the rule from the .rules file in the openhab database. Well then you just need to extract the rule definition from the openhab database (using the GET /rest/rules API endpoint), delete the .rules file and thereby deleting the existing, read only definition of the rule in the openhab database and then load the good working rule again in the openhab database (using the PUT /rest/rules API endpoint).

Alternatively, you could also copy paste the scripts from your existing .rules files into the UI but then you need quite a bit of manual work because you also need to supply the trigger in another way.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.