Sent data out serial also shows up in received command

I am working on Pool automation and I have some serial sensor boards to interface with. To get a value I have to send a “r/r” out the serial port. That part is working fine and I see:

2016-07-10 12:31:38.001 [INFO ] [runtime.busevents             ] - Pool_ORP_Sensor received command r
2016-07-10 12:31:38.908 [INFO ] [runtime.busevents             ] - Pool_ORP_Sensor state updated to 328.4

I then have a rule with “Item Pool_ORP_Sensor received update”, however my problem is that rule is triggering on the “r” I am sending as well as the response back. I guess from the log I expected to just get the update 328.4.

BTW, I also am having trouble filtering out the “r” from the received update. I have tried to match it with:

if (Pool_ORP_Sensor.state != "r") {

and

if (Pool_ORP_Sensor.state.toString != "r") {

but both fail to match the r that is there.

Was missing the /r, should be:

 if (Pool_ORP_Sensor.state != "r\r") {

Still tho not sure why the rule ever sees the sent data.

Hmm, still not working. I have the r\r stripped out and Pool_ORP is a Number item, but it shows up as Uninitialized.

rule "Pool_ORP_Sensor"
when
  Item Pool_ORP_Sensor received update
then
  if (Pool_ORP_Sensor.state != "r\r") {
    Pool_ORP.postUpdate(Pool_ORP_Sensor.state)
    logInfo("Pool","ORP: " + Pool_ORP.state)
  }
end

The log shows:
2016-07-10 13:50:24.055 [INFO ] [runtime.busevents ] - Pool_ORP_Sensor received command r
2016-07-10 13:50:24.962 [INFO ] [runtime.busevents ] - Pool_ORP_Sensor state updated to 296.4
2016-07-10 13:50:24.962 [INFO ] [runtime.busevents ] - Pool_ORP state updated to 296.4
2016-07-10 13:50:24.963 [INFO ] [org.openhab.model.script.Pool ] - ORP: Uninitialized

I would expect postUpdate to convert 296.4 string to a Number.

There might be some white space around it. Also, sometimes I find it works better to explicitly call toString when comparing a State to a string, even for String Items. Try:

if(Pool_ORP_Sensor.state.toString.trim != "r") {

Throw in a sleep between the postUpdate and the logInfo in that rule. It takes some time for a postUpdate to go out to the event bus, and come back. It doesn’t have to be long, maybe 50-100 msec.

Tried 250 ms, still the same thing. I even manually did a postUpdate(Pool_ORP, -48) and now I just always see -48. It’s very odd because it says it is being updated in the logs.

2016-07-10 17:32:53.435 [INFO ] [runtime.busevents             ] - Pool_ORP_Sensor received command r
2016-07-10 17:32:54.342 [INFO ] [runtime.busevents             ] - Pool_ORP_Sensor state updated to -18.6
2016-07-10 17:32:54.342 [INFO ] [runtime.busevents             ] - Pool_ORP state updated to -18.6
2016-07-10 17:32:54.593 [INFO ] [org.openhab.model.script.Pool ] - ORP: -48

Rule:

rule "Pool_ORP_Sensor"
when
  Item Pool_ORP_Sensor received update
then
  if (Pool_ORP_Sensor.state != "r\r") {
    Pool_ORP.postUpdate(Pool_ORP_Sensor.state)
    Thread::sleep(250)
    logInfo("Pool","ORP: " + Pool_ORP.state)
  }
end

I can also tell that rrd4j.log is only saving the manual -48 I put in.

rrd4j.log:2016-07-10 17:22:07 DEBUG o.o.p.r.internal.RRD4jService[:132]- Stored 'Pool_ORP' with state '-48' in rrd4j database
rrd4j.log:2016-07-10 17:23:06 DEBUG o.o.p.r.internal.RRD4jService[:132]- Stored 'Pool_ORP' with state '-48' in rrd4j database
rrd4j.log:2016-07-10 17:24:06 DEBUG o.o.p.r.internal.RRD4jService[:132]- Stored 'Pool_ORP' with state '-48' in rrd4j database
rrd4j.log:2016-07-10 17:25:08 DEBUG o.o.p.r.internal.RRD4jService[:132]- Stored 'Pool_ORP' with state '-48' in rrd4j database
rrd4j.log:2016-07-10 17:26:08 DEBUG o.o.p.r.internal.RRD4jService[:132]- Stored 'Pool_ORP' with state '-48' in rrd4j database
rrd4j.log:2016-07-10 17:27:07 DEBUG o.o.p.r.internal.RRD4jService[:132]- Stored 'Pool_ORP' with state '-48' in rrd4j database
rrd4j.log:2016-07-10 17:28:07 DEBUG o.o.p.r.internal.RRD4jService[:132]- Stored 'Pool_ORP' with state '-48' in rrd4j database
rrd4j.log:2016-07-10 17:29:09 DEBUG o.o.p.r.internal.RRD4jService[:132]- Stored 'Pool_ORP' with state '-48' in rrd4j database
rrd4j.log:2016-07-10 17:30:08 DEBUG o.o.p.r.internal.RRD4jService[:132]- Stored 'Pool_ORP' with state '-48' in rrd4j database
rrd4j.log:2016-07-10 17:31:08 DEBUG o.o.p.r.internal.RRD4jService[:132]- Stored 'Pool_ORP' with state '-48' in rrd4j database
rrd4j.log:2016-07-10 17:32:07 DEBUG o.o.p.r.internal.RRD4jService[:132]- Stored 'Pool_ORP' with state '-48' in rrd4j database
rrd4j.log:2016-07-10 17:33:10 DEBUG o.o.p.r.internal.RRD4jService[:132]- Stored 'Pool_ORP' with state '-48' in rrd4j database
rrd4j.log:2016-07-10 17:34:07 DEBUG o.o.p.r.internal.RRD4jService[:132]- Stored 'Pool_ORP' with state '-48' in rrd4j database
rrd4j.log:2016-07-10 17:35:07 DEBUG o.o.p.r.internal.RRD4jService[:132]- Stored 'Pool_ORP' with state '-48' in rrd4j database

This is also odd, I tried just using var Number:

rule "Pool_ORP_Sensor"
when
  Item Pool_ORP_Sensor received update
then
  if (Pool_ORP_Sensor.state != "r\r") {
    var Number temp = Pool_ORP_Sensor.state
    Pool_ORP.postUpdate(temp)
    Thread::sleep(250)
    logInfo("Pool","ORP: " + Pool_ORP.state)
  }
end

I would expect that to also work, but get:

2016-07-10 17:39:00.332 [INFO ] [runtime.busevents             ] - Pool_ORP_Sensor received command r
2016-07-10 17:39:01.238 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Pool_ORP_Sensor': Could not invoke method: org.openhab.model.script.actions.BusEvent.postUpdate(org.openhab.core.items.Item,java.lang.Number) on instance: null

Odd.

It must have been some special char or something, I ended up using postUpdate(Pool_pH, Pool_pH_Sensor.state.toString.trim) and things worked fine. The thing that had me for so long was that the logs showed Pool_pH being updated correctly when actually it was not.

Is there a way to match if string does not start with t? The ORP sensor just requires me to send a “r”, but the pH sensor requires me to also send "t,{pool temp in C}. So I would like something like what I have below, but where it moves forward if there is no “r\r” or no line that starts with “t*”.

rule "Ping Pool Sensors"
when
  Time cron "0/10 * * * * ?"
then
  var Number temp = Pool_Temperature.state
  var tempC = (temp - 32) * 5/9
  //sendCommand(Pool_pH_Sensor, 't,' + tempC + '\r')
  sendCommand(Pool_pH_Sensor, 'r\r')
  Thread::sleep(100)
  sendCommand(Pool_ORP_Sensor, 'r\r')
end


rule "Pool_pH_Sensor"
when
  Item Pool_pH_Sensor received update
then
  if ((Pool_pH_Sensor.state != "r\r") && (Pool_pH_Sensor.state.toString.trim != "t")) {
    postUpdate(Pool_pH, Pool_pH_Sensor.state.toString.trim)
  }
end
if(!Pool_ORP_Sensor.state.toString.startsWith("t")) {

The startsWith method returns a boolean and the ! at the front negates that. Combined the result is it returns true for Strings that do not start with “t”.

1 Like