[SOLVED] Rule with less-than / more-than signs < > Syntax error?

Hi folks,

Im trying to switch my ceiling light to “white mode” if my BH-1750 lumen sensor value is under or above a given value.

The sensor is ok, and is updated every 5 minutes.

String  Switch00Lux     "Illuminance"       <sunrise>       (gSwitch_1)     { channel="mqtt:topic:Switch00:illuminance" }

But this rule doesn’t work:

rule "Change Ceiling Light to White"
when
    Item Switch00Lux received update
then
    if ( Switch00Lux.state < 10 ) {
        logInfo("Lumination", "it's getting to dark, illuminating!")
        Bridge1_CH2_White.sendCommand(ON)
    }
    else if ( Switch00Lux.state > 9 ) {
        logInfo("Lumination", "it's illuminated already")
    }
    else logInfo("Lumination", "this rule does not function :(")
end

I tried to figure out by myself what’s going wrong but with no luck…

The rule gets fired everytime my Item sends an update.
The Item publishes a state.

So I think the problem is the if clause. I don’t know the right syntax.

I also tried:

if ( Switch00Lux.state < "10" ) {
if ( Switch00Lux.state < '10' ) {
if ( Switch00Lux.state.toString < 10 ) {
if ( Switch00Lux.state.toString < "10" ) {
if ( Switch00Lux.state.toString < '10' ) {
if ( Switch00Lux < "10" ) {

state.toString causes an error in events log so this will definately not work. but I still dont know if and how I can use less-than / more-than signs.

Maybe someone knows the answer?!??

You may need to post your error messages to get any feedback

@Dan Try

if ( Switch00Lux.state as Number < 10 ) {

the error says that state.toString can’t be compared to “10” - different data types… but the error is not the problem…

Ok will try that, looks promising to me :wink:

==> /var/log/openhab2/openhab.log <==
2019-01-27 18:10:27.885 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'luminance.rules' has errors, therefore ignoring it: [6,40]: no viable alternative at input '10'

I get the above when trying with “as Number” in the if clause…

maybe any other suggestions??

Not sure if this will make a difference but you can try it.

rule "Change Ceiling Light to White"
when
    Item Switch00Lux received update
then
    var lux = Switch00Lux.state as Number
    if ( lux < 10 ) {
        logInfo("Lumination", "it's getting to dark, illuminating!")
        Bridge1_CH2_White.sendCommand(ON)
    }
    else if ( lux > 9 ) {
        logInfo("Lumination", "it's illuminated already")
    }
    else logInfo("Lumination", "this rule does not function :(")
end

Will the item still work if you change it from a String to a Number?

Number  Switch00Lux     "Illuminance"       <sunrise>       (gSwitch_1)     { channel="mqtt:topic:Switch00:illuminance" }

I have to give it a try - will report back after testing…
thanks a lot for your help!

I also tested it with a similar conclusion like:

val lumination = Switch.state

this also not functioning - forgot to mention that in my first post…

Note, I forgot to add “as Number” to the var in the rule but I have edited it so it’s included. Hope it works.:crossed_fingers:

thanks I’ve seen your edit… :wink:

the rule throws no errors anymore… I have to wait for the item to update.
but my rule produced no errors also - so we will see…

afterwards I will try to change the Item Type if this isn’t funtioning…

==> /var/log/openhab2/openhab.log <==
2019-01-27 18:44:16.054 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Change Ceiling Light to White': Could not cast 32 to java.lang.Number; line 5, column 22, length 27

Ok this error without changing the data type of the item

At least we have a value now (32).:laughing:

Drop the Number part in the var so its just var lux = Switch00Lux.state as Number

tried that…

==> /var/log/openhab2/openhab.log <==
2019-01-27 18:54:16.263 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Change Ceiling Light to White': Could not cast 135 to java.lang.Number; line 5, column 15, length 27

will try the other way round e.g.

var Number lux = Switch00Lux.state

no luck…

==> /var/log/openhab2/openhab.log <==
2019-01-27 18:59:16.810 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Change Ceiling Light to White': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.lib.NumberExtensions.operator_lessThan(java.lang.Number,java.lang.Number) on instance: null

Try var int lux = Switch00Lux.state as Number

EDIT: I doubt that will work either so try using val instead of var

val lux = Switch00Lux.state as Number
or
val lux = Switch00Lux.state

the first suggestion throws this:

==> /var/log/openhab2/openhab.log <==
2019-01-27 19:04:16.940 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Change Ceiling Light to White': Could not cast 127 to java.lang.Number; line 5, column 19, length 27

Ithink i tried with val as i mentioned but will give it another try… ahh btw with or without the int data type? you left it away in your edit… so I’m unsure

Without int.

ok will try

One last thing to try is

var Number lux = Switch00Lux.state.toString

Have you tried changing the item to a Number yet b/c the first example should have worked.:thinking:

I just tried to change the Item Type but until now I get no new value in log… will wait a little if the sensor responds

ok changing the item type to Number instead of String definately not working no data anymore…