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

Here’s another guess,:roll_eyes: at making it work.

var lux = (Switch00Lux.state as Number).intValue

Ok I first try your last suggestion above then I’ll try

Edit: throws no errors but the if clauses don’t work

==> /var/log/openhab2/openhab.log <==
2019-01-27 19:28:23.107 [INFO ] [se.smarthome.model.script.Lumination] - this rule does not function :(

will now try this…

That’s a good sign.:+1:

Did the error come when using this var Number lux = Switch00Lux.state.toString or one of the other attempt?

Ok
var lux = (Switch00Lux.state as Number).intValue

throws this error

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

I try

val lux = Switch00Lux.state.toString as Number

or

val lux = Switch00Lux.state.toString

now… after that your latest suggestion…

Problem is I have to wait for the sensor to respond after any change this takes a lot of additional time…

What did you use when you got this error?

val lux = Switch00Lux.state

Edit: if I use state.toString I get this error mentioned in my first post

==> /var/log/openhab2/openhab.log <==
2019-01-27 19:45:09.779 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'luminance.rules', using it anyway:
Cannot cast from String to Number

Since the Could not cast some number error didn’t pop up stick with that and allow the sensor time to respond and see what happens.

I did wait everytime I made a change… But with

val lux = Switch00Lux.state

nothing happens because no if clause is touched… instead the info log is triggered
that the rule is not functioning!!!

Have we tried this with val not var?

val Number lux = Switch00Lux.state

@Dan this is about the only other thing I can think of trying.

val Number lux = Integer::parseInt(transform("JSONPATH", "$.illuminance", Switch00Lux.state.toString))

Not sure about the JSONPATH part so you may need to adjust to fit what you have.

The item is a String type. So its state will be something like “32”, which you can’t do numeric comparisons with. You need to parse that string into an number, presumably an integer will do here.

val lux = Integer::parseInt(Switch00Lux.state)

Actually this has done the trick guys

val lux = Integer::parseInt(Switch00Lux.state.toString)

@rossko57 without ‘toString’ I got this error:

2019-01-27 22:34:37.552 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Change Ceiling Light to White': An error occurred during the script execution: Could not invoke method: java.lang.Integer.parseInt(java.lang.String) on instance: null

thanks for all your help!!!

the final Rule looks like this:

rule "Change Ceiling Light to White"
when
    Item Switch00Lux received update
then
    val lux = Integer::parseInt(Switch00Lux.state.toString)
    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

thank you all!

1 Like

Always trips me up that the state of a String Item isn’t a string, but there you are, a state is a state.

Hi All,

Could you help me with a variation on this please?

My HUE bulb gives a string for illuminance ending in " lx" have written the following rule to remove these characters, but I am still having problems.

I’ve changed val to a var and also for testing, I have separated them put to separate vars

var Number counter = 0
var Number lastCheck = 0

rule “Office Lights ON”
when
Item zigbee_device_01370A5C_286d970001073897_286D970001073897_1_intrusion changed to ON
then
var lux = hue_0106_001788499314_7_illuminance.state.toString
logInfo(“Office Lights”,“1”)

var ill = lux.replace(" lx",“”)
logInfo(“Office Lights”,“2”)

val bri = Integer::parseInt(ill)
logInfo(“Office Lights”,“3”)

if( bri < 37 )
//if(vTimeOfDay.state != “DAY”)
{
counter = counter + 1
gofficelights.sendCommand(100)
}
end

Error is:

2019-12-24 16:42:01.897 [INFO ] [smarthome.model.script.Office Lights] - 1

2019-12-24 16:42:01.901 [INFO ] [smarthome.model.script.Office Lights] - 2

2019-12-24 16:42:01.903 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Office Lights ON’: For input string: “0.9997697679981565”

Thank you

Ian

Try changing the when section to received update instead of ON.

when
Item zigbee_device_01370A5C_286d970001073897_286D970001073897_1_intrusion received update

Seems a long winded way to go about it.
How about -

if( hue_0106_001788499314_7_illuminance.state < 37 | "lx" )

Thank you that worked!!! I assume the “|” equates to remove the characters in the quotes?
Where would I find this syntax documented?

Ian

No, nothing like that at all. | is a separator and what follows is a unit of measurement.
32 | "lx" represents all one value, 32 lux. Not a simple number like 32.
As your Item is a QuantityType and so also has units, you can compare it directly with 32 lux.

1 Like