Need help with light sensor rule

Hello,
I am trying to build a rule to use my light sensors for controlling my roller shutters.
In the end, the roller shutters should close 3/4 if there is direct sunlight for more than 5 minutes.
But now I am stuck right at the beginning, because my condition for detecting direct sunlight does not work.

So this is my light sensor item:

Number:Illuminance LightSensorEast "Helligkeitssensor Osten Helligkeit" { channel="deconz:lightsensor:9e712078:04cf8cdf3c78c9d5010400:lightlux" }

and this is my rule:

var Number LightSensorEastOldValue = -1.0

rule "Update Item LightSensorEast"
when
Item LightSensorEast changed
then
logInfo("LightSensorEast", "LightSensorEast changed to {}", LightSensorEast.state)
logInfo("LightSensorEast", "LightSensorEastOldValue is {}", LightSensorEastOldValue)

//initialize the old value variable, if it is -1.0
if(LightSensorEastOldValue == -1.0)
{
    LightSensorEastOldValue = LightSensorEast.state
}

if(LightSensorEastOldValue > 10000 &&
   LightSensorEast.state <= 10000)
{
    logInfo("LightSensorEast", "Open rollershutter east")
}
if(LightSensorEastOldValue < 50000 &&
   LightSensorEast.state >= 50000)
{
    logInfo("LightSensorEast", "Close rollershutter east")
}

LightSensorEastOldValue = LightSensorEast.state
end

I see log messages when the light sensor changes e.g.

2020-06-26 06:50:16.964 [INFO ] [arthome.model.script.LightSensorEast] - LightSensorEast changed to 50489 lx
2020-06-26 06:50:16.971 [INFO ] [arthome.model.script.LightSensorEast] - LightSensorEastOldValue is 41172 lx

But why is there no message “Close rollershutter east” in the log? It should have entered the if-block, because the new value is bigger then 50000 and the old value is less than 50000, right?
What am I missing?

Any help appreciated!

Regards
Bernd

Hi, the state need maybe converted to a number as old value. Otherwise the comparsion is between number and string.

1 Like

You need to compare the state to another QuantityType and the global variable should be a QuantityType…

var Number LightSensorEastOldValue = -1.0|lx
...
if (LightSensorEastOldValue > 10000|lx && LightSensorEast.state <= 10000|lx) {
1 Like

Thank you, now it is working :slight_smile:

2020-06-26 11:23:47.794 [INFO ] [arthome.model.script.LightSensorEast] - LightSensorEast changed to 80427 lx
2020-06-26 11:23:47.823 [INFO ] [arthome.model.script.LightSensorEast] - LightSensorEastOldValue is 45165 lx
2020-06-26 11:23:47.850 [INFO ] [arthome.model.script.LightSensorEast] - Close rollershutter east