Light Level Adjustment

I’m trying to create a rule or two that controls when my living room lights are switched on and off. Using the time of day, whether or not the TV is switched on and the light level being reported or when motion is detected as I enter the living room.

So far I have the two rules below. Hopefully the rule name explains it well. I’m not a great fan of IF statements, but not being great at coding I have to live with what I have. Or as in this case, ask for some help and ideas.

I’m sure that is a far more efficient means of doing what I want, by building on the two rules below. My aim would be to achieve the same light level regardless of what triggers the event to switch the lights on or off.

Thanks for taking a look and any advice offered.

rule "Light Level Changed"
when
	Item Outside_ST815_Lux_Level changed
then
	if(now.getHourOfDay < 9 || Time_Of_Day.state == "MORNING") return;
	if(Outside_ST815_Lux_Level.state > 110) return;
	if(Outside_ST815_Lux_Level.state > 100) {
		Living_Room_Floor_Lamp_Switch.sendCommand(OFF)
		Living_Room_Table_Lamp_Switch.sendCommand(OFF)
		Living_Room_Twig_Lights.sendCommand(OFF)
		Kitchen_Bloom_Lamp_Switch.sendCommand(OFF)
	} else if(Outside_ST815_Lux_Level.state > 80 && Outside_ST815_Lux_Level.state < 100 && Living_Room_TV_Switch.state == ON && Time_Of_Day.state == "DAY" || Time_Of_Day.state == "EVENING") {
		Living_Room_Floor_Lamp_Dimmer.sendCommand(40)
		Living_Room_Table_Lamp_Dimmer.sendCommand(50)
		Kitchen_Bloom_Lamp_Dimmer.sendCommand(100)
	} else if(Outside_ST815_Lux_Level.state < 80 && Time_Of_Day.state == "DAY" && Living_Room_TV_Switch.state == ON) {
		Living_Room_Floor_Lamp_Dimmer.sendCommand(50)
		Living_Room_Table_Lamp_Dimmer.sendCommand(75)
		Living_Room_Twig_Lights.sendCommand(ON)
	} else if(Outside_ST815_Lux_Level.state < 50 && Living_Room_TV_Switch.state == ON) {
		Living_Room_Floor_Lamp_Dimmer.sendCommand(60)
		Living_Room_Table_Lamp_Dimmer.sendCommand(100)
	} else if(Outside_ST815_Lux_Level.state < 50 && Living_Room_TV_Switch.state == OFF && Time_Of_Day.state == "DAY" || Time_Of_Day.state == "EVENING") {
		Living_Room_Floor_Lamp_Dimmer.sendCommand(40)
		Living_Room_Table_Lamp_Dimmer.sendCommand(50)
		Kitchen_Bloom_Lamp_Dimmer.sendCommand(100)
	}
end
rule "Living Room Motion Sensor Activation"
when
	Item Living_Room_SP3102_Motion changed from OFF to ON
then
	postUpdate(Living_Room_Motion_Sensor_Last_Activation, new DateTimeType())
	if(Outside_ST815_Lux_Level.state < 110 && Living_Room_TV_Switch.state == OFF && Time_Of_Day.state == "EVENING" || Time_Of_Day.state == "NIGHT") {
		Living_Room_Table_Lamp_Dimmer.sendCommand(50)
	}
end

Well, one might come up with a clever calculation algorithm to replace your code, but why?
It’s you who has to maintain it in the future, so you need to understand it. Thus if it works, stick with it.

Yes it works, but that’s not always the point. I’d like to improve my rules and to learn as I do so.

I agree with Markus 100%, with modern CPU’s a little code that is not optimal is no big deal if it is easier to read. On that note I would recommend you add comments using // so you can understand it and why you did things in the future. I would personally use an algorithm that way there is more “resolution” and less rules to create more smooth seamless response.

Since you asked this jumped out at me. Changed to make it shorter…

if(Outside_ST815_Lux_Level.state > 100) {
		code here
	} else if(Outside_ST815_Lux_Level.state > 80 && Outside_ST815_Lux_Level.state < 100) {
code here
}

Consider this change as it is EXACTLY the same only more efficient. The first IF has meant that it is <100 when it reaches the second else if…

if(Outside_ST815_Lux_Level.state > 100) {
		code here
	} else if(Outside_ST815_Lux_Level.state > 80) {
code here
}

One approach I’ve seen that helps with Rules like this is documented in Design Pattern: How to Structure a Rule.

But in this case I’m not sure it would save many lines of code or make the Rule easier to read or understand.

Sometimes a bunch of if statements is the best you can do.

Thanks for the reply’s everyone. I’ll work on my rules using the above advice.