Recreate OH 2.X rule in openhab 3, using 100% Main-UI

In openhab 2.x I had a rule to dim the lights when I press PLAY or PAUSE in kodi.
something like this:

rule "Adjust living room lighting when Kodi Play/Pause"
when
    Item AchterkamerKodiMediacenter_MediaControl changed
then
    var String stateKodi = AchterkamerKodiMediacenter_MediaControl.state.toString()    
    switch (stateKodi.lowerCase) {
        case "play" : {
                    if (achterkamerlampdimmer_DimmerSwitch2.getStateAs(OnOffType) == ON) {achterkamerlampdimmer_DimmerSwitch2.sendCommand(0)}
                      }
       case "pause" : {                           
                        achterkamerlampdimmer_DimmerSwitch2.sendCommand(10) 
                      }
        }

I try to recreate this rule in openhab 3, but using 100% Main-UI

triggers:
  - id: "1"
    configuration:
      itemName: AchterkamerKodiMediacenter_MediaControl
      command: PLAY
    type: core.ItemCommandTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      itemName: achterkamerlampdimmer_DimmerSwitch2
      command: OFF
    type: core.ItemCommandAction

The rule via Main-UI does not work. I don’t know why yet. I tried to recreate the rule in a rule file, see the code at the top of this post but it gives an error

2021-02-23 22:35:15.503 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'kodi.rules' has errors, therefore ignoring it: [13,9]: mismatched input '<EOF>' expecting 'end'

oh yes I forgot to tell you that the Kodi binding works.
I can operate Kodi via the Items control and see PLAY and PAUSE appear in capital letters in the GUI.

The error is just pointing out that, based on the text you posted above, you don’t have the required end following the rule. It should be:

rule XXX
when
  Some trigger
then
  Something happens
end

As for the UI based rule, what you have shown here is not equivalent to what you have in your text rule. The triggers are different. You text rule trigger is:

Item AchterkamerKodiMediacenter_MediaControl changed

Which will cause the rule to run every time the item state is changed whether that change occurs through some external control that OH knows nothing about or some internal command that OH sends itself. The UI trigger

type: core.ItemCommandTrigger

Does not respond to arbitrary changes of the item’s state, but instead only responds when when some part of OH (a ui widget or another rule) sends a command via the binding to the device. This can occur whether the item changes state or not it is simply a recognition that OH has asked the item to do something. So if you have this rule set to ‘received command’ and you start playing Kodi through some other remote or button push on the kodi device, this rule will not trigger. When setting up the trigger with the UI you want to select the changed radio button:
image
and then it will match your .rules file rule.

1 Like

Just to chime in some additional info.

  • As JustinG points out, the error isn’t coming from your UI created Rule.

  • You can’t recreate that one rules as just one rule in the UI without using a Script Action. There is not if or switch logic to test the states of the Items unless you use a Script Action.

  • You can create this as multiple rules though. Trigger one rule when the Kodi Item changes to PLAY (you’ll have to figure out the case because you can’t do toLower on it). Then create a “But only if…” to test to see if the dimmer is ON. Finally send command 0 to the dimmer Item. Then create another rule to trigger on PAUSE and send command to the Dimmer Item.

  • If you’ve already a working Rules DSL rule, why rewrite it? If it’s just to have the rule be in the UI, why not just use a Script Action, choose Rules DSL as the language, and paste everything between then and end into the Script Action. As long as it doesn’t require use of global variables the code will work just fine.

I changed the rule from “core.ItemCommandTrigger” to “changed” and it works perfectly.

question about the * .rules file.

do I literally have to end the rule with “end” instead of “}” ?

rule "Adjust living room lighting when Kodi Play/Pause"
when
    Item AchterkamerKodiMediacenter_MediaControl changed
then
    var String stateKodi = AchterkamerKodiMediacenter_MediaControl.state.toString()    
    switch (stateKodi.lowerCase) {
        case "play" : {
                    if (achterkamerlampdimmer_DimmerSwitch2.getStateAs(OnOffType) == ON) {achterkamerlampdimmer_DimmerSwitch2.sendCommand(0)}
                      }
       case "pause" : {                           
                        achterkamerlampdimmer_DimmerSwitch2.sendCommand(10) 
                      }
        }
END

Thanks for any explanation

Hi Rich.

Thank you for explaining and adding to what Justin wrote.

If I choose Scripts> Rule DSL in openhab, can I work in the same way as I did in openhab 2.5.x in * .rules files?

Can I then use Switch / CASE statment and define Variable and Constands and convert to lower case?

Yes with the following changes (beyond the breaking changes introduced by OH 3 like ZonedDateTime):

  • You only include the stuff between then and end. The rule triggers are defined outside of the script.
  • You can’t define “global” variables. If you need to preserve a value from one run of the rule to the next or need to share a variable between rules, if you can’t use an Item, you’ll need to keep those in .rules files or rewrite them using a different approach.

The } is not the end of the rule it is just closing the switch statement. But yes, you do always need the end; it’s a part of the complete rules syntax. I don’t know the full technical details, but presumably it’s simply about defining for the rules parser which section of the file to run through the xtend engine.

To add just a couple more things.

  • It’s end not END
  • You are missing the closing } for the switch statement.

Beyond that JustinG is exactly right. The end is required as part of the syntax in .rules files.

Thanks,
I have been scripting in power shell the last jear and I have difficulty keeping syntax apart if I switch between languages. :unamused: