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
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:
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:
and then it will match your .rules file rule.
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
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.