[SOLVED] Lights switch not correctly

Hi All,

I use following simple codes to turn on/off lights periodically

rule "Periodically check auto lights"
when
    Time cron "0 */10 * ? * *"
then
	// If nobody is home and dark outside
	if (AlleAusserHaus.state == ON && now.isAfter((Sunset_Time.state as DateTimeType).zonedDateTime.toInstant().toEpochMilli) ||
            now.isBefore((Sunrise_Time.state as DateTimeType).zonedDateTime.toInstant().toEpochMilli)){
		logInfo("LightsAuto", "Auto Light Check")
        gEG_Licht.members.forEach[lamp|
			lamp.sendCommand( lamp.historicState(now.minusDays(7),"rrd4j").state.toString )
         logInfo("myrule","historic state {}",lamp.historicState(now.minusDays(7),"rrd4j").state)
        ]
	}
end

Unfortunately, the lights swtiches even if we are at home or not:


2018-11-06 00:30:00.051 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 00:40:00.055 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 00:50:00.058 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 01:00:00.049 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 01:10:00.044 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 01:20:00.066 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 01:30:00.071 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 01:40:00.046 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 01:50:00.053 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 02:00:00.085 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 02:10:00.057 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 02:20:00.079 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 02:30:00.051 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 02:40:00.048 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 02:50:00.046 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 03:00:00.060 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 03:10:00.057 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 03:20:00.068 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 03:30:00.057 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 03:40:00.049 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 03:50:00.054 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 04:00:00.061 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 04:10:00.100 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 04:20:00.055 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 04:30:00.064 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 04:40:00.273 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 04:50:00.050 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 05:00:00.064 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 05:10:00.050 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 05:20:00.046 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 05:30:00.080 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 05:40:00.060 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 05:50:00.050 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 06:00:00.081 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 06:10:00.069 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 06:20:00.052 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 06:30:00.047 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 06:40:00.079 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 06:50:00.050 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 07:00:00.078 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-06 07:10:00.065 [INFO ] [se.smarthome.model.script.LightsAuto] - Auto Light Check
2018-11-05 07:55:13.801 [vent.ItemStateChangedEvent] - AlleAusserHaus changed from OFF to ON
2018-11-05 08:02:13.281 [vent.ItemStateChangedEvent] - AlleAusserHaus changed from ON to OFF
2018-11-06 08:08:15.110 [vent.ItemStateChangedEvent] - AlleAusserHaus changed from OFF to ON
2018-11-06 13:13:38.941 [vent.ItemStateChangedEvent] - AlleAusserHaus changed from ON to OFF

Does someone have an idea, what is wrong in the code?
I would expect, that the lights only switch at the moment the variable

AlleAusserHaus

is equal to

ON

.

Thanks for your help in advance.
Dom

The way I read the logical expresseion in the if clause is “a and b or c”, but IMO it should read “a and (b or c)”.

Try breaking down the if statement:

rule "Periodically check auto lights"
when
    Time cron "0 */10 * ? * *"
then
    // If nobody is home and dark outside
    if (AlleAusserHaus.state == ON) {
        if (now.isAfter((Sunset_Time.state as DateTimeType).zonedDateTime.toInstant().toEpochMilli) || now.isBefore((Sunrise_Time.state as DateTimeType).zonedDateTime.toInstant().toEpochMilli)) {
            logInfo("LightsAuto", "Auto Light Check")
            gEG_Licht.members.forEach[lamp|
                lamp.sendCommand( lamp.historicState(now.minusDays(7),"rrd4j").state.toString )
                logInfo("myrule","historic state {}",lamp.historicState(now.minusDays(7),"rrd4j").state)
            ]
        }
    }
end

@Rivi: Should be: if nobody is at home and is it between sunset and sunrise, then switch the lights periodically.

@vzorglub: l will try your code, and let you know the result.

Yes. But how is the computer supposed to know that if the logic statements are not grouped accordingly, to tell it that the first condition (not at home) must always be met, and then either one of the two others? At the moment, I suspect the rulen fires when not at home before midnight, and always after in the night. To put two if-clauses is sich a grouping, but I suspect parenthesis might have done it as well.

Yes, but nesting is easier to read in this case