[SOLVED] Rule : how to split http binding result

My http bindings asks a python script ( garage door opener script) which returns the status:
request :

String GarageStatusLeft2 "[%s]" <garage> {http="<[http://192.168.1.55:8888/st?id=left:1000:REGEX((.*))]"}
return d.last_state

The rule says

 rule garageStatusLeft2
when
	Item GarageStatusLeft2 received update
then
	if (GarageStatusLeftDisp2.state.toString.toUpperCase != GarageStatusLeft2.state.toString.toUpperCase) {
		GarageStatusLeftDisp2.postUpdate(GarageStatusLeft2.state.toString.toUpperCase);
	}
end

This works. Now I would like to show the status along with the status date and time . So I have updated the python script to return:

return d.last_state + " # " + last_time

Return example : closed # 2018-05-01 07:57:13

I have changed the rule:

 rule garageStatusLeft2
when
	Item GarageStatusLeft2 received update
then
 #   val GarageStatusLeft3 =  GarageStatusLeft2.split("#")
	val GarageStatusLeft4 = GarageStatusLeft2.get(0)
	val GarageStatusLeft5 = GarageStatusLeft2.get(1)
	if (GarageStatusLeftDisp2.state.toString.toUpperCase != GarageStatusLeft4.state.toString.toUpperCase) {
		GarageStatusLeftDisp2.postUpdate(GarageStatusLeft4.state.toString.toUpperCase);
	}
end

I know that it is a very often asked question, but I have tried several answers without success:

  1. val GarageStatusLeft3 = GarageStatusLeft2.split("#")
    gives errors (split is unknown)
  2. val GarageStatusLeft4 = GarageStatusLeft3.get(0)
    gives errors “Rule ‘garageStatusLeft2’: ‘get’ is not a member of 'org.eclipse.smarthome.core.library.items.StringItem”.

I am a Python and openhab begineer. I have read that using json may help, but I don’t know how.
Any help or example is welcomed.
Gilles94

you need to put the return value into a buffer variable first, similar to the commented line
and you need to change the split to " # " or remove the spaces from the pyton

rule garageStatusLeft2
when
	Item GarageStatusLeft2 received update
then
	val GarageStatusBuffer = GarageStatusLeft2.state.toString.split(" # ")
	val GarageStatusLeft4 = GarageStatusBuffer.get(0)
	val GarageStatusLeft5 = GarageStatusBuffer.get(1)
	if (GarageStatusLeftDisp2.state.toString.toUpperCase != GarageStatusLeft4.state.toString.toUpperCase) {
		GarageStatusLeftDisp2.postUpdate(GarageStatusLeft4.state.toString.toUpperCase);
        logWarn("myLog", GarageStatusLeft5)
	}
end

Hi Mueslee,
I have just changed the rule with yours and now I have a new error message:

2018-05-01 13:21:55.538 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'garageStatusLeft2': 'state' is not a member of 'java.lang.String'; line 22, column 58, length 23
The issue is located in the "if" statement, second part after the !=

Any idea? Do I have to activate something to make it work?

Gilles94

ah i forgot to adjust the if statement, this should do

wow,
Thanks a lot Mueslee.
it works now. I have also added the missing item and sitemap statements.
I have a better understanding now between the sitemap, the item and the rule.I am also happy to have learnt something because I may have to reuse this somewhere else.
Gilles94

1 Like