Xiaomi mihome rules not working

Hi all,

I had a working configuration with OpenHAB 2.0 and the mihome binding (IOT marketplace version) for lots of Xiaomi sensors and devices.
The rules were working fine, e.g. for two Xiaomi Magic Cubes and a Smart Switch (round).

After upgrading to OpenHAB 2.1 with the latest mihome binding included the rules do not run anymore and throw errors:

  1. openhab.log:
    2017-07-05 22:29:52.839 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘cube_rose.rules’ is either empty or cannot be parsed correctly!

  2. Designer:
    a) no viable alternative at input 'Channel’
    b) the method or field receivedEvent is undefined

I re-created the .rules file just for one single cube or switch from the Xiaomi Mi Home binding example (Copy&Paste) and adjusted the device ID. The syntax has not changed. The file format is okay, but whatever I do, the .rules file is not accepted.

Are there any (new, unknown, undocumented) prerequisites with the new Xiaomi mihome binding?

Thanks in advance!

The Designer errors can be ignored. Designer 0.8 was written before Channel triggers were added to OH.

The first error indicates a serious parsing problem with the rule. Can you post the rule as you have it now?

Hi,

Thanks a lot, but openhab.log shows still parsing errors and the file is exactly the same as before. The test is even smaller rule from the binding examples. (ACTION replaced by sendcommand and the cube ID added)

rule "Xiaomi Cube"
when
Channel ‘mihome:sensor_cube:123456789:action’ triggered
then
var actionName = receivedEvent.getEvent()
switch(actionName) {
case “MOVE”: {

}
case “ROTATE_RIGHT”: {

}
case “ROTATE_LEFT”: {

}
case “FLIP90”: {

}
case “FLIP180”: {

}
case “TAP_TWICE”: {

}
case “SHAKE_AIR”: {

}
case “FREE_FALL”: {

}
case “ALERT”: {

}
}
end

UTF-8 format is Set.

Thx

Please edit your post and use code fences. The fifth button from the right will add the fences and put your code between them It is all but impossible to read code that is not properly formatted.

Hi rlkosak,

you’re right, of course. I only had my smartphone at hand to reply “quick & dirty”, sorry.
So, here the properly formatted code:

rule "Xiaomi Cube Rose Test"
when
	Channel 'mihome:sensor_cube:158d0001040d7f:action' triggered
then
    var actionName = receivedEvent.getEvent()
    switch(actionName) {
        case "MOVE": {
        }
        case "ROTATE_RIGHT": {
        }
        case "ROTATE_LEFT": {
        }
        case "FLIP90": {
        }
        case "TAP_TWICE": {
        }
        case "SHAKE_AIR": {
             // Code from mihome binding examples for the Xiaomi Gateway
             sendCommand(Gateway_SoundVolume, 2)
             sendCommand(Gateway_Sound, 11)
             Thread::sleep(2000) /* wait for 2 seconds */
             sendCommand(Gateway_Sound, 10000)
             sendCommand(Gateway_SoundVolume, 0)
        }
        case "FREE_FALL": {
        }
        case "ALERT": {
        }
    }
end

What I did:

  • Renamed the Rule name (replaced spaces with underscores)
  • Used Notepad++ to convert/encode with UTF-8 (UTF-8-BOM)
  • Copy&Paste the whole code once again in an empty UTF-8 text file again and saved it
  • Reboot Raspi and Openhab2
  • Same issues with other devices (Xiaomi Switch)

I will ignore the Eclipse Designer 0.8 debug errors, okay. (Is version 0.9 compatible?)
Thanks in advance for any useful hint!

0.9 is hopelessly broken at the moment. 0.8 is the best we have.

My best guess is there is some hidden character in there somewhere that is preventing OH from parsing the file. Your best bet may be copying over the rule file line by line, or even better, retyping it in to ensure there are no stray hidden characters hiding in there causing problems.

Okay, I will put Designer in its current state completely aside and focus on creating the required files with Notepad++ or other editors.
Because of all the file format issues (ANSI, UTF-8, UTF-8-BOM) I also thought of file format problems and ruled them out by retyping the code in an all-new empty file. No success, still parsing errors.
I converted back and forth with Notepad++ and tried other formats, but nothing helped.

Can someone confirm that rules are working for his own setup at all?
I am getting crazy and still blame OpenHAB and/or the binding as it was working with the same files before!
But I would like to get confirmed that something is broken with the binaries…

You might like VS Code :wink:

1 Like

Already heard of it, yeah, thanks. Does it run on Windows? Is there a quick step guide?

All of my rules run without error.

There is nothing apparent in the Rule itself that would cause the entire file to be unparsable.

There is nothing I know of in any binding that can cause a rule to become unparsable. Bindings and Rules are pretty thoroughly separated from each other.

Thousands of others are successfully running rules in 2.1.

So there has to be something weird and unique going on with this setup causing it to not be able to parse this file.

I would break it down even further. Start with an empty file.

Then add a simple rule triggered by an Item to see if that parses.

Then change the trigger to the Channel trigger and see if that parses.

Then add little by little until either the whole rule is present or the rule fails to parse.

Hi,
I’ll add to this post because it is similar to my issue I experience after upgrading to 2.1

I have some rules which are working, others are not. One of the rules which is not working anymore is related to Xiaomi mihome as well:

rule "Xiaomi Motion Sensor"
when
    Item MotionSensor_MotionStatus changed
then
    if (MotionSensor_MotionStatus.state == ON)
     { 
     if (now.isAfter((Sunset_Time.state as DateTimeType).calendar.timeInMillis) {
        sendCommand (WC_Decke_EG,ON) 
        }
    } 
    else {
        sendCommand (WC_Decke_EG,OFF)
    }
end

Now, the second rule is not related to mihome, but to KNX:

import.org.openhab.core.library.types.*
import org.openhab.model.script.actions.Timer
var Timer timer
rule "Licht Keller aus nach 5 Minuten"
when
Item KG_ALL changed
then
if(KG_ALL.state == ON) {
	timer = createTimer(now.plusMinutes(5)) [
	sendCommand(KG_ALL, OFF)
		]
} else {
if(timer!=null) {
	timer.cancel
	timer = null
	}
}
end

Both rules worked fine before upgrading to 2.1

Hope someone can help
thx

I also saved all rulles files via Notepad++ and also changed cases for my switch button rule due to I found that they are now differ from the first vertion of Xiaomi binding.
It’s working again :slight_smile:

Okay, I have created an all-new empty .rules file:

2017-07-09 13:46:49.767 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'xiaomi_new.rules'
2017-07-09 13:46:49.782 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'xiaomi_new.rules' is either empty or cannot be parsed correctly!

“Empty” is true, I will add more step by step and hope that I don’t need to reinstall the whole system again…:frowning:

Check out this issue on GitHub and see if it’s aligned with yours.
There’s a bug on the latest build.

No, the Xiaomi things are all properly initialized.
Now I’ve created a simple rule:

var Number counter
rule "Startup"
when
	system started
then
	counter = 0
end

This brings up the following in opehab.log:

2017-07-10 09:04:54.814 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'xiaomi_new.rules'
2017-07-10 09:04:54.824 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'xiaomi_new.rules' is either empty or cannot be parsed correctly!
2017-07-10 09:04:54.905 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'xiaomi_new.rules' has errors, therefore ignoring it: [4,2]: no viable alternative at input 'system'

Changed “system” to “System” (with capital S) and then it looks like this:

2017-07-10 09:16:43.983 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'xiaomi_new.rules'
2017-07-10 09:16:43.991 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'xiaomi_new.rules' is either empty or cannot be parsed correctly!
2017-07-10 09:16:44.154 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'xiaomi_new.rules'

First error can be ignored I think, because there are no further errors after the “Loading” line, right?

Yes, others have reported the empty error followed by the Loading model message and their rules seem to still work. So I would say that looks good.

Hi all,

after quite a while I had some time playing around again and could finally resolve the problem!
First I performed all software updates in OpenHABian, so everything is up-to-date now. All devices are still “Online” and responsive in Paper UI.

I used a very simple test rule for one of the Xiaomi Magic Cubes that makes a log entry:

rule "Xiaomi Cube Logging"
when
	Channel 'mihome:sensor_cube:1000000000abc1:action' triggered     /* just an example id */
then
	logInfo("RULE TEST", "Cube event triggered");
end

This did not work, so I renamed all files not required and just kept the important .items, .things, and .rules files, went through the steps to make sure they have the proper text file encoding (UTF-8 without BOM) in Notepad++ and after that it worked. Even the original .rules file is now working again. Maybe it was a combination of software updates, restarts and re-encoding/converting of the text files.

However, OpenHAB/OpenHABian seems to be STILL very sensitive to different UTF-8 file formats. I made some documentation notes for myself, so I can reconfirm which combination of formats and configs is definitely working.

Thanks for your help and ideas.