This page last changed on May 19, 2013 by aktur.


I would like to correct value of a sensor, for example when sensor shows 20.0 I would like to subtract 2.5 from it so it becomes 17.5. What would be the best approach of doing this in OpenRemote. Quick and dirty solution would be to call 'bc' via the Shell execution protocol but this is not portable and not in spirit of OR. Can you give me some hints how to do it properly?

Depends on what kind of command drives the sensor. If it is XML you can use XPATH for the calculation

Posted by pz1 at May 19, 2013 14:31

Unfortunately it is EnOcean sensor. But in fact I'm looking for a generic method of doing this. Perhaps it will be somewhere in rules. I know how to make a rule sensitive to sensor and modify a value. What I miss is how to send the rule's result back to UI.

Posted by aktur at May 19, 2013 15:48

If the rule updates the sensor value the UI should reflect the latest value automatically.

Posted by mredeker at May 20, 2013 18:26

Hi Marcus,

but what is the syntax for updating the sensor value? The code below has no effect:

rule "Correct Temperature"
   Event(source=="Temperature Correction", $temp:value)
   $temp = $temp - 2.5;
Posted by aktur at May 20, 2013 20:15

You have to assign the new value back to the sensor.
This is just an educated guess:

rule "Correct Temperature"
   $evt:Event(source=="Temperature Correction")
   $evt.setValue(eval($evt.getValue -2.5));
Posted by mredeker at May 20, 2013 20:27

I've changed this to get it through the compiler:

rule "Correct Temperature"
   $evt : Event(source=="TempC")
   $evt.setValue(Double.parseDouble($evt.getValue().toString()) - 2.5);

But still the label attached to TempC sensor shows uncorrected value.

Posted by aktur at May 20, 2013 21:38

Ok, I just checked the code and this only updates the event but not the sensor.
I have to check if it is possible to access the sensor or the status cache from within the rules.

Posted by mredeker at May 20, 2013 22:00

I remember having seen one or two posts in the past that said parameter passing was not possible in Drools 5.1, but might be available in version 5.2. I do not remember the context in which it was said.

Posted by pz1 at May 21, 2013 07:49

One "trick / ugly hack" you might use is as follow:

  • have 2 sensors, the actual that's read from the device and a virtual that is linked to your UI
  • for the virtual sensor, you would have 2 "in-memory" commands: a read (command: STATUS) and a write (command : ON)
  • have a rule on the actual value, do your computation, then call the write command

So something like (haven't checked in rules engine, so might need to fix syntax errors)

rule "Correct Temperature"
   $evt : Event(source=="TempC")
   double correctedValue = Double.parseDouble($evt.getValue().toString()) - 2.5;
   execute.command("VirtualWrite", Double.toString(correctedValue));
Posted by ebariaux at May 21, 2013 12:21

Hi Eric, yes it works nicely . I was not aware that one can write arbitrary string to the in-memory command. Now the calibration can be moved from hardware (expensive) to software (cheap). Yet another advantage of OpenRemote (YAAoOR)

Posted by aktur at May 21, 2013 13:08

I've summarized the whole story on my web
as this example is very useful for me. This mechanism is much more powerful than solely correcting a sensor value. Like in my example it is converting Celsius into Fahrenheit – useful for Americans who buy overseas sensors.

By the way, is it possible to put an arbitrary string into "in-memory" beside rules?

Posted by aktur at May 22, 2013 20:24

There currently is a validation rule in the designer that limits the input to ON, OFF and STATUS.
I don't know the history behind that, so maybe we could remove that limit and everything should work just fine (I believe this should be the case).

I'll discuss this with the team when I get the chance.

Posted by ebariaux at May 23, 2013 10:06

Limited it to ON/OFF/STATUS because people were confused as to how to use the in-memory commands, entering arbitrary values and wondering how it should work.

You can remove the validation under the condition that you produce wiki documentation on how to correctly use the commands that we can refer users to

Posted by juha at May 23, 2013 12:02
Document generated by Confluence on Jun 05, 2016 09:40