OpenHAB2 rules not working

Hi,

Hoping someone can help me here. I’ve searched through the wiki and the forums but no luck.

I’ve created a rule in Habmin 2 which should detect a change in the scene number received from my Fibaro Swipe. Based on the number, it should then turn on or off either the light or electric heater in my bedroom.

I can manually control the light and heater items via habmin, android app (rotini), or even tasker using the rest API.

Here is how it is created in habmin:

The actual script generated seems to have a problem in it as there is no “when” section filled in.

// This rule file is autogenerated by HABmin.
// Any changes made manually to this file will be overwritten next time HABmin rules are saved.
// Imports
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
// Global Variables
rule "swipe_right"
when
then
  if ((swipe_scene == 3)) {
    sendCommand(bedroom_heater, OFF)
  }
  else if ((swipe_scene == 4)) {
    sendCommand(bedroom_heater, ON)
  }
  else if ((swipe_scene == 1)) {
    sendCommand(bedroom_light, ON)
  }
  else if ((swipe_scene == 2)) {
    sendCommand(bedroom_light, OFF)
  }
end

If I edit it to include a when statement it still doesn’t work.

 when
  Item swipe_scene changed

I get no info in the log files about rules at all so I feel like the rules aren’t even running to the point of detecting any changes.

The log does show the scene number changing to the correct number so that part is working, pretty sure it’s just something with the actual rule itself.

Without adding logging statements to your rules you will not see anything in your logs unless there is an error. Add some logInfo statements (i don’t know how to do this in Habmin) and that should show you that the rule is running at least.

OK I thought that might be the case but wasn’t 100%.

How do I add the logInfo statements? I keep seeing references to defining the name of my logger in logback.xml but I don’t actually know where that file is…

I don’t know if OH 2 is different. In OH 1 you just use one of the log actions based on the level you want the statement to appear as (e.g. logInfo will show up as an Info level, logDebug as a debug level, etc.) By default the logback.xml is configured to show Info and above (i.e. warning and error).

OK, I’ve added in a log actions just after then, so:

when
	Item swipe_scene changed
then
	logInfo("Swipe scene changed")
	if ((swipe_scene == 3)) {
		sendCommand(bedroom_heater, OFF)
	}

After adding that line I get an error in the log:

2016-05-04 20:22:29.966 [ERROR] [.script.engine.ScriptExecutionThread] - Error during the execution of rule ‘swipe_right’: An error occured during the script execution: index=1, size=1

This is triggered when I perform a swipe so it looks like the rules IS running, just not doing anything after the “then” line.

If I remove the log action line again, I now get a new error:

2016-05-04 20:23:31.627 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘(3)_swipe_right.rules’ is either empty or cannot be parsed correctly!

Per the documentation at the link I sent you, logInfo requires two Strings, a kind of tag and the message. Try logInfo("Testing", "Swipe scene change")

Ah right, I was confused with that bit as the documentation also said:

Note2: Remember that if the name of your logger is “kitchen” you have to define it in logback.xml as

And I couldn’t find the logback.xml file to do that bit.

Anyway, thanks for the help - once I enabled logging properly I could see the rule was running properly so the problem was in the actual if statements.

I just had to change swipe_scene == 3 to swipe_scene.state == 3 etc and now it works fine.

@chris I’ve just noticed issue #162 on HABmin - looks like the same problem…

Hi Guys

I work with my swipe for some ours now, and i think i made a mistake in the rules.

I have OH2 running as Openhabian on a raspi.

I created the following:

Item file
String swipe_scene

Rule
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*

rule “Swipe”

when
Item swipe_scene changed
then
logInfo(“Swipe scene changed”)
if ((swipe_scene.state == 2)) {
sendCommand(Licht_Buero, OFF)
}

In my log, I can see the commands but nothing happens
2017-06-22 22:48:01.498 [ItemStateChangedEvent ] - swipe_scene changed from 3.0 to 2.0

I don´t know where I made the mistake.

THX
Daniel

Hi Daniel,

You should remove the imports, they are not needed anymore in openHAB 2.0 (at least not for the org.openhab.* packages).

Your swipe_scene is defined as a String, however in your rule you compare it with a number. Also, your log seems to indicate 2.0 yet you compare it with 2. When comparing strings 2.0 and 2 are not the same.

Can you try the following rule:

rule "Swipe"
when
  Item swipe_scene changed
then
  logInfo("Swipe scene changed")
  if ((swipe_scene.state == "2.0")) {
    sendCommand(Licht_Buero, OFF)
  }
end

You should see the “Swipe scene changed” log entry in your openhab.log file.

Hi Marcel

Thx for the fast reply.
After I changed it from 2 to 2.0 it worked sometimes. (Myabe it needs some time - in most cases it works when i swipe down and then up (Maybe this has something to do with the chang - because from 1 to 1 is no change?
In my log file i don´t see the log entry so I faded the line out.

I changed my rule to turn light on and off, et voila - doesn´t work anymory.

My rule looks like this at the moment

rule “Swipe UP”

when
Item swipe_scene changed
then
//logInfo(“Swipe scene changed”)
if ((swipe_scene.state == 1.0)) {
sendCommand(Licht_Kochbereich, ON)
else if ((swipe_scene.state == 2.0)) {
sendCommand(Licht_Kochbereich, OFF)

}
end

Every ID on the swipe could mean two thing. One time up, ON, two times up- OFF, is that correct?

Can i only turn things OFF with two times UP/DOWN or can i change this somehow?

Would you recommend a number or a string item?

Thx
Daniel

a my log
2017-06-22 23:49:41.282 [ItemStateChangedEvent ] - swipe_scene changed from 2.0 to 1.0
2017-06-22 23:49:41.368 [ItemCommandEvent ] - Item ‘Licht_Kochbereich’ received command ON
2017-06-22 23:49:43.806 [ItemStateChangedEvent ] - swipe_scene changed from 1.0 to 2.0
2017-06-22 23:49:44.013 [ItemStateChangedEvent ] - swipe_scene changed from 2.0 to 1.0
2017-06-22 23:49:44.118 [ItemCommandEvent ] - Item ‘Licht_Kochbereich’ received command ON
2017-06-22 23:49:48.044 [ItemStateChangedEvent ] - swipe_scene changed from 1.0 to 2.0

I took a quick look at how I compare String items in my rules:

	var mediatype = Kodi_LivingRoom_MediaType.state.toString() 
	if ((mediatype == "movie") || (mediatype == "episode"))

So that that would make your rule look like:

rule "Swipe UP"
when
  Item swipe_scene changed
then

  var nrOfSwipes = swipe_scene.state.toString()
  logInfo("RULE: Swipe UP", "Swipe scene changed to: " + nrOfSwipes)

  if (nrOfSwipes == "1.0") {
    sendCommand(Licht_Kochbereich, ON)
  else if (nrOfSwipes == "2.0") {
    sendCommand(Licht_Kochbereich, OFF)
  }
end

Regarding the log entry not ending up in the log, this was probably because the command was incorrect. It needs two parameters, not one so I have updated it in the example above. Also, make sure you check the openhab.log, not the event.log. In the event.log you will only find state changes.

I cannot comment on the the device itself because I don’t own one.

THX

that will help a lot.

So would you suggest to use a string item instead of number?

What do you think about the change? Is the rule only triggered by a change of the item state?

It should be triggerd every time, the swipe sends a strin/number from 1 to 1 e.g.

THX

Hi Daniel,

I’m not suggesting you to use a String item instead of a Number. It’s what you have defined in your items file :slight_smile:

However, if you want to use strings then you must make sure that when comparing them it is done in the correct way.

Using the current rule trigger, I don’t think that it will trigger from 1.0 to 1.0 because there is no change in value. For that to happen you need the following trigger:

rule "Swipe UP"
when
  Item swipe_scene received command
then

(see also: Event-based Triggers in the documentation)

Hi Marcel

I tried your rule, but it seems, that theres nothing happening. -->no line in the events log. As long as I use a string item, nothing happens in the events. When i change the item to number i get the following:

2017-06-27 22:51:27.837 [ItemStateChangedEvent ] - swipe_scene changed from NULL to 0
2017-06-27 22:51:28.248 [ItemStateChangedEvent ] - swipe_scene changed from 0 to 1.0
2017-06-27 22:51:28.412 [ItemStateChangedEvent ] - swipe_scene changed from 1.0 to 0
2017-06-27 22:51:28.886 [ItemStateChangedEvent ] - swipe_scene changed from 0 to 3.0
2017-06-27 22:51:29.006 [ItemStateChangedEvent ] - swipe_scene changed from 3.0 to 0

The openhab log looks like this.
2017-06-27 22:51:26.285 [WARN ] [class.ZWaveMultiInstanceCommandClass] - NODE 4: Originating Command Class CENTRAL_SCENE (0x5b) was on the root node.
2017-06-27 22:51:26.698 [WARN ] [class.ZWaveMultiInstanceCommandClass] - NODE 4: Originating Command Class CENTRAL_SCENE (0x5b) was on the root node.
2017-06-27 22:51:26.829 [WARN ] [class.ZWaveMultiInstanceCommandClass] - NODE 4: Originating Command Class BASIC (0x20) was on the root node.
2017-06-27 22:51:26.942 [WARN ] [class.ZWaveMultiInstanceCommandClass] - NODE 4: Originating Command Class BASIC (0x20) was on the root node.
2017-06-27 22:51:27.479 [WARN ] [class.ZWaveMultiInstanceCommandClass] - NODE 4: Originating Command Class CENTRAL_SCENE (0x5b) was on the root node.
2017-06-27 22:51:28.076 [WARN ] [class.ZWaveMultiInstanceCommandClass] - NODE 4: Originating Command Class CENTRAL_SCENE (0x5b) was on the root node.
2017-06-27 22:51:28.285 [WARN ] [class.ZWaveMultiInstanceCommandClass] - NODE 4: Originating Command Class BASIC (0x20) was on the root node.
2017-06-27 22:51:28.405 [WARN ] [class.ZWaveMultiInstanceCommandClass] - NODE 4: Originating Command Class BASIC (0x20) was on the root node.
2017-06-27 22:51:28.775 [WARN ] [class.ZWaveMultiInstanceCommandClass] - NODE 4: Originating Command Class CENTRAL_SCENE (0x5b) was on the root node

So if it gets the event of 0 to 1.0 why deosent it change anything.

my items:
Number swipe_scene
Switch Licht_Buero “Deckenlampe Büro” (gEG_Buero, gLichter, gLichterEG) [ “Lighting” ] {knx=“1/1/95+<10/1/70”}

Maybe you or someone else has an idea what is wrong.

THX