Rule matching a string

Hi I am trying to work round a problem with the rfxcom binding which does not fully support lighting4.

I found some cheap window door contacts which output 3 signals (open, closed and tamper) but these do not get picked up by the command value.

The workround seems to be to use the RawData value but this is a long string and I just need to match part of it.

So far I have set up an Item

String garage_1 { rfxcom="<158674:RawData" }

I can output this to the log file and I can see that there is a sequence of characters which relate to the three different values.

This is an example RawData output and the 10th - 16th characters tell me the door state. The other values keep changing.
0913001E26BD2E013970

I’ve look on the drools site and I see there are commands for matches or contains but in designer it does not seem accept this.

I was hoping to do something like this:

rule "garage_1 door status"
when
Item garage_1 changed
then
if
garage_1.state contains (xyc123)
{
sendCommand(garage, ON)
}
else if
garage_1.state contains (xyc124)
{
sendCommand(garage, OFF)
}
end

Hope this makes sense and someone can help!

Thanks

You need to convert the .state to a string before you can use the method .contains!

rule "garage_1 door status"
when 	
   Item garage_1 changed
then	
  if garage_1.state.toString.contains ("xyc123") {	
      sendCommand(garage, ON)	 
  }else if  garage_1.state.toString.contains ("xyc124")       {       
     sendCommand(garage, OFF) 
  }
end
1 Like

Thanks that works great! Only change is that the if statement needed to be in brackets…like so

if (garage_1.state.toString.contains (“xyc123”)) {
sendCommand(garage, ON)

Hi,

currently facing issue as well with string comparison. Goal is based on weather information switching on the climate.

Rule:

rule "Klima Vacation ON"
when 
	Time cron "0 */5 9-17 ? * MON-SUN" // daily 9-19:45 h alle 15 minuten
then
	if (Weather_Temperature.state=>23) { 

		if (Weather_Code.state == "partly cloudy") {
			sendCommand(AirConDG, ON)
			sendCommand(VentilatorDG, ON)
			sendCommand(AirConSZ, ON)
		}
		if (Weather_Code.state.toString == "partly-cloudy-day") {
			sendCommand(AirConDG, ON)
			sendCommand(VentilatorDG, ON)
			sendCommand(AirConSZ, ON)
		}
end

Item Content:

   
<item>
<type>StringItem</type>
<name>Weather_Code</name>
<state>sunny</state>
<link>http://192.168.1.10:8080/rest/items/Weather_Code</link>
</item>

error message i get is:

14:00:02.145 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob:57   ] - Error during the execution of rule Klima Vacation ON
java.lang.IllegalStateException: Could not invoke method: org.eclipse.xtext.xbase.lib.ObjectExtensions.operator_doubleArrow(T,org.eclipse.xtext.xbase.lib.Procedures$Procedure1) on instance: null
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:738) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._featureCallOperation(XbaseInterpreter.java:713) ~[na:na]
	at sun.reflect.GeneratedMethodAccessor60.invoke(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
	at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
	at org.openhab.model.script.interpreter.ScriptInterpreter.internalFeatureCallDispatch(ScriptInterpreter.java:69) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateAbstractFeatureCall(XbaseInterpreter.java:658) ~[na:na]
	at sun.reflect.GeneratedMethodAccessor53.invoke(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
	at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateIfExpression(XbaseInterpreter.java:327) ~[na:na]
	at sun.reflect.GeneratedMethodAccessor64.invoke(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
	at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateBlockExpression(XbaseInterpreter.java:321) ~[na:na]
	at sun.reflect.GeneratedMethodAccessor65.invoke(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
	at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:204) ~[na:na]
	at org.openhab.model.script.internal.engine.ScriptImpl.execute(ScriptImpl.java:59) ~[na:na]
	at org.openhab.model.rule.internal.engine.ExecuteRuleJob.execute(ExecuteRuleJob.java:55) ~[na:na]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:213) [quartz-all-2.1.7.jar:na]
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557) [quartz-all-2.1.7.jar:na]
Caused by: java.lang.IllegalArgumentException: argument type mismatch
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:729) ~[na:na]
	... 29 common frames omitted

someone able to assist me with?

thks
Karsten

Looks like the important part, suggesting this line is in trouble

if (Weather_Temperature.state=>23) {

Is Weather_Temperature a Number type Item, do you need to convert it to do numeric comparisons perhaps?

yes - defined as follows:

Number Weather_Temperature “Temperature [%.2f °C]”

Pop in some logInfo() to see what value it is taking, you may need to account for it being UNDEF

I think you meant
Weather_Temperature.state >= 23
“>=” is “greater than or equal to”
“=>” is the operator_doubleArrow, probably not what you intended

1 Like