Problem defining a variable from a sensor

Hello all,

I’m having a problem defining a variable from a sensor state.
The sensor is a HTTP GET command and produces a value, for example 16.5. I’m not sure is this is a “char” string.

This is the command I’m using:
CustomState(source == “Dummy”, setpointinit : value);

In boot.log I see the following error:
Rule Compilation error source cannot be resolved to a variable
Syntax error on token “:”, :: expected

Can someone help?

Thanks in advance
Cheers,
Rene

Seems like there is ; after CustomState(). Remove it. In case of more errors send the complete code.

Hello Michal,

Removing the ; did not solve my problem. I guess I’m going about it the wrong way.
I’m not a Java programmer :slight_smile:
The thing I’m trying to do is relatively simpel:
Assign an initial value to an in-memory sensor using the value of another sensor.
I’ve been looking at a lot of examples but I can’t work it out…

Could you give me some pointers?
Thanks.

Regards,
Rene

Are you getting the same syntax error? Please send the complete rule which isn't working as it should. Also, please try to explain the complete scenario you want to do.

As a basis I used the demo example. The functionality is a display of a temperature with a “+” an “-” button for controlling a thermostat.
This part I got working. My problem is setting the initial value of the display. For this I want to use the acual setting of the thermostat.
The sensor is working but I can’t get this value into a variable.
The error is:

Rule Compilation error source cannot be resolved to a variable
setpointinit cannot be resolved
Syntax error on token “:”, :: expected

Code of the rule:

rule “-PSB: Init”
salience 10
then
CustomState(source == “Dummy”, setpointinit : value);
execute.command(“kachel-setpoint-inc”,“OFF”);
execute.command(“kachel-setpoint-dec”,“OFF”);
end

At least you’ve missed the LHS of the rule. It should look something like:

rule “init”

when

CustomState(source == “Dummy”, setpointinit : value)

then

// and here you need to set the initial value of your thermostat

execute.command(“your thermostat setpoint command”, setpointinit.toString() );

end

Ok, that gets the value in the variable but it gets updated every time the value of “Dummy” changes…

Then you need to init it only when the value is not initialized yet, like this one

rule "init"
when
  CustomState(source == "Dummy", setpointinit : value)
  Event(source=="your thermostat set point sensor in-memory var", value=="")
then
  // and here you need to set the initial value of your thermostat
  execute.command("your thermostat setpoint command", setpointinit.toString() );
end

Uninitialized var can be either empty stiring "" or "status". You can have two rules to cover both.

Hi Michal,

Finally I’ve got it working :slight_smile:
I think the main problem was that the defining of the variable is done in the “when” part. That wasn’t logical to me…
Thanks for the assist.

Cheers,
Rene

Glad to hear this! It would be nice if you can post the final rules which are working for you. This way, the others with similar problem can see how it should be done.

And here’s the complete code of the rules for my thermostat:

package org.openremote.controller.protocol;

import java.util.;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.
;
import org.openremote.controller.model.event.*;
import org.openremote.controller.utils.Logger;
import org.openremote.controller.Constants;

global org.openremote.controller.statuscache.CommandFacade execute;

function void log(String msg)
{
Logger.getLogger(Constants.RUNTIME_EVENTPROCESSOR_LOG_CATEGORY + “.drools”).debug(msg);
}

declare Event
@role(event)
end

rule “init”
salience 10
when
CustomState(source == “kachel-setpoint-status”, setpointinit : value)
Event(source == “kachel-setpoint”, value == “”)
then
execute.command(“kachel-setpoint”,setpointinit);
execute.command(“kachel-setpoint-inc”,“OFF”);
execute.command(“kachel-setpoint-dec”,“OFF”);
end

function Double _GetTemp(Object o){
String s = o.toString();
try{
if(s.length()>1){
return(Double.parseDouble(s.substring(0, s.length()-1)));
}else{
return(0.0);
}
} catch (NumberFormatException e) {
return(0.0);
}
}

function String _ShiftTemp(Object o, double sh){
String s = o.toString();
try{
Double t = Double.parseDouble(s.substring(0,s.length()-1)) + sh;
return(String.format("%.1f\u00B0",t));
} catch (NumberFormatException e) {
return(“0.0\u00B0”);
}
}

function String _ShiftTemp_noformat(Object o, double sh){
String s = o.toString();
try{
Double t = Double.parseDouble(s.substring(0,s.length()-1)) + sh;
String rs = t.toString();
return(rs.substring(0,4));
} catch (NumberFormatException e) {
return(“0.0\u00B0”);
}
}

rule “kachel-setpoint inc”
timer(int:10ms)
when
Event(source == “kachel-setpoint”, $v: value, eval(_GetTemp(value) < 23))
Event(source == “kachel-setpoint-inc” , value == “ON”)
then
execute.command(“kachel-setpoint-inc”,“off”);
execute.command(“kachel-setpoint-dec”,“OFF”);
execute.command(“kachel-setpoint”, _ShiftTemp($v.toString(), 0.5));
execute.command(“kachel-setpoint-set”, _ShiftTemp_noformat($v.toString(), 0.5));
end

rule “kachel-setpoint dec”
timer(int:10ms)
when
Event(source == “kachel-setpoint”, $v: value, eval(_GetTemp(value) > 15))
Event(source == “kachel-setpoint-dec” , value == “ON”)
then
execute.command(“kachel-setpoint-inc”,“OFF”);
execute.command(“kachel-setpoint-dec”,“off”);
execute.command(“kachel-setpoint”, _ShiftTemp($v.toString(), -0.5));
execute.command(“kachel-setpoint-set”, _ShiftTemp_noformat($v.toString(), -0.5));
end

rule “kachel-setpoint INC/dec” // needed when change on the boundary
salience -10
timer(int:300ms)
when
Event(source == “kachel-setpoint-inc” , value == “ON”)
then
execute.command(“kachel-setpoint-inc”,“off”);
execute.command(“kachel-setpoint-dec”,“OFF”);
end

rule “kachel-setpoint inc/DEC” // needed when change on the boundary
salience -10
timer(int:300ms)
when
Event(source == “kachel-setpoint-dec” , value == “ON”)
then
execute.command(“kachel-setpoint-inc”,“OFF”);
execute.command(“kachel-setpoint-dec”,“off”);
end